首页 > 分享 > 机器学习算法(八) k近邻算法

机器学习算法(八) k近邻算法

对两组数据进行分类和可视化

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.colors import ListedColormap

from sklearn.neighbors import KNeighborsClassifier

from sklearn import datasets

# data prepare

iris = datasets.load_iris()

X = iris.data[:, :2] # The first two column

y = iris.target

# train & visual

k_list = [1, 3, 5, 8, 10, 15]

h = .02

cmap_light = ListedColormap(['orange', 'cyan', 'cornflowerblue'])

cmap_bold = ListedColormap(['darkorange', 'c', 'darkblue'])

plt.figure(figsize = (15, 14))

for ind, k in enumerate(k_list): # ind array label, k value

clf = KNeighborsClassifier(k)

clf.fit(X, y)

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1

y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

z = z.reshape(xx.shape)

plt.subplot(321+ind)

# shading='auto'、'nearest'或'gouraud' after 3.3 default 'flat' is not recommand

plt.pcolormesh(xx, yy, z, shading='nearest', cmap = cmap_light)

plt.scatter(X[:, 0], X[:, 1], c =y, cmap = cmap_bold, edgecolor = 'k', s = 20)

plt.xlim(xx.min(), xx.max())

plt.ylim(yy.min(), yy.max())

plt.title('3-class classification(k = %i)' %k)

plt.show()

import numpy as np

from sklearn import datasets

from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import train_test_split

iris = datasets.load_iris()

X = iris.data

y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

clf = KNeighborsClassifier(n_neighbors = 5, p =2, metric = 'minkowski') # minkowski matrices

clf.fit(X_train, y_train)

X_pred = clf.predict(X_test)

acc = sum(X_pred == y_test) / X_pred.shape[0]

print('predict acc: %.3f' % acc)

相关知识

K近邻算法和鸢尾花问题
机器学习算法
K近邻学习
【机器学习】k
机器学习算法应用于智能农业植物病虫害检测.pptx
k近邻法
基于机器学习的鸢尾花数据集的三分类算法的实现 C++
【10月23日】机器学习实战(一)KNN算法:手写识别系统
【机器学习】KNN算法实现鸢尾花分类
机器学习之KNN算法

网址: 机器学习算法(八) k近邻算法 https://m.huajiangbk.com/newsview1190461.html

所属分类:花卉
上一篇: 洛基山羊,攀爬能力超强,90度绝
下一篇: 凌霄花攀爬能力强,养上一棵,墙壁