本筆記為參加阿里云“天池龍珠計劃 機(jī)器學(xué)習(xí)訓(xùn)練營”所做的學(xué)習(xí)記錄大审,代碼及知識內(nèi)容均來源于訓(xùn)練營,本人稍作擴(kuò)充亏栈。
具體活動內(nèi)容請移步阿里云天池龍珠計劃; 同時感謝公眾號“機(jī)器學(xué)習(xí)煉丹術(shù)”的介紹球及、推廣和組織。
4 算法實戰(zhàn)
4.1 Demo實踐
Step1:庫函數(shù)導(dǎo)入
## 基礎(chǔ)函數(shù)庫
import numpy as np
## 導(dǎo)入畫圖庫
import matplotlib.pyplot as plt
import seaborn as sns
## 導(dǎo)入邏輯回歸模型函數(shù)
from sklearn.linear_model import LogisticRegression
Step2:模型訓(xùn)練
##Demo演示LogisticRegression分類
## 構(gòu)造數(shù)據(jù)集
x_fearures = np.array([[-1, -2], [-2, -1], [-3, -2], [1, 3], [2, 1], [3, 2]])
y_label = np.array([0, 0, 0, 1, 1, 1])
## 調(diào)用邏輯回歸模型
lr_clf = LogisticRegression()
## 用邏輯回歸模型擬合構(gòu)造的數(shù)據(jù)集
lr_clf = lr_clf.fit(x_fearures, y_label) #其擬合方程為 y=w0+w1*x1+w2*x2
Step3:模型參數(shù)查看
## 查看其對應(yīng)模型的w
print('the weight of Logistic Regression:',lr_clf.coef_)
## 查看其對應(yīng)模型的w0
print('the intercept(w0) of Logistic Regression:',lr_clf.intercept_)
#Output:
# the weight of Logistic Regression: [[0.73455784 0.69539712]]
# the intercept(w0) of Logistic Regression: [-0.13139986]
Step4:數(shù)據(jù)和模型可視化
## 可視化構(gòu)造的數(shù)據(jù)樣本點
plt.figure()
plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap='viridis')
plt.title('Dataset')
plt.show()
## 可視化決策邊界
plt.figure()
plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap='viridis')
plt.title('Dataset')
nx, ny = 200, 100
x_min, x_max = plt.xlim()
y_min, y_max = plt.ylim()
x_grid, y_grid = np.meshgrid(np.linspace(x_min, x_max, nx),np.linspace(y_min, y_max, ny))
z_proba = lr_clf.predict_proba(np.c_[x_grid.ravel(), y_grid.ravel()])
z_proba = z_proba[:, 1].reshape(x_grid.shape)
plt.contour(x_grid, y_grid, z_proba, [0.5], linewidths=2., colors='blue')
plt.show()
## 可視化預(yù)測新樣本
plt.figure()
## new point 1
x_fearures_new1 = np.array([[0, -1]])
plt.scatter(x_fearures_new1[:,0],x_fearures_new1[:,1], s=50, cmap='viridis')
plt.annotate(s='New point 1',xy=(0,-1),xytext=(-2,0),color='blue',arrowprops=dict(arrowstyle='-|>',connectionstyle='arc3',color='red'))
## new point 2
x_fearures_new2 = np.array([[1, 2]])
plt.scatter(x_fearures_new2[:,0],x_fearures_new2[:,1], s=50, cmap='viridis')
plt.annotate(s='New point 2',xy=(1,2),xytext=(-1.5,2.5),color='red',arrowprops=dict(arrowstyle='-|>',connectionstyle='arc3',color='red'))
## 訓(xùn)練樣本
plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap='viridis')
plt.title('Dataset')
# 可視化決策邊界
plt.contour(x_grid, y_grid, z_proba, [0.5], linewidths=2., colors='blue')
plt.show()
Step5:模型預(yù)測
## 在訓(xùn)練集和測試集上分布利用訓(xùn)練好的模型進(jìn)行預(yù)測
y_label_new1_predict = lr_clf.predict(x_fearures_new1)
y_label_new2_predict = lr_clf.predict(x_fearures_new2)
print('The New point 1 predict class:\n',y_label_new1_predict)
print('The New point 2 predict class:\n',y_label_new2_predict)
## 由于邏輯回歸模型是概率預(yù)測模型(前文介紹的 p = p(y=1|x,\theta)),所有我們可以利用 predict_proba 函數(shù)預(yù)測其概率
y_label_new1_predict_proba = lr_clf.predict_proba(x_fearures_new1)
y_label_new2_predict_proba = lr_clf.predict_proba(x_fearures_new2)
print('Predicted probability of the New point 1 for each class:\n',y_label_new1_predict_proba)
print('Predicted probability of the New point 2 for each class:\n',y_label_new2_predict_proba)
#Output:
#The New point 1 predict class:
# [0]
#The New point 2 predict class:
# [1]
#Predicted probability of the New point 1 for each class:
# [[0.69567724 0.30432276]]
#Predicted probability of the New point 2 for each class:
# [[0.11983936 0.88016064]]:
可以發(fā)現(xiàn)訓(xùn)練好的回歸模型將X_new1預(yù)測為了類別0(判別面左下側(cè))叮贩,X_new2預(yù)測為了類別1(判別面右上側(cè))击狮。其訓(xùn)練得到的邏輯回歸模型的概率為0.5的判別面為上圖中藍(lán)色的線。
所謂決策邊界就是能夠把樣本正確分類的一條邊界益老,主要有線性決策邊界(linear decision boundaries)和非線性決策邊界(non-linear decision boundaries)彪蓬。注意:決策邊界是假設(shè)函數(shù)的屬性,由參數(shù)決定捺萌,而不是由數(shù)據(jù)集的特征決定档冬。
學(xué)習(xí)感悟:
本節(jié)的難點在于決策邊界可視化那段,涉及的基礎(chǔ)知識和高階知識特別多桃纯,查閱了相關(guān)資料酷誓,悶頭學(xué)了一天,還是沒有把握說自己已經(jīng)掌握了态坦。Anyway, 等以后有了知識積累再回頭來學(xué)吧盐数!
參考資料
CSDN論壇博主 Broke_Leaf:決策邊界繪制和plt.contourf函數(shù)講解
CSDN論壇博主 新之: 機(jī)器學(xué)習(xí)06-邏輯回歸-分類與決策邊界
知乎用戶 觸摸壹縷陽光 : Python-Numpy模塊Meshgrid函數(shù)