從零開始機器學習 第1篇 一個感知機的實現

來源:互聯網
上載者:User

標籤:bsp   csv   ade   archive   machine   das   head   分類   src   

0. 訓練資料集:Iris dataset (鳶尾花資料集),擷取網址https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data

如所示,鳶尾花資料集中每行資料前四列為特徵值分別是花瓣長/寬、花萼長/寬,鳶尾花分三類:Setosa,Versicolor,Virginica

   

 

 

 

可以用如下範例程式碼儲存資料集並顯示最後5行

1 import pandas as pd2 df = pd.read_csv(‘https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data‘, header=None)3 df.tail()

 

通過對資料集中的鳶尾花四個特徵值,對資料集分類,確定屬於哪一類的鳶尾花,這裡選取前100個資料分析,範例程式碼如下:

 1 import matplotlib.pyplot as plt 2 import numpy as np 3 import pandas as pd 4  5 df = pd.read_csv(‘https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data‘, header=None) 6  7  8 # select setosa and versicolor 9 y = df.iloc[0:100, 4].values10 y = np.where(y == ‘Iris-setosa‘, -1, 1)11 12 # extract sepal length and petal length13 X = df.iloc[0:100, [0, 2]].values14 15 # plot data16 plt.scatter(X[:50, 0], X[:50, 1],17             color=‘red‘, marker=‘o‘, label=‘setosa‘)18 plt.scatter(X[50:100, 0], X[50:100, 1],19             color=‘blue‘, marker=‘x‘, label=‘versicolor‘)20 21 plt.xlabel(‘sepal length [cm]‘)22 plt.ylabel(‘petal length [cm]‘)23 plt.legend(loc=‘upper left‘)24 25 plt.tight_layout()26 27 plt.show()

輸出結果如下:

通過對四個特徵中兩個(花瓣長,花萼長)的統計,可以看到藍色和紅色有了明顯的界限,實現了分類。

從零開始機器學習 第1篇 一個感知機的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.