-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodelos.py
More file actions
99 lines (92 loc) · 2.41 KB
/
Copy pathmodelos.py
File metadata and controls
99 lines (92 loc) · 2.41 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
""" Modelos de classificação
"""
from myutils import RavelTransformer
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_selection import chi2, SelectPercentile
from sklearn.impute import SimpleImputer
from sklearn.naive_bayes import BernoulliNB
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OrdinalEncoder
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
def clf_bnb(X, y):
""" Bernoulli naive bayes para a
variável de texto informed_purpose
"""
vectorizer = CountVectorizer(
binary=True,
strip_accents='ascii'
)
imputer_vectorizer = Pipeline([
('imputer', SimpleImputer(strategy='constant', fill_value='nulo')),
('ravel', RavelTransformer()),
('vector', vectorizer)
])
ctr = ColumnTransformer(
[('texto', imputer_vectorizer, ['informed_purpose'])]
)
selector = SelectPercentile(
score_func=chi2,
percentile=20
)
bnb = Pipeline([
('ctr', ctr),
('sel', selector),
('bnb', BernoulliNB(binarize=None))
])
bnb.fit(X, y)
return bnb
def clf_dt1(X, y):
""" Árvore de decisão contínua
"""
toimputer = [
'monthly_income',
'loan_amount',
'monthly_payment',
'collateral_debt',
'collateral_value',
]
ctr = ColumnTransformer(
[('toimputer', SimpleImputer(strategy='median'), toimputer)]
)
dt1 = Pipeline([
('ctr', ctr),
('dt1', DecisionTreeClassifier(max_depth=5))
])
dt1.fit(X, y)
return dt1
def clf_dt2(X, y):
""" Árvore de decisão categórica
"""
pass_cols = [
'id',
'age',
'zip_code',
'banking_debts',
'commercial_debts',
'auto_year',
]
toencoder = [
'auto_brand',
'informed_restriction',
'form_completed',
'channel',
'landing_page',
]
imputer_encoder = Pipeline([
('imputer', SimpleImputer(strategy='most_frequent')),
('encoder', OrdinalEncoder())
])
ctr = ColumnTransformer(
[
('pass_cols', 'passthrough', pass_cols),
('toencoder', imputer_encoder, toencoder)
]
)
dt2 = Pipeline([
('ctr', ctr),
('dt2', DecisionTreeClassifier(max_depth=6))
])
dt2.fit(X, y)
return dt2