Machine-Learning-Day-4-5-6

邏輯回歸

Day 4,5,6的任務(wù)是邏輯回歸. 開始任務(wù)~


Screen Shot 2019-01-18 at 3.42.59 PM.png
Screen Shot 2019-01-18 at 3.43.08 PM.png
Screen Shot 2019-01-18 at 3.43.19 PM.png
Step1 Data Preprocessing

首先我們import numpy, pandas, matplotlib. 使用pandas來read數(shù)據(jù)集. 使用sklearn來分配訓(xùn)練集和測試集. test_size為四分之一. 注意, 這里有必要的話, 我們需要使用特征縮放.

code如下:

# Step1 Data Preprocessing
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('../datasets/Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
Y = dataset.iloc[:,4].values
print('X')
print(X)
print('Y')
print(Y)

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
Step2 Logistic Regression Model

該項(xiàng)工作的庫將會是一個線性模型庫, 之所以被稱為線性是因?yàn)檫壿嫽貧w是一個線性分類器, 這意味著我們在二維空間中, 我們兩類用戶(購買和不購買)將被一條直線分割. 然后導(dǎo)入邏輯回歸類. 下一步我們將創(chuàng)建該類的對象, 它將作為我們訓(xùn)練集的分類器.

code如下:

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train, Y_train)
Step3 Prediction

預(yù)測結(jié)果, 可以使用邏輯回歸的線性分類器來預(yù)測結(jié)果.
code如下:

y_pred = classifier.predict(X_test)
Step4 Evaluate the Prediction

我們預(yù)測了測試集. 現(xiàn)在我們將評估邏輯回歸模型是否正確的學(xué)習(xí)和理解. 因此這個混淆矩陣將包含我們模型的正確和錯誤的預(yù)測.

code如下:

# Step4 Evaluate the Prediction
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test, y_pred)
print('cm')
print(cm)
Step5 Visualization

可視化我們的結(jié)果.

code如下:

# Step5 Visualization
from matplotlib.colors import ListedColormap
X_set,Y_set=X_train,Y_train
X1,X2=np. meshgrid(np. arange(start=X_set[:,0].min()-1, stop=X_set[:, 0].max()+1, step=0.01),
                   np. arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(),X1.max())
plt.ylim(X2.min(),X2.max())
for i,j in enumerate(np. unique(Y_set)):
    plt.scatter(X_set[Y_set==j,0],X_set[Y_set==j,1],
                c = ListedColormap(('red', 'green'))(i), label=j)

plt. title(' LOGISTIC(Training set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()

X_set,Y_set=X_test,Y_test
X1,X2=np. meshgrid(np. arange(start=X_set[:,0].min()-1, stop=X_set[:, 0].max()+1, step=0.01),
                   np. arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01))

plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(),X1.max())
plt.ylim(X2.min(),X2.max())
for i,j in enumerate(np. unique(Y_set)):
    plt.scatter(X_set[Y_set==j,0],X_set[Y_set==j,1],
                c = ListedColormap(('red', 'green'))(i), label=j)

plt. title(' LOGISTIC(Test set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()
Day_4_TestSet.png
Day4_TrainSet.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容