Loading…
DS · Model evaluation · Program 15 · In the record
Model evaluation: confusion matrix, accuracy, precision, recall, F1, ROC-AUC.
1from sklearn.datasets import load_breast_cancer2from sklearn.model_selection import train_test_split3from sklearn.pipeline import make_pipeline4from sklearn.preprocessing import StandardScaler5from sklearn.linear_model import LogisticRegression6from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, roc_auc_score7 8X, y = load_breast_cancer(return_X_y=True)9X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)10 11model = make_pipeline(StandardScaler(), LogisticRegression()).fit(X_train, y_train)12y_pred = model.predict(X_test)13y_prob = model.predict_proba(X_test)[:, 1]14 15print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))16print("Accuracy:", accuracy_score(y_test, y_pred))17print("Precision:", precision_score(y_test, y_pred))18print("Recall:", recall_score(y_test, y_pred))19print("F1-Score:", f1_score(y_test, y_pred))20print("ROC-AUC:", roc_auc_score(y_test, y_prob))Confusion Matrix: [[53 1] [ 2 87]] Accuracy: 0.9790209790209791 Precision: 0.9886363636363636 Recall: 0.9775280898876404 F1-Score: 0.9830508474576272 ROC-AUC: 0.9977111943404079

Run program 15 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.