缺少的那一節(jié)
我用的python3.7,調(diào)整了視頻中的錯(cuò)誤,函數(shù)的位置和命名問題,但是出現(xiàn)TypeError: can't multiply sequence by non-int of type 'float'這個(gè)錯(cuò)誤,python3中不能這么寫嗎,以下為我的感知器類代碼
import numpy as np
class Perception(object):
??? '''
??? eta:學(xué)習(xí)率
??? n_iter:權(quán)重向量的訓(xùn)練次數(shù)
??? w_:神經(jīng)分叉權(quán)重向量
??? error_:記錄神經(jīng)元判斷出錯(cuò)次數(shù)
??? '''
??? def __init__(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)樣本分類
??????? x:shape(n_samples,n_features)
??????? n_samples:樣本量
??????? n_feature:神經(jīng)元分叉數(shù)量
??????? 例:x:[[1,2,3],[4,5,6]]
??????? n_samples:2
??????? n_feature:3
??????? y:[1,-1]
??????? '''
??????? '''
??????? 初始化權(quán)重向量為0
??????? 加一是因?yàn)閷⑶懊嫠惴ㄌ岬降膚0,也就是步調(diào)函數(shù)閾值
??????? w_:權(quán)重向量
??????? error_:錯(cuò)誤記錄向量
??????? '''
??????? 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,這是我的代碼,謝謝采納~