import numpu as np
class Perceptron(object):
"""""
eta:学习率
n_iter:权重向量的训练次数
w_:神经分叉权重向量
errors:用于记录神经元判断出错次数
"""""
def _int_(self,eta=0.01,n_iter=10):
self.eta=eta,
self.n_iter=n_iter
pass
def fit(self,x,y):
"""""
输入训练数据,培训神经元,x输入样本向量,y对应样本分类
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=学习率*(y-y') y:预测值,y'计算值
"""""
update=self.eta*(target-self.predic(xi))
""""
xi 是一个向量
update*xi 等价:
[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 运行---------------------------
File "<ipython-input-8-161dd3d13aa5>", line 24 for _in range(self.n_iter) ^SyntaxError: invalid syntax
这么明显的错误,这里for _in range(self.n_iter) ,注意空格隔开呀,加工空格就行:for _ in range(self.n_iter)