Python:使用from sklearn.model_selection import train_test_split把数据划分为训练数据和测试数据

import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
import matplotlib.cm
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

dig = datasets.load_digits() #读入sklearn内置数据
print(dig.keys())

X = dig.data
y = dig.target

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
KNN = KNeighborsClassifier(n_neighbors=3)
KNN.fit(X_train,y_train)
y_predict = KNN.predict(X_test)
accuracy = sum(y_predict == y_test)/len(y_test)
print("预测结果准确度:",accuracy)
from sklearn.metrics import accuracy_score

print("sklearn自带精度accuracy_score:",accuracy_score(y_test,y_predict))


some = X[555]
print(y[555])
some1 = some.reshape(8,8)
plt.imshow(some1,cmap = matplotlib.cm.binary)
plt.show()

 

你可能感兴趣的:(Python学习)