代碼運(yùn)行有誤,大神們幫忙看看
老師好,運(yùn)行代碼時(shí)顯示構(gòu)造類傳參有誤,哪里錯(cuò)了呢?
####### 算法代碼 #######
import numpy as np
class Perceptron(object):
? ? """
? ? eta: 學(xué)習(xí)率
? ? n_iter: 權(quán)重向量的訓(xùn)練次數(shù)
? ? w_: 神經(jīng)分叉權(quán)重向量
? ? errors_: 隊(duì)列,用于記錄神經(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_feateures]
? ? ? ? X:[1, 2, 3], [4, 5, 6]
? ? ? ? n_samples: 2
? ? ? ? n_features: 3
? ? ? ??
? ? ? ? y:[1, -1]
? ? ? ? """
? ? ? ? """
? ? ? ? 初始化權(quán)重向量為0
? ? ? ? 加一是因?yàn)榍懊嫠惴ㄌ岬降膚0,也就是步調(diào)函數(shù)閾值
? ? ? ? """
? ? ? ? self.w_ = np.zeros(1 + X.shape[1])
? ? ? ? self.errors_ = []
? ? ? ??
? ? ? ? for _ in range(self.n_iter):
? ? ? ? ? ? errors = 0
? ? ? ? ? ? """
? ? ? ? ? ? X = [[1,2,3], [4,5,6]]
? ? ? ? ? ? y = [1,-1]
? ? ? ? ? ? zip(X,y) = [([1,2,3], 1), ([4,5,6], -1)]
? ? ? ? ? ? """
? ? ? ? ? ? for xi, target in zip(X,y):
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? update = η * (y - y')
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? update = self.eta * (target - self.predict(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
####### 圖形化代碼?#######
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來(lái)正常顯示負(fù)號(hào)
# 取出0到100行第四列
y = df.loc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
# 抽取前100條數(shù)據(jù)的第0列和第2列
X = df.iloc[0:100, [0, 2]].values
# 前50條數(shù)據(jù)第0列當(dāng)作x坐標(biāo),前50條數(shù)據(jù)第1列當(dāng)作y坐標(biāo)
plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')
plt.xlabel('花瓣長(zhǎng)度')
plt.ylabel('花徑長(zhǎng)度')
# 設(shè)置圖例位置
plt.legend(loc='upper left')
plt.show()
####### 報(bào)錯(cuò)的代碼 #######
ppn = Perceptron(eta=0.1, n_iter=10)
ppn.fit(X, y)
plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o')
plt.xlabel('Epochs')
plt.ylabel('錯(cuò)誤分類次數(shù)')
plt.show()
上面代碼運(yùn)行時(shí)報(bào)錯(cuò),Perceptron類中申明了predict方法,為什么報(bào)沒(méi)有predict屬性呢:
<ipython-input-98-9ba268a8ffae> in fit(self, X, y)
? ? ?40? ? ? ? ? ? ? ? ?update = η * (y - y')
? ? ?41? ? ? ? ? ? ? ? ?"""
---> 42? ? ? ? ? ? ? ? ?update = self.eta * (target - self.predict(xi))
? ? ?43? ? ? ? ? ? ? ? ?"""
? ? ?44? ? ? ? ? ? ? ? ?xi是一個(gè)向量
AttributeError: 'Perceptron' object has no attribute 'predict'
2018-08-06
需要把def net_input()和def predict()兩個(gè)函數(shù)放到def fit()函數(shù)的前面
原因:代碼好像是從上往下解釋的,執(zhí)行到fit函數(shù)式,程序還不知道有net_input和predict這兩個(gè)函數(shù)