File size: 1,996 Bytes
e757838 7f3c0a8 e757838 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from __future__ import annotations
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from sklearn.base import ClassifierMixin
from sklearn.pipeline import make_pipeline
from sklearn.metrics import roc_curve, auc
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import FunctionTransformer, OneHotEncoder
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, RandomTreesEmbedding
def create_and_split_dataset(n_samples: int) -> list[tuple[np.ndarray, np.array]]:
# Create Data
X, y = make_classification(n_samples=n_samples, random_state=10)
# Split Data
X_full_train, X_test, y_full_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=10
)
# Split Data for Ensemble and Linear Model
X_train_ensemble, X_train_linear, y_train_ensemble, y_train_linear = train_test_split(
X_full_train, y_full_train, test_size=0.5, random_state=10
)
return (X_train_ensemble, y_train_ensemble), (X_train_linear, y_train_linear), (X_test, y_test)
def rf_apply(X, model):
return model.apply(X)
def gbdt_apply(X, model):
return model.apply(X)[:, :, 0]
def plot_roc(X: np.ndarray, y:np.array, models: tuple[str, ClassifierMixin]):
fig = go.Figure()
fig.add_shape(
type='line', line=dict(dash='dash'),
x0=0, x1=1, y0=0, y1=1
)
for model_name, model in models:
y_score = model.predict_proba(X)[:, 1]
fpr, tpr, _ = roc_curve(y, y_score)
auc_val = auc(fpr, tpr)
name = f"{model_name} (AUC={auc_val:.4f})"
fig.add_trace(go.Scatter(x=fpr, y=tpr, name=name, mode='lines'))
fig.update_layout(
title="Model ROC Curve Comparison",
xaxis_title='False Positive Rate',
yaxis_title='True Positive Rate',
height=600
)
return fig
|