Advertisement
brandblox

Lab_ML(10/03/25)

Mar 10th, 2025
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. # %%
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import seaborn as sns
  6. from sklearn.datasets import load_diabetes
  7. from sklearn.preprocessing import StandardScaler
  8. from sklearn.model_selection import train_test_split
  9. from sklearn.linear_model import LogisticRegression
  10. from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, auc
  11.  
  12. # %%
  13. diabetes = load_diabetes()
  14. X,y = diabetes.data, diabetes.target  
  15.  
  16. # %%
  17. y_binary = (y>np.median(y)).astype(int)
  18.  
  19. # %%
  20. X_train, X_test, y_train, y_test = train_test_split(X,y_binary, test_size=0.2,random_state=42)
  21.  
  22. # %%
  23. scalar = StandardScaler()
  24. X_train = scalar.fit_transform(X_train)
  25. X_test = scalar.transform(X_test)
  26.  
  27. # %%
  28. model = LogisticRegression()
  29. model.fit(X_train,y_train)
  30.  
  31. # %%
  32. y_pred = model.predict(X_test)
  33. accuracy = accuracy_score(y_test,y_pred)
  34. print("Accuracy: {:.2f}%".format(accuracy*100))
  35.  
  36. # %%
  37. print("Confusion Matrix: \n",confusion_matrix(y_test,y_pred))
  38. print("\n Classification Report: \n", classification_report(y_test,y_pred))
  39.  
  40. # %%
  41. plt.figure(figsize=(8,6))
  42. sns.scatterplot(x=X_test[:,2], y=X_test[:,8], hue=y_test, palette={0: 'blue', 1: 'red'}, marker='o')
  43. plt.xlabel("BMI")
  44. plt.ylabel("Age")
  45. plt.title("Logistic Regression Decision Boundary\n Accuracy: {:.2f}%".format(accuracy * 100))
  46. plt.legend(title="Diabetes", loc="upper right")
  47. plt.show()
  48.  
  49. # %%
  50. y_prob = model.predict_proba(X_test)[:, 1]
  51. fpr, tpr, thresholds = roc_curve(y_test, y_prob)
  52. roc_auc = auc(fpr, tpr)
  53.  
  54. plt.figure(figsize=(8, 6))
  55. plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC Curve (AUC = {roc_auc:.2f})')
  56. plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--', label='Random')
  57. plt.xlabel('False Positive Rate')
  58. plt.ylabel('True Positive Rate')
  59. plt.title(f'Receiver Operating Characteristic (ROC) Curve\nAccuracy: {accuracy * 100:.2f}%')
  60. plt.legend(loc='lower right')
  61. plt.show()
  62.  
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement