-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodels.py
More file actions
55 lines (42 loc) · 1.82 KB
/
models.py
File metadata and controls
55 lines (42 loc) · 1.82 KB
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
import numpy as np
import streamlit as st
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin, ClassifierMixin
from sklearn.linear_model import LogisticRegression
from category_encoders import WOEEncoder
from beta_calibration import BetaCalibration
class ModelTypes:
LOG_REG = 'Regressão Logística'
LGBM = 'LightGBM'
ANN = 'Rede Neural'
# KNN = 'kNN'
XGB = 'XGBoost'
class AutoWOEEncoder(BaseEstimator, TransformerMixin):
def __init__(self):
self.woe_encoder = None
self.categorical_features = None
def fit(self, X, y=None):
# Identify categorical columns
self.categorical_features = X.select_dtypes(include=['object', 'category']).columns.tolist()
self.woe_encoder = WOEEncoder(cols=self.categorical_features)
self.woe_encoder.fit(X, y)
return self
def transform(self, X):
return self.woe_encoder.transform(X)
class BetaCalibratedClassifier(BaseEstimator, ClassifierMixin):
def __init__(self, base_estimator=None):
self.base_estimator = base_estimator or LogisticRegression()
self.beta_calibrator = BetaCalibration()
def fit(self, X, y):
self.base_estimator.fit(X, y)
probs = self.base_estimator.predict_proba(X)[:, 1]
self.beta_calibrator.fit(probs, y) # uses train set predictions to calibrate -- not ideal
return self
def predict(self, X):
probs = self.base_estimator.predict_proba(X)[:, 1]
calibrated_probs = self.beta_calibrator.predict(probs)
return calibrated_probs >= 0.5
def predict_proba(self, X):
probs = self.base_estimator.predict_proba(X)[:, 1]
calibrated_probs = self.beta_calibrator.predict(probs)
return np.vstack((1-calibrated_probs, calibrated_probs)).T