自己实现
def accuracy_score(y_true, y_predict): """计算y_true和y_predict之间的准确率""" assert len(y_true) == len(y_predict), \ "the size of y_true must be equal to the size of y_predict" return np.sum(y_true == y_predict) / len(y_true)
def score(self, X_test, y_test): """根据测试数据集 X_test 和 y_test 确定当前模型的准确度""" y_predict = self.predict(X_test) return accuracy_score(y_test, y_predict)
from sklearn.metrics import accuracy_score #测试准确度accuracy_score(y_test,Y_predict)
sklearn自带精准度
from sklearn.model_selection import train_test_splitfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn import datasetsdigits = datasets.load_digits()X = digits.datay = digits.targetX_train,X_test,y_train,y_test = train_test_split(X,y,test_size= 0.2,random_state=666)KNN_classifier = KNeighborsClassifier(n_neighbors=3)KNN_classifier.fit(X_train,y_train)KNN_classifier.score(X_test,y_test)