-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGP1.final.py
388 lines (324 loc) · 13.5 KB
/
GP1.final.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# IE598 Machine Learning in Finance, Fall 2018
# University of Illinois at Urbana-Champaign
#
# Final Group Project
#
# Authors: Joseph Loss, Ruozhong Yang, Fengkai Xu, Biao Feng, and Yuchen Duan
#
# source code available at https://github.com/chicago-joe/Machine-Learning-in-Finance-Final-Project
# --------------------------------------------------------------------------------
# Model Outline:
# 1) Exploratory Data Analysis
# 2) Preprocessing, feature extraction, feature selection
# 3) Model fitting and evaluation, (you should fit at least 3 different machine learning models)
# 4) Hyperparameter tuning
# 5) Ensembling
# --------------------------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.base import BaseEstimator, ClassifierMixin, clone
from sklearn.ensemble import RandomForestClassifier
import six
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline, _name_estimators
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.tree import DecisionTreeClassifier
# 1) Exploratory Data Analysis
df = pd.read_excel('C:\\Users\\jloss\\PyCharmProjects\\Machine-Learning-in-Finance-Final-Project\\data\\GP1_CreditScore.xlsx', sep = ',')
df.shape
df.info()
df.head()
df.describe()
cols = ['Sales/Revenues', 'Gross Margin', 'EBITDA', 'EBITDA Margin', 'Net Income Before Extras',
'Total Debt', 'Net Debt', 'LT Debt', 'ST Debt', 'Cash', 'Free Cash Flow', 'Total Debt/EBITDA',
'Net Debt/EBITDA', 'Total MV', 'Total Debt/MV', 'Net Debt/MV', 'CFO/Debt', 'CFO',
'Interest Coverage', 'Total Liquidity', 'Current Liquidity', 'Current Liabilities',
'EPS Before Extras', 'PE', 'ROA', 'ROE', 'InvGrd']
# correlation matrix
cm = np.corrcoef(df[cols].values.T)
sns.set(font_scale = 0.5)
hm = sns.heatmap(cm,
cbar = True,
annot = True,
square = True,
fmt = '.2f',
annot_kws = { 'size':3 },
yticklabels = cols,
xticklabels = cols)
plt.savefig('correlation matrix.png',dpi=960 )
plt.show()
X = df[['Sales/Revenues', 'Gross Margin', 'EBITDA', 'EBITDA Margin', 'Net Income Before Extras',
'Total Debt', 'Net Debt', 'LT Debt', 'ST Debt', 'Cash', 'Free Cash Flow', 'Total Debt/EBITDA',
'Net Debt/EBITDA', 'Total MV', 'Total Debt/MV', 'Net Debt/MV', 'CFO/Debt', 'CFO',
'Interest Coverage', 'Total Liquidity', 'Current Liquidity', 'Current Liabilities',
'EPS Before Extras', 'PE', 'ROA', 'ROE']].values
y = df['InvGrd'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 42, stratify = y)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
# 2) Preprocessing, feature extraction, feature selection
# Select the feature(with importance)
forest = RandomForestClassifier(criterion = 'gini', n_estimators = 100, random_state = 42, n_jobs = 2)
forest.fit(X_train_std, y_train)
print(forest.feature_importances_)
print(X_train_std.shape)
model = SelectFromModel(forest, prefit = True)
X_train_std = model.transform(X_train_std)
X_test_std = model.transform(X_test_std)
print(X_test_std.shape)
print(X_train_std.shape)
# 3) Model fitting and evaluation, (you should fit at least 3 different machine learning models)
# 4) Hyperparameter tuning
# KNN
knn = KNeighborsClassifier()
params_knn = {
'n_neighbors':range(1, 101)
}
grid_knn = GridSearchCV(estimator = knn,
param_grid = params_knn,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_knn.fit(X_train_std, y_train)
best_model_knn = grid_knn.best_estimator_
print(best_model_knn.score(X_test_std, y_test))
# Random Forest
forest = RandomForestClassifier()
params_forest = {
'criterion':['gini'],
'n_estimators':range(1, 101),
'random_state':[42]
}
grid_forest = GridSearchCV(estimator = forest,
param_grid = params_forest,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_forest.fit(X_train_std, y_train)
best_model_forest = grid_forest.best_estimator_
print(best_model_forest.score(X_test_std, y_test))
# Decision Tree
tree = DecisionTreeClassifier()
params_tree = {
'criterion':['gini'],
'max_depth':range(1, 101),
'random_state':[42]
}
grid_tree = GridSearchCV(estimator = tree,
param_grid = params_tree,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_tree.fit(X_train_std, y_train)
best_model_tree = grid_tree.best_estimator_
print(best_model_tree.score(X_test_std, y_test))
# Logistic Regression
lr = LogisticRegression(max_iter = 1000, solver = 'lbfgs', multi_class = 'auto')
params_lr = {
'C':range(1, 101),
'random_state':[42]
}
grid_lr = GridSearchCV(estimator = lr,
param_grid = params_lr,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_lr.fit(X_train_std, y_train)
best_model_lr = grid_lr.best_estimator_
print(best_model_lr.score(X_test_std, y_test))
# 5) Ensembling
# Majority Vote Classifier
class MajorityVoteClassifier(BaseEstimator,
ClassifierMixin):
""" A majority vote ensemble classifier
Parameters
----------
classifiers : array-like, shape = [n_classifiers]
Different classifiers for the ensemble
vote : str, {'classlabel', 'probability'} (default='label')
If 'classlabel' the prediction is based on the argmax of
class labels. Else if 'probability', the argmax of
the sum of probabilities is used to predict the class label
(recommended for calibrated classifiers).
weights : array-like, shape = [n_classifiers], optional (default=None)
If a list of `int` or `float` values are provided, the classifiers
are weighted by importance; Uses uniform weights if `weights=None`.
"""
def __init__(self, classifiers, vote = 'classlabel', weights = None):
self.classifiers = classifiers
self.named_classifiers = { key:value for key, value
in _name_estimators(classifiers) }
self.vote = vote
self.weights = weights
def fit(self, X, y):
""" Fit classifiers.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Matrix of training samples.
y : array-like, shape = [n_samples]
Vector of target class labels.
Returns
-------
self : object
"""
if self.vote not in ('probability', 'classlabel'):
raise ValueError("vote must be 'probability' or 'classlabel'"
"; got (vote=%r)"
% self.vote)
if self.weights and len(self.weights) != len(self.classifiers):
raise ValueError('Number of classifiers and weights must be equal'
'; got %d weights, %d classifiers'
% (len(self.weights), len(self.classifiers)))
# Use LabelEncoder to ensure class labels start with 0, which
# is important for np.argmax call in self.predict
self.lablenc_ = LabelEncoder()
self.lablenc_.fit(y)
self.classes_ = self.lablenc_.classes_
self.classifiers_ = []
for clf in self.classifiers:
fitted_clf = clone(clf).fit(X, self.lablenc_.transform(y))
self.classifiers_.append(fitted_clf)
return self
def predict(self, X):
""" Predict class labels for X.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Matrix of training samples.
Returns
----------
maj_vote : array-like, shape = [n_samples]
Predicted class labels.
"""
if self.vote == 'probability':
maj_vote = np.argmax(self.predict_proba(X), axis = 1)
else: # 'classlabel' vote
# Collect results from clf.predict calls
predictions = np.asarray([clf.predict(X)
for clf in self.classifiers_]).T
maj_vote = np.apply_along_axis(
lambda x:
np.argmax(np.bincount(x,
weights = self.weights)),
axis = 1,
arr = predictions)
maj_vote = self.lablenc_.inverse_transform(maj_vote)
return maj_vote
def predict_proba(self, X):
""" Predict class probabilities for X.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
----------
avg_proba : array-like, shape = [n_samples, n_classes]
Weighted average probability for each class per sample.
"""
probas = np.asarray([clf.predict_proba(X)
for clf in self.classifiers_])
avg_proba = np.average(probas, axis = 0, weights = self.weights)
return avg_proba
def get_params(self, deep = True):
""" Get classifier parameter names for GridSearch"""
if not deep:
return super(MajorityVoteClassifier, self).get_params(deep = False)
else:
out = self.named_classifiers.copy()
for name, step in six.iteritems(self.named_classifiers):
for key, value in six.iteritems(step.get_params(deep = True)):
out['%s__%s' % (name, key)] = value
return out
# Ensembling
clf1 = grid_knn.best_estimator_
clf2 = grid_forest.best_estimator_
clf3 = grid_tree.best_estimator_
pipe1 = Pipeline([['sc', StandardScaler()], ['clf', clf1]])
pipe3 = Pipeline([['sc', StandardScaler()], ['clf', clf3]])
clf_labels = ['KNN', 'RandomForest', 'Decision tree']
print('10-fold cross validation:\n')
mv_clf = MajorityVoteClassifier(classifiers = [pipe1, clf2, pipe3])
clf_labels += ['Majority voting']
all_clf = [pipe1, clf2, pipe3, mv_clf]
for clf, label in zip(all_clf, clf_labels):
scores = cross_val_score(estimator = clf, X = X_train_std, y = y_train, cv = 10, scoring = 'roc_auc')
print("ROC AUC: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))
# muti
X = df[['Sales/Revenues', 'Gross Margin', 'EBITDA', 'EBITDA Margin', 'Net Income Before Extras',
'Total Debt', 'Net Debt', 'LT Debt', 'ST Debt', 'Cash', 'Free Cash Flow', 'Total Debt/EBITDA',
'Net Debt/EBITDA', 'Total MV', 'Total Debt/MV', 'Net Debt/MV', 'CFO/Debt', 'CFO', 'Interest Coverage',
'Total Liquidity', 'Current Liquidity', 'Current Liabilities', 'EPS Before Extras', 'PE', 'ROA', 'ROE']].values
y = df['Class'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 42, stratify = y)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
# KNN
knn = KNeighborsClassifier()
params_knn = {
'n_neighbors':range(1, 101)
}
grid_knn = GridSearchCV(estimator = knn,
param_grid = params_knn,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_knn.fit(X_train_std, y_train)
best_model_knn_muti = grid_knn.best_estimator_
print('muti=' + str(best_model_knn_muti.score(X_test_std, y_test)))
# RandomForest
forest = RandomForestClassifier()
params_forest = {
'criterion':['gini'],
'n_estimators':range(1, 101),
'random_state':[42]
}
grid_forest = GridSearchCV(estimator = forest,
param_grid = params_forest,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_forest.fit(X_train_std, y_train)
best_model_forest_muti = grid_forest.best_estimator_
print('muti=' + str(best_model_forest_muti.score(X_test_std, y_test)))
# DecisionTree
tree = DecisionTreeClassifier()
params_tree = {
'criterion':['gini'],
'max_depth':range(1, 101),
'random_state':[42]
}
grid_tree = GridSearchCV(estimator = tree,
param_grid = params_tree,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_tree.fit(X_train_std, y_train)
best_model_tree_muti = grid_tree.best_estimator_
print('muti=' + str(best_model_tree_muti.score(X_test_std, y_test)))
# logistic Regression
lr = LogisticRegression(max_iter = 1000, solver = 'lbfgs', multi_class = 'auto')
params_lr = {
'C':range(1, 101),
'random_state':[42]
}
grid_lr = GridSearchCV(estimator = lr,
param_grid = params_lr,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_lr.fit(X_train_std, y_train)
best_model_lr_muti = grid_lr.best_estimator_
print('muti=' + str(best_model_lr_muti.score(X_test_std, y_test)))
# This part select the feature use for fitting(but seem to make no sense)
# If you want to use it, run it before the StandardScaler after the model