from
sklearn.model_selection
import
cross_val_predict, cross_val_score
from
sklearn.metrics
import
accuracy_score, classification_report
from
sklearn.metrics
import
confusion_matrix
def
print_score(clf, X_train, y_train, X_test, y_test, train
=
True
):
if
train:
print
("Train Result:")
print
("
-
-
-
-
-
-
-
-
-
-
-
-
")
print
("Classification Report: \n {}\n".
format
(classification_report(
y_train, clf.predict(X_train))))
print
("Confusion Matrix: \n {}\n".
format
(confusion_matrix(
y_train, clf.predict(X_train))))
res
=
cross_val_score(clf, X_train, y_train,
cv
=
10
, scoring
=
'accuracy'
)
print
("Average Accuracy: \t {
0
:.
4f
}".
format
(np.mean(res)))
print
("Accuracy SD: \t\t {
0
:.
4f
}".
format
(np.std(res)))
print
("accuracy score: {
0
:.
4f
}\n".
format
(accuracy_score(
y_train, clf.predict(X_train))))
print
("
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
")
elif
train
=
=
False
:
print
("Test Result:")
print
("
-
-
-
-
-
-
-
-
-
-
-
")
print
("Classification Report: \n {}\n".
format
(
classification_report(y_test, clf.predict(X_test))))
print
("Confusion Matrix: \n {}\n".
format
(
confusion_matrix(y_test, clf.predict(X_test))))
print
("accuracy score: {
0
:.
4f
}\n".
format
(
accuracy_score(y_test, clf.predict(X_test))))
print
("
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
")
knn
=
KNeighborsClassifier(n_neighbors
=
7
)
knn.fit(X_train, y_train)
print_score(knn, X_train, y_train, X_test, y_test, train
=
True
)
print_score(knn, X_train, y_train, X_test, y_test, train
=
False
)