sklearn cross_validation交叉验证
程序员文章站
2022-03-22 17:40:16
...
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# cross_validation交叉验证
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.3)
# n_neightbors 综合附近5个点来考虑y的值
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train,y_train)
print(knn.score(X_test,y_test))
结果:0.9555555555555556
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# cross_validation交叉验证
iris = datasets.load_iris()
X = iris.data
y = iris.target
from sklearn.cross_validation import cross_val_score
knn = KNeighborsClassifier(n_neighbors=5)
# 使用的model是knn 但是X,y 被自动分成5组,
# 每组的test_data和train_data 是不一样的
scores = cross_val_score(knn,X,y,cv=5,scoring='accuracy')
print(scores.mean())
结果:0.9733333333333334
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# cross_validation交叉验证
iris = datasets.load_iris()
X = iris.data
y = iris.target
from sklearn.cross_validation import cross_val_score
import matplotlib.pyplot as plt
k_range = range(1,31)
k_score = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
scores = cross_val_score(knn,X,y,cv=10,scoring='accuracy')# for classification
#loss = -cross_val_score(knn,X,y,cv=10,scoring='mean_squared_error')# for regression
k_score.append(scores.mean())
plt.plot(k_range, k_score)
plt.xlabel('Value of K for KNN')
plt.ylabel('Corss-Validated Accuracy')
plt.show()
上一篇: 3-5-1K均值聚类
下一篇: PHP函数运用之返回两给定日期的天数差
推荐阅读
-
Python3.5 + sklearn利用SVM自动识别字母验证码方法示例
-
Python3.5 + sklearn利用SVM自动识别字母验证码方法示例
-
Python sklearn KFold 生成交叉验证数据集的方法
-
R语言使用boosting方法对数据分类与交叉验证
-
【模型评估】k折交叉验证和sklearn运用实战(cross validation)
-
SKLEARN--交叉验证
-
使用sklearn cross_val_score进行交叉验证
-
ImportError: cannot import name 'cross_validation' from 'sklearn'
-
交叉验证(Cross-Validation)
-
8 交叉验证 1 Cross-validation