-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankruptcy (15).py
524 lines (357 loc) · 16.9 KB
/
bankruptcy (15).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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# -*- coding: utf-8 -*-
"""bankruptcy.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1WAf9nRlRHjU5zhYlm-IF-CfppRjedjKK
#Bankruptcy prediction
## Overview
In this notebook we explore bankruptcy prediction using machine learning techniques. It employs various methods such as logistic regression and random forest classifier to predict bankruptcy based on financial data.
## Dataset
The dataset used in this analysis is sourced from Kaggle: [Company Bankruptcy Prediction](https://www.kaggle.com/datasets/fedesoriano/company-bankruptcy-prediction). It comprises various financial attributes of companies along with a binary target variable indicating bankruptcy.
"""
# Imports
import numpy as np
import pandas as pd
"""## Dataset"""
!wget -O data.csv 'https://drive.google.com/uc?export=download&id=1-qkqMYZ5O-ZkpEkwbY1Fwexiz6Y4KYc0'
dataset = pd.read_csv('data.csv')
dataset.head()
from scipy.stats import zscore
features_only = dataset.iloc[:, 1:]
z_scores = np.abs(zscore(features_only))
threshold = 3
outlier_mask = (z_scores > threshold).any(axis=1)
clean_features = features_only[~outlier_mask]
clean_target = dataset.iloc[:, 0][~outlier_mask] # Align the target with the cleaned features
clean_dataset = pd.concat([clean_target, clean_features], axis=1)
print(f"Original dataset shape: {dataset.shape}")
print(f"Cleaned dataset shape: {clean_dataset.shape}")
#dataset = clean_dataset
X = dataset.iloc[:, 1:].values
y = dataset.iloc[:, 0].values
y_series = pd.Series(y)
print("Class Balance:")
print(y_series.value_counts())
"""## Correlation HeatMap"""
import seaborn as sns
import matplotlib.pyplot as plt
correlation_matrix = dataset.iloc[:, 1:].corr()
# Plot the heatmap
plt.figure(figsize=(60, 60))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Correlation Matrix Heatmap')
plt.show()
"""## Univariate Selection
Univariate selection methods evaluate each feature individually against the target variable. One such method is selecting features based on the p-value from an ANOVA F-value.
"""
from sklearn.feature_selection import SelectKBest, f_classif
column_names = dataset.columns[1:]
selector = SelectKBest(f_classif, k=60) # Adjust 'k'
selector.fit(X, y)
mask = selector.get_support()
new_features = []
for i, bool in enumerate(mask):
if bool:
new_features.append(column_names[i])
print(new_features)
"""## Train / Test Split"""
from sklearn.model_selection import train_test_split
X_df = pd.DataFrame(X, columns=column_names)
selected_X_df = X_df[new_features] #Filter attributes
selected_X = selected_X_df.values
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 1)
X_train, X_test, y_train, y_test = train_test_split(selected_X, y, test_size = 0.25, random_state = 1)
print(X_train)
print(X_test)
print(y_train)
print(y_test)
"""## Feature Scaling"""
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
print(X_test)
"""# Logistic Regresion
### Inbalanced prediction
"""
from sklearn.linear_model import LogisticRegression
LRclassifier = LogisticRegression(max_iter=1000)
LRclassifier.fit(X_train, y_train)
# Print dataset balance
y_series = pd.Series(y_train)
class_counts = y_series.value_counts()
total_instances = len(y_train)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
y_pred = LRclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred))
"""### Class weights"""
# Print dataset balance
y_series = pd.Series(y_train)
class_counts = y_series.value_counts()
total_instances = len(y_train)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
from sklearn.linear_model import LogisticRegression
LRclassifier = LogisticRegression(max_iter=1000, class_weight={0: 0.4, 1: 1})
LRclassifier.fit(X_train, y_train)
y_pred = LRclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred))
"""### Synthetic Minority Over-sampling Technique (SMOTE)
"""
from imblearn.over_sampling import SMOTE
smote = SMOTE(random_state=1)
X_train_smote, y_train_smote = smote.fit_resample(X_train, y_train)
# Print dataset balance
y_series = pd.Series(y_train_smote)
class_counts = y_series.value_counts()
total_instances = len(y_train_smote)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
from sklearn.linear_model import LogisticRegression
LRclassifier = LogisticRegression(max_iter=1000)
LRclassifier.fit(X_train_smote, y_train_smote)
y_pred = LRclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred))
"""### Synthetic Minority Over-sampling Technique (SMOTE) 70:30
"""
from imblearn.over_sampling import SMOTE
smote = SMOTE(random_state=1, sampling_strategy=0.3)
X_train_smote, y_train_smote = smote.fit_resample(X_train, y_train)
# Print dataset balance
y_series = pd.Series(y_train_smote)
class_counts = y_series.value_counts()
total_instances = len(y_train_smote)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
from sklearn.linear_model import LogisticRegression
LRclassifier = LogisticRegression(max_iter=1000)
LRclassifier.fit(X_train_smote, y_train_smote)
y_pred = LRclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred))
"""### Combination of oversampling and undersampling"""
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
smote = SMOTE(random_state=1, sampling_strategy=0.3)
rus = RandomUnderSampler(random_state=1, sampling_strategy=0.4)
# Combine oversampling and undersampling
X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train)
X_train_resampled, y_train_resampled = rus.fit_resample(X_train_resampled, y_train_resampled)
# Print dataset balance
y_series = pd.Series(y_train_resampled)
class_counts = y_series.value_counts()
total_instances = len(y_train_resampled)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
# Train the logistic regression model on the balanced dataset
LRclassifier = LogisticRegression(max_iter=1000)
LRclassifier.fit(X_train_resampled, y_train_resampled)
# Predict on the test set
y_pred = LRclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred))
"""### Cross validation"""
from sklearn.model_selection import cross_val_score, StratifiedKFold
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
scores = cross_val_score(LRclassifier, X_train_resampled, y_train_resampled, cv=kf, scoring='accuracy')
print("Cross-validated scores:", scores)
print("Average score:", scores.mean())
scores_f1 = cross_val_score(LRclassifier, X_train_resampled, y_train_resampled, cv=kf, scoring='f1')
print("Cross-validated F1 scores:", scores_f1)
print("Average F1 score:", scores_f1.mean())
"""# Random Forest Classifier
### Using class weights
"""
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
import pandas as pd
# Train the Random Forest model directly on the original training data
RFclassifier = RandomForestClassifier(n_estimators=100, random_state=1, class_weight={0: 0.6, 1: 1.0})
RFclassifier.fit(X_train, y_train)
y_pred_RF = RFclassifier.predict(X_test)
# Print dataset balance
y_series = pd.Series(y_train)
class_counts = y_series.value_counts()
total_instances = len(y_train)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
# Print Random Forest results
print("\nRandom Forest Results:")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_RF))
print("\nClassification Report:\n", classification_report(y_test, y_pred_RF))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred_RF))
"""### Using SMOTE and RandomUnderSampler
"""
from sklearn.ensemble import RandomForestClassifier
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
smote = SMOTE(random_state=1, sampling_strategy=0.3)
rus = RandomUnderSampler(random_state=1, sampling_strategy=0.4)
# Combine oversampling and undersampling
X_train_forest, y_train_forest = smote.fit_resample(X_train, y_train)
X_train_forest, y_train_forest = rus.fit_resample(X_train_forest, y_train_forest)
# Print dataset balance
y_series = pd.Series(y_train_forest)
class_counts = y_series.value_counts()
total_instances = len(y_train_forest)
class_percentages = (class_counts / total_instances) * 100
print("Class Counts:")
print(class_counts)
print("\nClass Percentages:")
print(class_percentages)
RFclassifier = RandomForestClassifier(n_estimators=100, random_state=1, class_weight={0: 0.6, 1: 1.0})
RFclassifier.fit(X_train_forest, y_train_forest)
y_pred_RF = RFclassifier.predict(X_test)
print("\nRandom Forest Results:")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_RF))
print("\nClassification Report:\n", classification_report(y_test, y_pred_RF))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred_RF))
"""## Cross validation"""
from sklearn.model_selection import cross_val_score, StratifiedKFold
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
scores = cross_val_score(RFclassifier, X_train_forest, y_train_forest, cv=kf, scoring='accuracy')
print("Cross-validated scores:", scores)
print("Average score:", scores.mean())
scores_f1 = cross_val_score(RFclassifier, X_train_forest, y_train_forest, cv=kf, scoring='f1')
print("Cross-validated F1 scores:", scores_f1)
print("Average F1 score:", scores_f1.mean())
"""## k-Nearest Neighbors (k-NN)
"""
from sklearn.neighbors import KNeighborsClassifier
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
smote = SMOTE(random_state=1, sampling_strategy=0.3)
rus = RandomUnderSampler(random_state=1, sampling_strategy=0.4)
# Combine oversampling and undersampling
X_train_knn, y_train_knn = smote.fit_resample(X_train, y_train)
X_train_knn, y_train_knn = rus.fit_resample(X_train_knn, y_train_knn)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_knn, y_train_knn)
y_pred_knn = knn.predict(X_test)
print("\nk-NN Results:")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_knn))
print("\nClassification Report:\n", classification_report(y_test, y_pred_knn))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred_knn))
"""## Cross-Validation k-NN"""
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score, StratifiedKFold
knn = KNeighborsClassifier(n_neighbors=5)
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
knn_scores = cross_val_score(knn, X_train_knn, y_train_knn, cv=kf, scoring='accuracy')
knn_f1_scores = cross_val_score(knn, X_train_knn, y_train_knn, cv=kf, scoring='f1')
print("k-NN Cross-Validated Scores:", knn_scores)
print("k-NN Average Score:", knn_scores.mean())
print("k-NN Cross-Validated F1 Scores:", knn_f1_scores)
print("k-NN Average F1 Score:", knn_f1_scores.mean())
"""## Support Vector Machine (SVM)
"""
from sklearn.svm import SVC
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
# Combine oversampling and undersampling
X_train_svc, y_train_svc = smote.fit_resample(X_train, y_train)
X_train_svc, y_train_svc = rus.fit_resample(X_train_svc, y_train_svc)
# SVM with linear kernel
svm_classifier = SVC(kernel='linear', C=1.0, random_state=1)
svm_classifier.fit(X_train_svc, y_train_svc)
y_pred_svm = svm_classifier.predict(X_test)
print("\nSVM Results:")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_svm))
print("\nClassification Report:\n", classification_report(y_test, y_pred_svm))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred_svm))
"""## Cross-Validation SVM"""
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score, StratifiedKFold
svm_classifier = SVC(kernel='linear', C=1.0, random_state=1)
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
svm_scores = cross_val_score(svm_classifier, X_train_svc, y_train_svc, cv=kf, scoring='accuracy')
svm_f1_scores = cross_val_score(svm_classifier, X_train_svc, y_train_svc, cv=kf, scoring='f1')
print("SVM Cross-Validated Scores:", svm_scores)
print("SVM Average Score:", svm_scores.mean())
print("SVM Cross-Validated F1 Scores:", svm_f1_scores)
print("SVM Average F1 Score:", svm_f1_scores.mean())
"""### CatBoost"""
# Install CatBoost
!pip install catboost
from catboost import CatBoostClassifier
# Combine oversampling and undersampling
X_train_cat, y_train_cat = smote.fit_resample(X_train, y_train)
X_train_cat, y_train_cat = rus.fit_resample(X_train_cat, y_train_cat)
catboost_model = CatBoostClassifier(iterations=1000, learning_rate=0.1, depth=6, random_seed=1, verbose=False)
catboost_model.fit(X_train_cat, y_train_cat)
y_pred_catboost = catboost_model.predict(X_test)
print("\nCatBoost Results:")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_catboost))
print("\nClassification Report:\n", classification_report(y_test, y_pred_catboost))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred_catboost))
"""## Cross-Validation CatBoost"""
from sklearn.model_selection import cross_val_score, StratifiedKFold
from catboost import CatBoostClassifier
catboost_model = CatBoostClassifier(iterations=1000, learning_rate=0.1, depth=6, random_seed=1, verbose=False)
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
# Perform cross-validation for CatBoost
catboost_scores = cross_val_score(catboost_model, X_train_cat, y_train_cat, cv=kf, scoring='accuracy')
catboost_f1_scores = cross_val_score(catboost_model, X_train_cat, y_train_cat, cv=kf, scoring='f1')
print("CatBoost Cross-Validated Scores:", catboost_scores)
print("CatBoost Average Score:", catboost_scores.mean())
print("CatBoost Cross-Validated F1 Scores:", catboost_f1_scores)
print("CatBoost Average F1 Score:", catboost_f1_scores.mean())
"""### XGBoost"""
# Install XGBoost
!pip install xgboost
from xgboost import XGBClassifier
# Combine oversampling and undersampling
X_train_xboost, y_train_xboost = smote.fit_resample(X_train, y_train)
X_train_xboost, y_train_xboost = rus.fit_resample(X_train_xboost, y_train_xboost)
xgboost_model = XGBClassifier(n_estimators=1000, learning_rate=0.1, max_depth=6, random_state=1, use_label_encoder=False)
xgboost_model.fit(X_train_xboost, y_train_xboost)
y_pred_xgboost = xgboost_model.predict(X_test)
print("\nXGBoost Results:")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_xgboost))
print("\nClassification Report:\n", classification_report(y_test, y_pred_xgboost))
print("\nAccuracy Score:", accuracy_score(y_test, y_pred_xgboost))
"""## Cross-Validation XGBoost"""
from sklearn.model_selection import cross_val_score, StratifiedKFold
from xgboost import XGBClassifier
xgboost_model = XGBClassifier(n_estimators=1000, learning_rate=0.1, max_depth=6, random_state=1, use_label_encoder=False)
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
# Perform cross-validation for XGBoost
xgboost_scores = cross_val_score(xgboost_model, X_train_xboost, y_train_xboost, cv=kf, scoring='accuracy')
xgboost_f1_scores = cross_val_score(xgboost_model, X_train_xboost, y_train_xboost, cv=kf, scoring='f1')
print("XGBoost Cross-Validated Scores:", xgboost_scores)
print("XGBoost Average Score:", xgboost_scores.mean())
print("XGBoost Cross-Validated F1 Scores:", xgboost_f1_scores)
print("XGBoost Average F1 Score:", xgboost_f1_scores.mean())