缺少的那一節
我用的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
2020-07-20
我也用的python3.7,這是我的代碼,謝謝采納~