1. 程式人生 > >sklearn k最鄰近演算法

sklearn k最鄰近演算法

1、介紹

k最鄰近演算法可以說是一個非常經典而且原理十分容易理解的演算法,可以應用於分類和聚合。

優點

         1、簡單,易於理解,易於實現,無需估計引數,無需訓練;

         2、適合對稀有事件進行分類;

         3、特別適合於多分類問題(multi-modal,物件具有多個類別標籤), kNN比SVM的表現要好;

缺點

         1、對規模超大的資料集擬合時間較長,對高維資料擬合欠佳,對稀疏資料集束手無策

         2、當樣本不平衡時,如一個類的樣本容量很大,而其他類樣本容量很小時,有可能導致當輸入一個新樣本時,該類樣本並不接近目標樣本

2、程式碼實際應用

  • 分類
from sklearn.datasets import make_blobs
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np

X, y = make_blobs(n_samples=500, centers=5, random_state=8)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y_train))])

clf = KNeighborsClassifier()
clf.fit(X_train, y_train)

X_min, X_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(X_min, X_max, .02), np.arange(y_min, y_max, .02))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.pcolormesh(xx, yy, Z, cmap=plt.cm.spring)

plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cmap)
# for idx, cl in enumerate(np.unique(y_train)):
#     plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cmap)
    # plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cmap, edgecolors='y', marker=markers[idx], alpha=0.8, linewidths=1)

print("模型的正確率:{:.2f}".format(clf.score(X_test, y_test)) )
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("Classifier:KNN")
plt.show()
  • 迴歸
from sklearn.datasets import make_regression
from sklearn.neighbors import KNeighborsRegressor

import matplotlib.pyplot as plt
import numpy as np

# 生成隨機迴歸資料
X, y = make_regression(n_features=1, n_informative=1, noise=50, random_state=8)

reg = KNeighborsRegressor(n_neighbors=2)
reg.fit(X, y)
# 隨機產生x的預測值,根據訓練模型來預測y的值
z = np.linspace(-3, 3, 200).reshape(-1, 1)
plt.scatter(X, y, c='orange', edgecolors='k')

plt.plot(z, reg.predict(z), c='k', linewidth=3)
print("模型評分:{:.2f}".format(reg.score(X, y)))
plt.title('KNN Regressor')
plt.show()
  • 分類實際應用:將酒分類
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np

wine_dataset = load_wine()

print(wine_dataset['data'].shape)
X_train, X_test, y_train, y_test = train_test_split(wine_dataset['data'], wine_dataset['target'], random_state=0)

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

clf = KNeighborsClassifier(n_neighbors=7)
clf.fit(X_train, y_train)

print("測試資料集得分: {:.2f}".format(clf.score(X_test, y_test)))

X_new = np.array([[13.2, 2.77, 2.51, 18.5, 96.6, 1.04, 2.55, 0.57, 1.47, 6.2, 1.05, 3.33, 820]])

prediction = clf.predict(X_new)
print("預測新紅酒的分類為:{}".format(wine_dataset['target_names'][prediction]))