第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

正在回答

2 回答

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ù)制分開下

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

zjuPeco 提問者

非常感謝!
2017-05-23 回復(fù) 有任何疑惑可以回復(fù)我~

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)確。


1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

攻城獅ZSP

不好意思,我改了一下老師的代碼,這里的dw是w的梯度,在循環(huán)內(nèi)部進(jìn)行累加,在每一輪迭代時(shí)更新一次。不能每計(jì)算一次x,y就進(jìn)行一次更新. self.W才是真正的權(quán)重,這個(gè)沒有被置零過。
2017-05-24 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

求第3章完整代碼

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)