缺少的那一节
我用的python3.7,调整了视频中的错误,函数的位置和命名问题,但是出现TypeError: can't multiply sequence by non-int of type 'float'这个错误,python3中不能这么写吗,以下为我的感知器类代码
import numpy as np
class Perception(object):
'''
eta:学习率
n_iter:权重向量的训练次数
w_:神经分叉权重向量
error_:记录神经元判断出错次数
'''
def __init__(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)
n_samples:样本量
n_feature:神经元分叉数量
例:x:[[1,2,3],[4,5,6]]
n_samples:2
n_feature:3
y:[1,-1]
'''
'''
初始化权重向量为0
加一是因为将前面算法提到的w0,也就是步调函数阈值
w_:权重向量
error_:错误记录向量
'''
self.errors_ = []
self.w_ = np.zeros(1 + x.shape[1])
for _ in range(self.n_iter):
errors = 0
for xi,target in zip(x,y):
'''
zip(x,y):[[1,2,3,1],[4,5,6,-1]]
'''
update = (self.eta) * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
'''
▽w(n) = update * x[n]
'''
errors += int(update != 0.0)
self.errors_.append(errors)
pass
pass
def net_input(self,x):
'''
z = w0 * 1 + w1 * x1 +... +wn * xn
'''
z = np.dot(x,self.w_[1:]) + self.w_[0]
return z
pass
def predict(self,x):
return np.where(self.net_input(x)>=0.0,1,-1)
pass