3.0運(yùn)行問(wèn)題解答
import numpu as np
class Perceptron(object):
? ? """""
? ? eta:學(xué)習(xí)率
? ? n_iter:權(quán)重向量的訓(xùn)練次數(shù)
? ? w_:神經(jīng)分叉權(quán)重向量
? ? errors:用于記錄神經(jīng)元判斷出錯(cuò)次數(shù) ? ?
? ? """""
? ? def _int_(self,eta=0.01,n_iter=10): ? ? ?
? ? ? ? self.eta=eta,
? ? ? ? self.n_iter=n_iter
? ? pass
def fit(self,x,y):
? ? """""
? ? 輸入訓(xùn)練數(shù)據(jù),培訓(xùn)神經(jīng)元,x輸入樣本向量,y對(duì)應(yīng)樣本分類(lèi)
? ? x:shape[n_samples,n_features]
? ? x:[[1,2,3],[4,5,6]]
? ? n_samples:2
? ? n_features:3
? ? y:[1,-1]
? ? """""
? ? self.w_=np.zero(1+x.shape[1])
? ? self.errors_=[]
? ? for _in range(self.n_iter)
? ? ? ? error=0
? ? ? ? """""
? ? ? ? x:[1,2,3],[4,5,6]
? ? ? ? y:[1,-1]
? ? ? ? zip作用:
? ? ? ? zip[x,y]=[[1,2,3,1],[4,5,6,-1]]
? ? ? ? """""
? ? ? ? for xi,target in zip(x,y)
? ? ? ? ? ? """""
? ? ? ? ? ? update=學(xué)習(xí)率*(y-y') y:預(yù)測(cè)值,y'計(jì)算值
? ? ? ? ? ? """""
? ? ? ? ? ? update=self.eta*(target-self.predic(xi))
? ? ? ? ? ? """"
? ? ? ? ? ? xi 是一個(gè)向量
? ? ? ? ? ? update*xi 等價(jià):
? ? ? ? ? ? [w(1)]=x[1]*update,[w(2)]=x[2]*update,[w(3)]=x[3]*update
? ? ? ? ? ? """"
? ? ? ? ? ? self.w_[1:]+=update*xi
? ? ? ? ? ? self.w_[0]+=update
? ? ? ? ? ? errors+=int(update!==0.0)
? ? ? ? ? ? self.errors_.append(errors)
? ? ? ? pass ??
? ? ? ??
? ? pass
? ? def net_input(self,x):
? ? """"
? ? z=W0*1+W1*X1+...Wn*Xn
? ? """"
? ? return np.dot[x,self.w_[1:]]+self.w_[0]
? ? pass
? ? def predict(self,x):
? ? ? ? return np.where(self.net_input(x)>=0.0,1,-1) ??
? ? pass
pass
------------------3.6 運(yùn)行---------------------------
File "<ipython-input-8-161dd3d13aa5>", line 24 ? ?for _in range(self.n_iter) ? ? ? ? ? ? ? ?^SyntaxError: invalid syntax
2017-09-29
這么明顯的錯(cuò)誤,這里for _in range(self.n_iter) ,注意空格隔開(kāi)呀,加工空格就行:for _ in range(self.n_iter)?