Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import matplotlib.pyplot as plt
- import seaborn as sns
- from sklearn.model_selection import train_test_split
- from sklearn.preprocessing import StandardScaler
- from sklearn.linear_model import LogisticRegression
- from sklearn.metrics import (
- confusion_matrix, classification_report,
- roc_curve, auc, precision_score, recall_score, f1_score
- )
- df = pd.read_csv('heart.csv')
- display(df)
- X = df.drop("target", axis=1)
- y = df["target"]
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
- scaler = StandardScaler()
- X_train_scaled = scaler.fit_transform(X_train)
- X_test_scaled = scaler.transform(X_test)
- model = LogisticRegression(max_iter=1000)
- model.fit(X_train_scaled, y_train)
- y_pred = model.predict(X_test_scaled)
- y_pred_proba = model.predict_proba(X_test_scaled)[:, 1]
- cm = confusion_matrix(y_test, y_pred)
- precision = precision_score(y_test, y_pred)
- recall = recall_score(y_test, y_pred)
- f1 = f1_score(y_test, y_pred)
- print("Confusion Matrix:\n", cm)
- print("\nClassification Report:\n", classification_report(y_test, y_pred))
- print(f"Precision: {precision:.3f}")
- print(f"Recall: {recall:.3f}")
- print(f"F1 Score: {f1:.3f}")
- plt.figure(figsize=(5,4))
- sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", cbar=False)
- plt.xlabel("Predicted")
- plt.ylabel("Actual")
- plt.title("Confusion Matrix")
- plt.show()
- fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
- roc_auc = auc(fpr, tpr)
- plt.figure(figsize=(6,5))
- plt.plot(fpr, tpr, color="darkorange", lw=2, label=f"ROC curve (AUC = {roc_auc:.2f})")
- plt.plot([0, 1], [0, 1], color="navy", lw=2, linestyle="--")
- plt.xlabel("False Positive Rate")
- plt.ylabel("True Positive Rate")
- plt.title("Receiver Operating Characteristic (ROC) Curve")
- plt.legend(loc="lower right")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment