課程
/后端開發(fā)
/Python
/機(jī)器學(xué)習(xí)-實(shí)現(xiàn)簡(jiǎn)單神經(jīng)網(wǎng)絡(luò)
如題,萬分感謝分享。
2017-05-16
源自:機(jī)器學(xué)習(xí)-實(shí)現(xiàn)簡(jiǎn)單神經(jīng)網(wǎng)絡(luò)
正在回答
import?numpy?as?np class?Perceptron(object): ????def?__init__(self,?eta=0.01,?n_iter=10): ????????self.eta=eta ????????self.n_iter=n_iter ???????? ????????pass ????def?fit(self,X,y): ????????self.W=np.zeros(1+X.shape[1]) ????????self.errors=[] ????????for?_?in?range(self.n_iter): ????????????error=0 ????????????dw=np.zeros(1+X.shape[1]) ????????????for?xi,target?in?zip(X,y):???????????????? ????????????????update=self.eta*(target-self.predict(xi)) ????????????????dw[1:]+=update*xi ????????????????dw[0]+=update ????????????????error+=int(update!=0) ????????????????pass ????????????self.W+=dw ????????????self.errors.append(error) ???????????? ????????????pass ???????? ????????pass ????def?net_input(self,xi): ????????return?np.dot(self.W[1:],xi)+self.W[0] ????def?predict(self,xi): ????????return?np.where(self.net_input(xi)>0,1,-1) ???? ??????? ????????pass #文件讀取 file="D:/python/nn/data.csv" import?pandas?as?pd df=pd.read_csv(file,header=None) df.head(10) #顯示原始數(shù)據(jù) import?matplotlib.pyplot?as?plt import?numpy?as?np y=df.loc[0:99,4].values y=np.where(y=='Iris-setosa',-1,1) X=df.iloc[0:100,[0,2]].values plt.scatter(X[:50,0],X[:50,1],color='red',marker='o',label='setosa') plt.scatter(X[50:100,0],X[50:100,1],color='blue',marker='x',label='versicolor'?) plt.xlabel(u'花瓣長度') plt.ylabel(u'花徑長度') plt.legend(loc='upper?left') #plt.show() #訓(xùn)練并打印錯(cuò)誤曲線 ppn=Perceptron(0.1,20) ppn.fit(X,y) print?(ppn.W) plt.scatter(range(1,len(ppn.errors)+1),ppn.errors,color='red',marker='o') #plt.show() #定義打印分類器邊界函數(shù) from?matplotlib.colors?import?ListedColormap def?plot_decision_regions(X,y,classifier,resolution=0.02): ????markers=('s','x','o','v') ????colors=('lightgreen','gray','cyan','red','blue') ????cmap=ListedColormap(colors[:len(np.unique(y))]) ???? ????x1_min,x1_max=X[:,0].min()-1,X[:,0].max() ????x2_min,x2_max=X[:,1].min()-1,X[:,1].max() ???? ????print(x1_min,x1_max) ????print(x2_min,x2_max) ???? ????xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution), ??????????????????????np.arange(x2_min,x2_max,resolution)?) ????#print(xx2.shape) ????#print(xx2) ???? ????z=classifier.predict(np.array([xx1.ravel(),xx2.ravel()])) ????#print(xx1.ravel()) ????#print(xx2.ravel()) ????#print(z) ????z=z.reshape(xx1.shape) ????plt.contourf(xx1,xx2,z,alpha=0.8,cmap=cmap) ????plt.xlim(xx1.min(),xx1.max()) ????plt.ylim(xx2.min(),xx2.max()) ???? ????for?idx,cl?in?enumerate(np.unique(y)): ????????plt.scatter(x=X[y==cl,0],y=X[y==cl,1],alpha=0.8,c=cmap(idx),marker=markers[idx],label=cl) #打印邊界及原始數(shù)據(jù) plot_decision_regions(X,y,ppn) plt.xlabel(u'花瓣長度') plt.ylabel(u'花徑長度') plt.legend(loc='upper?left') plt.show()
給你,每個(gè)模塊自己復(fù)制分開下
zjuPeco 提問者
for _ in range(self.n_iter):
? ? ? ? ? ? error=0
? ? ? ? ? ? dw=np.zeros(1+X.shape[1])
? ? ? ? ? ? for xi,target in zip(X,y): ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? update=self.eta*(target-self.predict(xi))
? ? ? ? ? ? ? ? dw[1:]+=update*xi
? ? ? ? ? ? ? ? dw[0]+=update
? ? ? ? ? ? ? ? error+=int(update!=0)
? ? ? ? ? ? ? ? pass
? ? ? ? ? ? self.W+=dw
? ? ? ? ? ? self.errors.append(error)
這段代碼中,在進(jìn)行10次迭代的過程中,每次迭代的時(shí)候都把權(quán)重重新置為 0 。
我覺得有點(diǎn)問題。(下劃線部分)
如果每次迭代都 從新 將權(quán)重置為0,那么每次迭代都是一樣的,是沒有意義的。
如果每次迭代后 權(quán)重 被帶到下一次循環(huán)中,那么在經(jīng)過10次迭代后得到的權(quán)重會(huì)更加的準(zhǔn)確。
攻城獅ZSP
舉報(bào)
人工智能時(shí)代,你準(zhǔn)備好成為抓住機(jī)遇的那百分之二嗎。
3 回答有木有完整的代碼
1 回答編碼出錯(cuò),求助
3 回答代碼與數(shù)據(jù)
1 回答問一下,如果只用30個(gè)數(shù)據(jù)做訓(xùn)練,剩下的20個(gè)數(shù)據(jù)做預(yù)測(cè),這樣應(yīng)該怎么改,改完之后代碼有問題。求教大佬。
3 回答代碼運(yùn)行錯(cuò)誤,我是按照老師的代碼寫的啊
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-05-18
給你,每個(gè)模塊自己復(fù)制分開下
2017-05-21
for _ in range(self.n_iter):
? ? ? ? ? ? error=0
? ? ? ? ? ? dw=np.zeros(1+X.shape[1])
? ? ? ? ? ? for xi,target in zip(X,y): ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? update=self.eta*(target-self.predict(xi))
? ? ? ? ? ? ? ? dw[1:]+=update*xi
? ? ? ? ? ? ? ? dw[0]+=update
? ? ? ? ? ? ? ? error+=int(update!=0)
? ? ? ? ? ? ? ? pass
? ? ? ? ? ? self.W+=dw
? ? ? ? ? ? self.errors.append(error)
這段代碼中,在進(jìn)行10次迭代的過程中,每次迭代的時(shí)候都把權(quán)重重新置為 0 。
我覺得有點(diǎn)問題。(下劃線部分)
如果每次迭代都 從新 將權(quán)重置為0,那么每次迭代都是一樣的,是沒有意義的。
如果每次迭代后 權(quán)重 被帶到下一次循環(huán)中,那么在經(jīng)過10次迭代后得到的權(quán)重會(huì)更加的準(zhǔn)確。