本文主要是介绍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 npX, 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() + 1xx, 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 KNeighborsRegressorimport 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 npwine_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]))
这篇关于sklearn k最邻近算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!