机器学习算法(八) 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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值