python机器学习之分类器
生活随笔
收集整理的這篇文章主要介紹了
python机器学习之分类器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡單分類器
1 import numpy as np 2 import matplotlib.pyplot as mp 3 x = np.array([ 4 [3, 1], 5 [2, 5], 6 [1, 8], 7 [6, 4], 8 [5, 2], 9 [3, 5], 10 [4, 7], 11 [4, -1]]) 12 y = np.array([0, 1, 1, 0, 0, 1, 1, 0]) 13 14 # X[:,0]是numpy中數(shù)組的一種寫法,表示對一個二維數(shù)組,取該二維數(shù)組第一維中的所有數(shù)據(jù),第二維中取第0個數(shù)據(jù),直觀來說,X[:,0]就是取所有行的第0個數(shù)據(jù), 15 # X[:,1] 就是取所有行的第1個數(shù)據(jù)。 16 l, r, h = x[:, 0].min() - 1, x[:, 0].max() + 1, 0.005 17 b, t, v = x[:, 1].min() - 1, x[:, 1].max() + 1, 0.005 18 # meshgrid()根據(jù)傳入的兩個一維數(shù)組參數(shù)生成兩個數(shù)組元素的列表 19 grid_x = np.meshgrid(np.arange(l, r, h), np.arange(b, t, v)) 20 # 做列拼接 21 # np.c_是按行連接兩個矩陣,就是把兩矩陣左右相加,要求行數(shù)相等,類似于pandas中的merge() 22 flat_x = np.c_[grid_x[0].ravel(), grid_x[1].ravel()] 23 flat_y = np.zeros(len(flat_x), dtype=int) 24 25 flat_y[flat_x[:, 0] < flat_x[:, 1]] = 1 26 grid_y = flat_y.reshape(grid_x[0].shape) 27 28 29 mp.figure('Simple Classfication', facecolor='lightgray') 30 mp.title('Simple Classfication', fontsize=20) 31 mp.xlabel('x', fontsize=14) 32 mp.ylabel('y', fontsize=14) 33 mp.tick_params(labelsize=10) 34 # 用顏色繪制網(wǎng)格 35 mp.pcolormesh(grid_x[0], grid_x[1], grid_y, cmap='gray') 36 mp.scatter(x[:, 0], x[:, 1], c=y, cmap='brg', s=60) 37 mp.show() 簡單分類邏輯分類
J(k1,k2,b) = sigma(-ylog(y')-(1-y)log(1-y'))/m +m正則函數(shù)(||k1,k2,b||)x正則強度
接口:
sklearn.linear_model.LogisticRegression(solver='liblinear', C=正則強度)
1 import numpy as np 2 import sklearn.linear_model as lm 3 import matplotlib.pyplot as mp 4 x = np.array([ 5 [4, 7], 6 [3.5, 8], 7 [3.1, 6.2], 8 [0.5, 1], 9 [1, 2], 10 [1.2, 1.9], 11 [6, 2], 12 [5.7, 1.5], 13 [5.4, 2.2]]) 14 y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]) 15 # 創(chuàng)建邏輯分類器 16 model = lm.LogisticRegression(solver='liblinear', 17 C=1000) 18 model.fit(x, y) 19 l, r, h = x[:, 0].min() - 1, x[:, 0].max() + 1, 0.005 20 b, t, v = x[:, 1].min() - 1, x[:, 1].max() + 1, 0.005 21 grid_x = np.meshgrid(np.arange(l, r, h), 22 np.arange(b, t, v)) 23 flat_x = np.c_[grid_x[0].ravel(), grid_x[1].ravel()] 24 flat_y = model.predict(flat_x) 25 grid_y = flat_y.reshape(grid_x[0].shape) 26 mp.figure('Logistic Classification', 27 facecolor='lightgray') 28 mp.title('Logistic Classification', fontsize=20) 29 mp.xlabel('x', fontsize=14) 30 mp.ylabel('y', fontsize=14) 31 mp.tick_params(labelsize=10) 32 mp.pcolormesh(grid_x[0], grid_x[1], grid_y, cmap='gray') 33 mp.scatter(x[:, 0], x[:, 1], c=y, cmap='brg', s=60) 34 mp.show() 邏輯分類器轉(zhuǎn)載于:https://www.cnblogs.com/qinhao2/p/9397806.html
總結(jié)
以上是生活随笔為你收集整理的python机器学习之分类器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浦发银行的信用卡能取现余额吗 其实在申领
- 下一篇: Java使用MyEclipse2017时