1.训练集和测试集
在用模型进行预测或分类的时候,需要判断得到模型的好坏,就要事先将数据分为训练集合测试集,
在sklearn中直接调用train_test_split来划分数据:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
2.为了验证模型的准确率,就要用相应的指标来判断预测/分类的准确率,比如:分类准确率,召回率等,
sklearn中的准确度:
from sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)
knn_clf = KNeighborsClassifier(n_neighbors=3)
knn_clf.fit(X_train, y_train)
y_predict = knn_clf.predict(X_test)
accuracy_score(y_test, y_predict)
y_predictknn_clf.score(X_test,y_test)
3.精准率和召回率
精准率(查准率):预测值为1ÿ