課程
/后端開發(fā)
/Python
/機(jī)器學(xué)習(xí)-實(shí)現(xiàn)簡單神經(jīng)網(wǎng)絡(luò)
老師,你用到的代碼在哪里呀?數(shù)據(jù)呢?可以放出來給我們試一試嗎?
2018-06-17
源自:機(jī)器學(xué)習(xí)-實(shí)現(xiàn)簡單神經(jīng)網(wǎng)絡(luò) 4-1
正在回答
調(diào)用sklearn包中datasets,里面有好像iris數(shù)據(jù)集
iris數(shù)據(jù)集,可以到我的百度網(wǎng)盤下載:
https://pan.baidu.com/s/17dK9fdGHzGY1SfI-s1pt6w
class?AdalineG(object):???? ????"""???? ????eta:float???? ????學(xué)習(xí)效率,處于0和1之間???????? ????n_iter:int????對訓(xùn)練數(shù)據(jù)進(jìn)行學(xué)習(xí)改進(jìn)次數(shù)???????? ????w_:一維向量???? ????存儲權(quán)重?cái)?shù)值???????? ????error_:????存儲每次迭代改進(jìn)時(shí),網(wǎng)絡(luò)對數(shù)據(jù)進(jìn)行錯誤判斷的次數(shù)???? ????"""???????????? ????def?__init__(self,eta=0.01,n_iter=50):???????? ????????self.eta?=?eta???????? ????????self.n_iter?=?n_iter???????????? ????def?net_input(self,?X):???????? ????????return?np.dot(X,?self.w_[1:])?+?self.w_[0]???????? ????def?activation(self,?X):???????? ????????return?self.net_input(X)??????? ????def?predict(self,?X):???????? ????????return?np.where(self.activation(X)?>=0,?1,?-1)???????? ????def?fit(self,?X,?y):???????? ????????"""???????? ????????X:二維數(shù)組[n_sampls,?n_features]???????? ????????n_samples?表示X中含有訓(xùn)練數(shù)據(jù)條目數(shù)???????? ????????n_faetures?含有4個數(shù)據(jù)的一維向量,用于表示一條訓(xùn)練條目???????????????? ????????y:一維向量???????? ????????用于存儲每一訓(xùn)練條目對應(yīng)的正確分類???????? ????????"""???????????????? ????????self.w_?=?np.zeros(1?+?X.shape[1])???????? ????????self.cost_?=?[]???????????????? ????????for?i?in?range(self.n_iter):???????????? ????????????output?=?self.net_input(X)???????????? ????????????errors?=?(y-output)???????????? ????????????self.w_[1:]?+=?self.eta?*?X.T.dot(errors)???????????? ????????????self.w_[0]?+=?self.eta?*?errors.sum()???????????? ????????????cost?=?(errors?**?2).sum()?/2.0???????????? ????????????self.cost_.append(cost)???????? ????????return?self ???????? ???????? file?=?"D:/PyCharm_test_file/Jupyter_test/iris1.xlsx"?#此處添加iris數(shù)據(jù)集 import?pandas?as?pd df?=?pd.read_excel(file,header=None) import?matplotlib.pyplot?as?plt import?numpy?as?np y?=?df.loc[0:99,?4].values y?=?np.where(y?==?'Iris-setosa',?-1,?1) X?=?df.iloc[0:100,?[0,?2]].values from?matplotlib.colors?import?ListedColormap def?plot_decision_regions(X,?y,?classifier,?resolution=0.02):???? ????marker?=?('s',?'x',?'o',?'v')???? ????colors?=?('red',?'blue',?'lightgreen',?'gray',?'cyan')???? ????cmap?=?ListedColormap(colors[:len(np.unique(y))])???? ????x1_min,?x1_max?=?X[:,?0].min()?-?1,?X[:,?0].max()???? ????x2_min,?x2_max?=?X[:,?1].min()?-?1,?X[:,?1].max()???? ????#將x1、x2最大最小值通過arange函數(shù)得到的向量,擴(kuò)展成兩個二維矩陣???? ????xx1,?xx2?=?np.meshgrid(np.arange(x1_min,?x1_max,?resolution),?np.arange(x2_min,?x2_max,?resolution))???? ????#預(yù)測???? ????Z?=?classifier.predict(np.array([xx1.ravel(),?xx2.ravel()]).T)?#ravel還原成單維向量???? ????#繪制???? ????Z=?Z.reshape(xx1.shape)?#將Z轉(zhuǎn)換成與xx1一樣的二維數(shù)組???? ????plt.contourf(xx1,?xx2,?Z,?alpha=0.4,?cmap=cmap)?#在兩組分類結(jié)果中間畫分割線-->必須線性可分???? ????plt.xlim(xx1.min(),?xx1.max())???? ????plt.ylim(xx2.min(),?xx2.max())???? ????for?idx,?cl?in?enumerate(np.unique(y)):???????? ????????plt.scatter(x=X[y==cl,?0],?y=X[y==cl,?1],?alpha=0.8,?c=cmap(idx),?marker=marker[idx],?label=cl) ???????? ???????? ???????? ada?=?AdalineG(eta=0.0001,?n_iter=50) ada.fit(X,?y) plot_decision_regions(X,?y,?classifier=ada) plt.title('Adaline-Gradient?descent') plt.rcParams['font.sans-serif']=['SimHei'] plt.xlabel('花莖長度') plt.ylabel('花瓣長度') plt.legend(loc='upper?left') plt.show() """ 打印出模型對數(shù)據(jù)判斷的錯誤次數(shù)(迭代過程) """ plt.plot(range(1,?len(ada.cost_)+1),?ada.cost_,?marker='o') plt.xlabel('Epochs(迭代次數(shù))') plt.ylabel('sum-squard-error')plt.show()
舉報(bào)
人工智能時(shí)代,你準(zhǔn)備好成為抓住機(jī)遇的那百分之二嗎。
4 回答關(guān)于數(shù)據(jù)獲取
2 回答數(shù)據(jù)顯示問題
1 回答問一下,如果只用30個數(shù)據(jù)做訓(xùn)練,剩下的20個數(shù)據(jù)做預(yù)測,這樣應(yīng)該怎么改,改完之后代碼有問題。求教大佬。
2 回答后面數(shù)據(jù)的處理與可視化,基本沒聽懂,有哪位大神可以解讀一下嗎
2 回答誰有訓(xùn)練用的數(shù)據(jù)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-08-12
調(diào)用sklearn包中datasets,里面有好像iris數(shù)據(jù)集
2018-07-13
iris數(shù)據(jù)集,可以到我的百度網(wǎng)盤下載:
https://pan.baidu.com/s/17dK9fdGHzGY1SfI-s1pt6w
2018-07-13