亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python中的簡單感知器模型

python中的簡單感知器模型

鴻蒙傳說 2022-11-09 16:56:15
我在運行 fit 函數時遇到這種類型的錯誤。許多人說它在 python 2.7 中運行。我想知道如何在 python 3 中完成它。還有其他方法可以做到嗎?class Perceptron:    def __init__(self):        self.w=None        self.b=None    def model(self,x):        return 1 if (np.dot(self.w,x)>=self.b) else 0    def predict(self,X):        Y=[]        for x in X:            result = self.model(x)            Y.append(result)        return np.array(Y)    def fit(self, X, Y, epochs = 1, lr=1):        self.w = np.ones(X.shape[1])        self.b = 0        accuracy = {}        max_accuracy = 0        wt_matrix = []        for i in range(epochs):            for x, y in zip(X,Y):                y_pred = self.model(x)                if y==1 and y_pred == 0:                    self.w = self.w +lr* x                    self.b = self.b + lr*1                elif y==0 and y_pred== 1:                    self.w = self.w-lr*x                    self.b = self.b-lr*1            wt_matrix.append(self.w)            accuracy[i] =  accuracy_score(self.predict(X),Y)            if(accuracy[i]>max_accuracy):                max_accuracy = accuracy[i]                chkptw=self.w                chkptb=self.b        self.w =chkptw        self.b=chkptb        print(max_accuracy)        plt.plot(accuracy.values())        plt.ylim([0,1])        plt.show           return np.array(wt_matrix) 這是代碼:wt_matrix = perceptron.fit(X_train,Y_train,100)當我調用該函數時,它顯示了這種類型的錯誤TypeError                                 Traceback (most recent call last)<ipython-input-76-8b850a516f0e> in <module>()----> 1 wt_matrix = perceptron.fit(X_train,Y_train,100)8 frames/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)     83      84     """---> 85     return array(a, dtype, copy=False, order=order)     86      87 TypeError: float() argument must be a string or a number, not 'dict_values'
查看完整描述

1 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

這是一個簡單的類型轉換問題。改變

plt.plot(accuracy.values())

plt.plot(list(accuracy.values()))


查看完整回答
反對 回復 2022-11-09
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號