python3機器學習經典實例-學習筆記6-分類演算法
創建一個簡單的分類器
- 首先本程序需要用到的數據包
import numpy as npimport matplotlib.pyplot as plt
補充:python中的list和array的不同之處
python中的list是python的內置數據類型,list中的數據類不必相同的,而array的中的類型必須全部相同。在list中的數據類型保存的是數據的存放的地址,簡單的說就是指針,並非數據,這樣保存一個list就太麻煩了,例如list1=[1,2,3,a]需要4個指針和四個數據,增加了存儲和消耗cpu。
numpy中封裝的array有很強大的功能,裡面存放的都是相同的數據類型
- 生成二維輸入的數據array和標籤labels
# input dataX = np.array([[3,1], [2,5], [1,8], [6,4], [5,2], [3,5], [4,7], [4,-1]])# labelsy = [0, 1, 1, 0, 0, 1, 1, 0]
- 基於y的值標籤將輸入數據class_0 和class_1 分為兩類
# separate the data into classes based on yclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
- 將分好的class_0 和class_1 進行作圖,並以不同的形狀進行數據的標記。結果如下圖。
# plot input dataplt.figure()plt.scatter(class_0[:,0], class_0[:,1], color=black, marker=s)plt.scatter(class_1[:,0], class_1[:,1], color=black, marker=x)
這裡是自己生成的一條直線,並不是依據分類的節點的坐標。也就是說本例只是讓我們簡單的知道分類的形式。
- 畫線斜率為1的直線
# draw the separator lineline_x = range(10)line_y = line_x
將分類的結果進行圖像顯示:
# plot labeled data and separator line plt.figure()plt.scatter(class_0[:,0], class_0[:,1], color=black, marker=s)plt.scatter(class_1[:,0], class_1[:,1], color=black, marker=x)plt.plot(line_x, line_y, color=black, linewidth=3)plt.show()
結果如下:
未完待續。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
推薦閱讀:
※【機器學習Machine Learning】資料大全
※2 最簡單的驗證碼生成
※吳恩達機器學習第三周課後感
※尋找邊界
※TensorFlow-機器學習入門(第一課)


