import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.impute import SimpleImputer
df = pd.read_csv('/content/dataset_Superstore_-163487463.csv')
df = df.dropna(subset=['Sales'])
df['Product_of_Interest'] = df['Sales'] > 500
X = df.drop(columns=['Sales', 'Product_of_Interest'])
y = df['Product_of_Interest']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
numeric_features = X_train.select_dtypes(include=[np.number]).columns.tolist()
categorical_features = X_train.select_dtypes(include=['object']).columns.tolist()
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('encoder', OneHotEncoder(handle_unknown='ignore'))
])
preprocessor = ColumnTransformer(transformers=[
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)
])
models = [
('Logistic Regression', LogisticRegression(random_state=42)),
('Random Forest', RandomForestClassifier(random_state=42)),
('SVM', SVC(probability=True, random_state=42))
]
results = []
for name, model in models:
model_pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('model', model)
])
model_pipeline.fit(X_train, y_train)
y_pred_proba = model_pipeline.predict_proba(X_test)[:, 1]
auc_roc = roc_auc_score(y_test, y_pred_proba)
results.append((name, auc_roc))
for name, auc_roc in results:
print(f"{name} - AUC ROC Score: {auc_roc}")
plt.figure(figsize=(8, 6))
for name, model in models:
model_pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('model', model)
])
model_pipeline.fit(X_train, y_train)
y_pred_proba = model_pipeline.predict_proba(X_test)[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
auc_roc = roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr, tpr, lw=2, label=f"{name} (AUC = {auc_roc:.2f})")
plt.plot([0, 1], [0, 1], color='gray', linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curves Comparison')
plt.legend(loc='lower right')
plt.grid(True)
plt.show()