每次与分类器的精度差异?

时间:2018-05-29 02:37:30

标签: machine-learning scikit-learn artificial-intelligence classification supervised-learning

每次运行此代码时,精度都会有所不同。任何人都可以解释原因吗?我在这里错过了什么吗?在此先感谢:)

以下是我的代码:

import scipy
import numpy
from sklearn import datasets
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 = .5)

# Use a classifier of K-nearestNeibour
from sklearn.neighbors import KNeighborsClassifier
my_classifier = KNeighborsClassifier()

my_classifier.fit(X_train,y_train)
predictions = my_classifier.predict(X_test)
print(predictions)

from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,predictions))

1 个答案:

答案 0 :(得分:3)

train_test_split随机将数据拆分为训练集和测试集,因此每次运行脚本时都会得到不同的拆分。如果需要,可以将random_state参数设置为某个数字,并确保每次运行脚本时都获得相同的分割:

X_train, X_test, y_train,y_test = train_test_split(X,y, test_size = .5, random_state = 0)

每次都应该给你0.96的准确度。