Files
smartguide/service/scripts/profiling/founding_potential/src/models.py
T
2026-08-27 13:53:10 +02:00

103 lines
3.8 KiB
Python

"""Model registries: primitive baselines and stronger/state-of-the-art models."""
from catboost import CatBoostClassifier
from lightgbm import LGBMClassifier
from sklearn.dummy import DummyClassifier
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from xgboost import XGBClassifier
from . import config
def baseline_models(random_state=config.RANDOM_STATE):
"""Primitive models used as the accuracy floor.
The feature matrix mixes raw year values (~2000), log1p-scaled counts and
0/1 dummies, so the scale-sensitive models (logistic regression, Naive
Bayes, kNN) are wrapped in a `StandardScaler` pipeline -- otherwise a
feature like `first_publication_year` (~2000) would dominate distance/
gradient computations over e.g. a 0-1 dummy or a log1p count in [0, 8].
Tree-based models are scale-invariant and stay unscaled.
"""
return {
"dummy_most_frequent": DummyClassifier(strategy="most_frequent"),
"logistic_regression": make_pipeline(
StandardScaler(),
LogisticRegression(max_iter=2000, class_weight="balanced", random_state=random_state),
),
"gaussian_nb": make_pipeline(StandardScaler(), GaussianNB()),
"knn": make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=15)),
"decision_tree": DecisionTreeClassifier(
max_depth=6, min_samples_leaf=5, class_weight="balanced", random_state=random_state
),
}
def advanced_models(random_state=config.RANDOM_STATE, scale_pos_weight=1.0):
"""Stronger / state-of-the-art tabular models."""
return {
"random_forest": RandomForestClassifier(
n_estimators=400,
max_depth=12,
min_samples_leaf=2,
class_weight="balanced",
random_state=random_state,
n_jobs=-1,
),
"svm_rbf": make_pipeline(
StandardScaler(),
SVC(kernel="rbf", C=2.0, class_weight="balanced", probability=True, random_state=random_state),
),
"xgboost": XGBClassifier(
n_estimators=400,
max_depth=5,
learning_rate=0.05,
subsample=0.8,
colsample_bytree=0.8,
objective="binary:logistic",
eval_metric="logloss",
scale_pos_weight=scale_pos_weight,
random_state=random_state,
n_jobs=-1,
),
"lightgbm": LGBMClassifier(
n_estimators=400,
num_leaves=31,
learning_rate=0.05,
class_weight="balanced",
random_state=random_state,
n_jobs=-1,
verbose=-1,
),
"catboost": CatBoostClassifier(
iterations=400,
depth=6,
learning_rate=0.05,
auto_class_weights="Balanced",
random_seed=random_state,
verbose=False,
),
}
def stacking_model(random_state=config.RANDOM_STATE, scale_pos_weight=1.0):
"""Stacked ensemble of the three tree-based models with a logistic meta-learner."""
base = advanced_models(random_state=random_state, scale_pos_weight=scale_pos_weight)
estimators = [
("random_forest", base["random_forest"]),
("xgboost", base["xgboost"]),
("lightgbm", base["lightgbm"]),
]
return StackingClassifier(
estimators=estimators,
final_estimator=LogisticRegression(max_iter=2000, class_weight="balanced", random_state=random_state),
stack_method="predict_proba",
n_jobs=-1,
)