KNN-K近鄰法
Day 7,8,9,10,11的任務(wù)是學(xué)習(xí)和實現(xiàn)KNN. 開始任務(wù)~
Knn.jpg
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
# Import Datasets
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)
# Separate TrainSets and TestSets
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.transform(X_test)
Step2 KNN Model
code如下:
# Step2 KNN train the Model
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5, metric="minkowski", p=2)
classifier.fit(X_train, Y_train)
Step3 Prediction
預(yù)測結(jié)果, 可以使用KNN的分類器來預(yù)測結(jié)果.
code如下:
# Step3 Predection
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)
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(' KNN(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(' KNN(Test set)')
plt. xlabel(' Age')
plt. ylabel(' Estimated Salary')
plt. legend()
plt. show()
Day_4_TestSet.png
Day4_TrainSet.png