scikit-learn的基本用法(五)——交叉验证1

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要介绍scikit-learn中的交叉验证。通过交叉验证来选取KNN算法中的K值。

  • Demo 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import numpy as np
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import cross_val_score

# 加载iris数据集
iris = datasets.load_iris()
# 读取特征
X = iris.data
# 读取分类标签
y = iris.target
# 定义分类器
knn = KNeighborsClassifier(n_neighbors = 5)
# 进行交叉验证数据评估, 数据分为5部分, 每次用一部分作为测试集
scores = cross_val_score(knn, X, y, cv = 5, scoring = 'accuracy')
# 输出5次交叉验证的准确率
print scores
  • 结果
1
[ 0.96666667  1.          0.93333333  0.96666667  1.        ]
  • Demo 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import cross_val_score

# 确定knn中k的取值

# 加载iris数据集
iris = datasets.load_iris()
# 读取特征
X = iris.data
# 读取分类标签
y = iris.target
# 定义knn中k的取值, 0-10
k_range = range(1, 30)
# 保存k对应的准确率
k_scores = []
# 计算每个k取值对应的准确率
for k in k_range:
# 获得knn分类器
knn = KNeighborsClassifier(n_neighbors = k)
# 对数据进行交叉验证求准确率
scores = cross_val_score(knn, X, y, cv = 10, scoring = 'accuracy')
# 保存交叉验证结果的准确率均值
k_scores.append(scores.mean())

# 绘制k取不同值时的准确率变化图像
plt.plot(k_range, k_scores)
plt.xlabel('K Value in KNN')
plt.ylabel('Cross-Validation Mean Accuracy')
plt.show()
  • 结果

image

参考资料

  1. https://www.youtube.com/user/MorvanZhou
如果有收获,可以请我喝杯咖啡!