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 LinearRegression, LogisticRegression
- from sklearn.naive_bayes import GaussianNB
- from sklearn.neighbors import KNeighborsClassifier
- from sklearn.svm import SVC
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.cluster import KMeans
- 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():
- # Load and preprocess the diabetes dataset
- 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):
- # Split the dataset into features and target
- 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):
- # Standardize the features
- 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 select_model(model_type):
- # Select and return the model based on the type
- if model_type == "linear_regression":
- return LinearRegression()
- elif model_type == "logistic_regression":
- return LogisticRegression()
- elif model_type == "decision_tree":
- return DecisionTreeClassifier()
- elif model_type == "random_forest":
- return RandomForestClassifier()
- elif model_type == "knn":
- return KNeighborsClassifier()
- elif model_type == "naive_bayes":
- return GaussianNB()
- elif model_type == "svm":
- return SVC(probability=True)
- elif model_type == "k_means":
- return KMeans(n_clusters=2) # For binary clustering, modify as needed
- def train_model(model, X_train, y_train):
- # Train the selected model
- model.fit(X_train, y_train)
- def make_predictions(model, X_test):
- # Make predictions using the trained model
- return model.predict(X_test)
- def evaluate_model(model, X_test, y_test, y_pred):
- # Evaluate the model using accuracy, precision, recall, and F1 score
- if hasattr(model, "predict_proba"): # If model supports probability prediction
- y_probs = model.predict_proba(X_test)[:, 1] # Get probabilities for ROC Curve
- 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()
- 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 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)
- # Example usage
- if __name__ == "__main__":
- target_column = "target" # Column to predict
- # Load and prepare the data
- 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)
- # Select the model type here (replace with your choice)
- model_type = "decision_tree" # Can be any model type from the available options
- # Choose model
- model = select_model(model_type)
- # Train model
- train_model(model, X_train, y_train)
- # Make predictions
- y_pred = make_predictions(model, X_test)
- # Display sample predictions
- display_predictions(X_test, y_pred)
- # Evaluate the model
- evaluate_model(model, X_test, y_test, y_pred)
- # Plot confusion matrix
- plot_confusion_matrix(y_test, y_pred)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement