Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import seaborn as sns
- from sklearn.model_selection import train_test_split
- from sklearn.preprocessing import StandardScaler
- from sklearn.tree import DecisionTreeClassifier
- from sklearn.linear_model import LogisticRegression
- from sklearn.metrics import precision_recall_fscore_support, confusion_matrix, accuracy_score, roc_curve, auc, classification_report
- from sklearn.datasets import load_diabetes
- def load_data():
- data = load_diabetes()
- df = pd.DataFrame(data.data, columns=data.feature_names)
- df = df.drop(columns=["s1"]) # Dropping one unnecessary column
- df['target'] = (data.target > data.target.mean()).astype(int) # Convert target to binary
- print("First 5 rows of the dataset:")
- print(df.head())
- return df
- def split_data(df, target_column):
- X = df.drop(columns=[target_column])
- y = df[target_column]
- return train_test_split(X, y, test_size=0.2, random_state=42)
- def scale_features(X_train, X_test):
- scaler = StandardScaler()
- X_train_scaled = scaler.fit_transform(X_train)
- X_test_scaled = scaler.transform(X_test)
- return X_train_scaled, X_test_scaled
- def train_decision_tree(X_train, y_train):
- # model = LogisticRegression()
- model = DecisionTreeClassifier()
- model.fit(X_train, y_train)
- return model
- def make_predictions(model, X_test):
- return model.predict(X_test)
- def display_predictions(X_test, y_pred):
- print("Sample Predictions:")
- sample_df = pd.DataFrame(X_test[:5], columns=[f'Feature_{i}' for i in range(X_test.shape[1])])
- sample_df['Predicted Target'] = y_pred[:5]
- print(sample_df)
- def evaluate_model(y_test, y_pred):
- accuracy = accuracy_score(y_test, y_pred)
- precision, recall, f1, _ = precision_recall_fscore_support(y_test, y_pred, average='binary')
- print(f"Accuracy: {accuracy:.2f}, Precision: {precision:.2f}, Recall: {recall:.2f}, F1 Score: {f1:.2f}")
- print("\nClassification Report:\n", classification_report(y_test, y_pred))
- def plot_confusion_matrix(y_test, y_pred):
- cm = confusion_matrix(y_test, y_pred)
- sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
- plt.xlabel("Predicted")
- plt.ylabel("Actual")
- plt.title("Confusion Matrix")
- plt.show()
- def plot_roc_curve(y_test, y_probs):
- fpr, tpr, _ = roc_curve(y_test, y_probs)
- roc_auc = auc(fpr, tpr)
- plt.plot(fpr, tpr, label=f'ROC Curve (AUC = {roc_auc:.2f})')
- plt.plot([0, 1], [0, 1], linestyle='--')
- plt.xlabel("False Positive Rate")
- plt.ylabel("True Positive Rate")
- plt.title("ROC Curve")
- plt.legend()
- plt.show()
- # Example usage
- if __name__ == "__main__":
- target_column = "target"
- df = load_data()
- X_train, X_test, y_train, y_test = split_data(df, target_column)
- X_train, X_test = scale_features(X_train, X_test)
- model = train_decision_tree(X_train, y_train)
- y_pred = make_predictions(model, X_test)
- display_predictions(X_test, y_pred)
- evaluate_model(y_test, y_pred)
- plot_confusion_matrix(y_test, y_pred)
- y_probs = model.predict_proba(X_test)[:, 1]
- plot_roc_curve(y_test, y_probs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement