This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
220 lines (186 loc) · 6.66 KB
/
model.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#Z0096
from measure import model_report
from sklearn.dummy import DummyClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
#################### Model telco_churn Data ####################
def baseline_model(X, y, strategy='most_frequent', random_state=19):
'''
Generates a baseline model using sklearn DummyClassifier strategy
default set to 'most_frequent' and random_state=19
'''
# assign baseline model and fit to data
baseline = DummyClassifier(strategy=strategy, random_state=random_state)
baseline.fit(X, y)
# assign baseline predictions
y_baseline = baseline.predict(X)
# print baseline accuracy score and first ten values for training data
print(f'''
Baseline Accuracy Score: {baseline.score(X, y):.2%}
First Ten Baseline Predictions: {y_baseline[:10]}
''')
return baseline, y_baseline
def tree_model(X, y, lite=False,
criterion='gini',
splitter='best',
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_features=None,
random_state=19,
max_leaf_nodes=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
class_weight=None,
ccp_alpha=0.0):
'''
Creates sklearn DecisionTree model with all default hyperparameters
and random_state=19, then returns the model fit to dataset and its
predictions
'''
# assign model and fit to data
model = DecisionTreeClassifier(
criterion=criterion,
splitter=splitter,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
min_weight_fraction_leaf=min_weight_fraction_leaf,
max_features=max_features,
random_state=random_state,
max_leaf_nodes=max_leaf_nodes,
min_impurity_decrease=min_impurity_decrease,
min_impurity_split=min_impurity_split,
class_weight=class_weight,
ccp_alpha=ccp_alpha)
model.fit(X, y)
# assign model predictions
y_pred = model.predict(X)
model_report(y, y_pred, lite=lite)
return model, y_pred
def forest_model(X, y, lite=False,
n_estimators=100,
criterion='gini',
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_features='auto',
max_leaf_nodes=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
bootstrap=True,
oob_score=False,
n_jobs=None,
random_state=19,
verbose=0,
warm_start=False,
class_weight=None,
ccp_alpha=0.0,
max_samples=None):
'''
Creates sklearn RandomForestClassifier model with all default
hyperparameters and random_state=19, then returns the model fit to
dataset and its predictions
'''
# assign model and fit to data
model = RandomForestClassifier(
n_estimators=n_estimators,
criterion=criterion,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
min_weight_fraction_leaf=min_weight_fraction_leaf,
max_features=max_features,
max_leaf_nodes=max_leaf_nodes,
min_impurity_decrease=min_impurity_decrease,
min_impurity_split=min_impurity_split,
bootstrap=bootstrap,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
warm_start=warm_start,
class_weight=class_weight,
ccp_alpha=ccp_alpha,
max_samples=max_samples)
model.fit(X, y)
# assign model predictions
y_pred = model.predict(X)
model_report(y, y_pred, lite=lite)
return model, y_pred
def knn_model(X, y, lite=False,
n_neighbors=5,
weights='uniform',
algorithm='auto',
leaf_size=30,
p=2,
metric='minkowski',
metric_params=None,
n_jobs=None):
'''
Creates sklearn KNeighborsClassifier model with all default
hyperparameters, then returns the model fit to dataset and its
predictions
'''
# assign model and fit to data
model = KNeighborsClassifier(
n_neighbors=n_neighbors,
weights=weights,
algorithm=algorithm,
leaf_size=leaf_size,
p=p,
metric=metric,
metric_params=metric_params,
n_jobs=n_jobs)
model.fit(X, y)
# assign model predictions
y_pred = model.predict(X)
model_report(y, y_pred, lite=lite)
return model, y_pred
def logit_model(X, y, lite=False,
penalty='l2',
dual=False,
tol=0.0001,
C=1.0,
fit_intercept=True,
intercept_scaling=1,
class_weight=None,
random_state=19,
solver='lbfgs',
max_iter=100,
multi_class='auto',
verbose=0,
warm_start=False,
n_jobs=None,
l1_ratio=None):
'''
Creates sklearn LogicRegression model with all default
hyperparameters and random_state=19, then returns the model fit to
dataset and its predictions
'''
# assign model and fit to data
model = LogisticRegression(
penalty=penalty,
dual=dual,
tol=tol,
C=C,
fit_intercept=fit_intercept,
intercept_scaling=intercept_scaling,
class_weight=class_weight,
random_state=random_state,
solver=solver,
max_iter=max_iter,
multi_class=multi_class,
verbose=verbose,
warm_start=warm_start,
n_jobs=n_jobs,
l1_ratio=l1_ratio)
model.fit(X, y)
# assign model predictions
y_pred = model.predict(X)
model_report(y, y_pred, lite=lite)
return model, y_pred