模型准确度测算

自己实现

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_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets

digits = datasets.load_digits()
X = digits.data
y = digits.target

X_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)

 

posted @ 2018-05-08 16:32  Erick-LONG  阅读(746)  评论(0编辑  收藏  举报