交叉验证的应用cross_validation
1 用于调节参数
root@ubuntu:~/work#vi cross.py
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import cross_val_score
x=load_iris().data
y=load_iris().target
model=KNeighborsClassifier(n_neighbors=5)
scores=cross_val_score(model,x,y,cv=10,scoring='accuracy')
print scores.mean()
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"cross.py"10L, 306C written
root@ubuntu:~/work#python cross.py
/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:44:DeprecationWarning: This module was deprecated in version 0.18 in favor of themodel_selection module into which all the refactored classes and functions aremoved. Also note that the interface of the new CV iterators are different fromthat of this module. This module will be removed in 0.20.
"This module will be removed in0.20.", DeprecationWarning)
0.966666666667
说明:K近邻法默认n_neightbors=5,适当调整此参数可以提高精度,现在开始调整
root@ubuntu:~/work#vi cross.py
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import cross_val_score
x=load_iris().data
y=load_iris().target
for i in xrange(1,136):
model=KNeighborsClassifier(n_neighbors=i)
scores=cross_val_score(model,x,y,cv=10,scoring='accuracy')
print "%d\t%f" %(i,scores.mean())
~
~
~
~
~
~
~