67 KiB
67 KiB
In [1]:
import pandas as pd
random_state = 9
def get_class(row):
return 0 if row["Financial Distress"] > -0.5 else 1
df = pd.read_csv("data-distress/FinancialDistress.csv").drop(
["Company", "Time", "x80"], axis=1
)
df["Financial Distress"] = df.apply(get_class, axis=1)
df
Out[1]:
Financial Distress | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | ... | x73 | x74 | x75 | x76 | x77 | x78 | x79 | x81 | x82 | x83 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 1.2810 | 0.022934 | 0.87454 | 1.21640 | 0.060940 | 0.188270 | 0.52510 | 0.018854 | 0.182790 | ... | 36.0 | 85.437 | 27.07 | 26.102 | 16.000 | 16.0 | 0.2 | 0.060390 | 30 | 49 |
1 | 0 | 1.2700 | 0.006454 | 0.82067 | 1.00490 | -0.014080 | 0.181040 | 0.62288 | 0.006423 | 0.035991 | ... | 36.0 | 107.090 | 31.31 | 30.194 | 17.000 | 16.0 | 0.4 | 0.010636 | 31 | 50 |
2 | 0 | 1.0529 | -0.059379 | 0.92242 | 0.72926 | 0.020476 | 0.044865 | 0.43292 | -0.081423 | -0.765400 | ... | 35.0 | 120.870 | 36.07 | 35.273 | 17.000 | 15.0 | -0.2 | -0.455970 | 32 | 51 |
3 | 1 | 1.1131 | -0.015229 | 0.85888 | 0.80974 | 0.076037 | 0.091033 | 0.67546 | -0.018807 | -0.107910 | ... | 33.0 | 54.806 | 39.80 | 38.377 | 17.167 | 16.0 | 5.6 | -0.325390 | 33 | 52 |
4 | 0 | 1.0623 | 0.107020 | 0.81460 | 0.83593 | 0.199960 | 0.047800 | 0.74200 | 0.128030 | 0.577250 | ... | 36.0 | 85.437 | 27.07 | 26.102 | 16.000 | 16.0 | 0.2 | 1.251000 | 7 | 27 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
3667 | 0 | 2.2605 | 0.202890 | 0.16037 | 0.18588 | 0.175970 | 0.198400 | 2.22360 | 1.091500 | 0.241640 | ... | 22.0 | 100.000 | 100.00 | 100.000 | 17.125 | 14.5 | -7.0 | 0.436380 | 4 | 41 |
3668 | 0 | 1.9615 | 0.216440 | 0.20095 | 0.21642 | 0.203590 | 0.189870 | 1.93820 | 1.000100 | 0.270870 | ... | 28.0 | 91.500 | 130.50 | 132.400 | 20.000 | 14.5 | -16.0 | 0.438020 | 5 | 42 |
3669 | 0 | 1.7099 | 0.207970 | 0.26136 | 0.21399 | 0.193670 | 0.183890 | 1.68980 | 0.971860 | 0.281560 | ... | 32.0 | 87.100 | 175.90 | 178.100 | 20.000 | 14.5 | -20.2 | 0.482410 | 6 | 43 |
3670 | 0 | 1.5590 | 0.185450 | 0.30728 | 0.19307 | 0.172140 | 0.170680 | 1.53890 | 0.960570 | 0.267720 | ... | 30.0 | 92.900 | 203.20 | 204.500 | 22.000 | 22.0 | 6.4 | 0.500770 | 7 | 44 |
3671 | 0 | 1.6148 | 0.176760 | 0.36369 | 0.18442 | 0.169550 | 0.197860 | 1.58420 | 0.958450 | 0.277780 | ... | 29.0 | 91.700 | 227.50 | 214.500 | 21.000 | 20.5 | 8.6 | 0.611030 | 8 | 45 |
3672 rows × 83 columns
In [2]:
from collections import Counter
from src.utils import split_stratified_into_train_val_test
from imblearn.over_sampling import ADASYN
X_train, X_test, y_train, y_test = split_stratified_into_train_val_test(
df,
stratify_colname="Financial Distress",
frac_train=0.8,
frac_val=0,
frac_test=0.2,
random_state=random_state,
)
ada = ADASYN()
X_train, y_train = ada.fit_resample(X_train, y_train)
# print(f"Original dataset shape {len(df[["Financial Distress"]])}")
# X, y = reducer.fit_resample(
# df.drop(["Financial Distress"], axis=1), df[["Financial Distress"]]
# )
# print(f"Original dataset shape {len(y)}")
# X_train = pd.DataFrame(
# sc.fit_transform(X_train.values), columns=X_train.columns, index=X_train.index
# )
# X_test = pd.DataFrame(
# sc.fit_transform(X_test.values), columns=X_test.columns, index=X_test.index
# )
display(X_train.head(3))
display(y_train.head(3))
display(X_test.head(3))
display(y_test.head(3))
x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | ... | x73 | x74 | x75 | x76 | x77 | x78 | x79 | x81 | x82 | x83 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1.0902 | 0.081715 | 0.71056 | 0.93446 | 0.14445 | 0.060042 | 0.74048 | 0.087447 | 0.28232 | 0.14572 | ... | 28.0 | 79.951 | 66.12 | 59.471 | 18.0 | 12.0 | -13.4 | 1.1152 | 13 | 28 |
1 | 1.6711 | 0.445250 | 0.21104 | 0.59523 | 0.30998 | 0.133260 | 1.34060 | 0.748040 | 0.56436 | 0.48288 | ... | 32.0 | 87.100 | 175.90 | 178.100 | 20.0 | 14.5 | -20.2 | 1.7354 | 2 | 18 |
2 | 1.6321 | 0.375790 | 0.46072 | 0.90327 | 0.28563 | 0.287440 | 1.35890 | 0.416040 | 0.69684 | 0.45008 | ... | 30.0 | 92.900 | 203.20 | 204.500 | 22.0 | 22.0 | 6.4 | 5.5809 | 8 | 17 |
3 rows × 82 columns
Financial Distress | |
---|---|
0 | 0 |
1 | 0 |
2 | 0 |
x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | ... | x73 | x74 | x75 | x76 | x77 | x78 | x79 | x81 | x82 | x83 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3379 | 0.98569 | 0.12912 | 0.62266 | 0.74377 | 0.13716 | -0.006040 | 0.65175 | 0.173600 | 0.34218 | 0.008050 | ... | 35.5 | 113.980 | 33.690 | 32.734 | 17.000 | 15.5 | 0.10000 | 0.045774 | 9 | 19 |
156 | 0.91084 | 0.04889 | 0.79108 | 0.68615 | 0.10943 | -0.050311 | 0.60633 | 0.071253 | 0.23401 | 0.011391 | ... | 35.0 | 120.870 | 36.070 | 35.273 | 17.000 | 15.0 | -0.20000 | 0.152330 | 7 | 29 |
2215 | 1.43350 | 0.20068 | 0.46538 | 0.54146 | 0.25140 | 0.096212 | 0.82271 | 0.370630 | 0.37538 | 0.187750 | ... | 36.0 | 98.066 | 29.543 | 28.489 | 16.583 | 16.0 | 0.31667 | 0.563030 | 1 | 19 |
3 rows × 82 columns
Financial Distress | |
---|---|
3379 | 0 |
156 | 0 |
2215 | 0 |
In [3]:
from src.utils import run_classification, run_regression
from sklearn import tree
fitted_model = tree.DecisionTreeClassifier(max_depth=7, random_state=random_state).fit(
X_train.values, y_train.values.ravel()
)
result = run_classification(
fitted_model, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test
)
result
c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\base.py:486: UserWarning: X has feature names, but DecisionTreeClassifier was fitted without feature names warnings.warn( c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\base.py:486: UserWarning: X has feature names, but DecisionTreeClassifier was fitted without feature names warnings.warn(
Out[3]:
{'pipeline': DecisionTreeClassifier(max_depth=7, random_state=9), 'probs': array([1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 0.98984772, 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 0.04451683, 0.98984772, 0.98984772, 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 1. , 0.98984772, 0.04451683, 0.98984772, 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 0.64285714, 0.98984772, 0.04451683, 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 0.64285714, 0.04451683, 1. , 1. , 1. , 0.04451683, 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 0. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 0.15901814, 1. , 1. , 0.64285714, 1. , 1. , 0.64285714, 1. , 0.15901814, 0.15901814, 0.64285714, 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 0.04451683, 1. , 0.04451683, 0.15901814, 1. , 0.98984772, 1. , 1. , 0.625 , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 0.04451683, 0.98984772, 0.98984772, 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.64285714, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.64285714, 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 0.15901814, 0.15901814, 1. , 0.98984772, 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 0.15901814, 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.64285714, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 0.15901814, 1. , 0.98984772, 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0. , 0.15901814, 1. , 0.64285714, 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 0.98984772, 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 0.98984772, 1. , 0.08333333, 1. , 1. , 1. , 1. , 1. , 0. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 0.98984772, 1. , 1. , 0. , 1. , 1. , 0.64285714, 0.98984772, 1. , 0.04451683, 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 0. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 0.15901814, 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 0.15901814, 1. , 0.8 , 1. , 1. , 1. , 1. , 1. , 1. , 0.64285714, 0.64285714, 1. , 1. , 0.08333333, 1. , 1. , 1. , 0.8 , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 0.04451683, 1. , 0.15901814, 1. , 0.04451683, 1. , 1. , 1. , 0.98984772, 0.15901814, 1. , 0.64285714, 1. , 1. , 1. , 0.64285714, 1. , 1. , 0.64285714, 0. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 0.98984772, 1. , 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 0.98984772, 0.04451683, 1. , 0.98984772, 1. , 1. , 0.04451683, 1. , 1. , 1. , 0.64285714, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 0.35294118, 0.15901814, 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 0.04451683, 1. , 0.15901814, 1. , 0.04451683, 1. , 1. , 1. , 0.64285714, 1. , 0.04451683, 1. , 0.15901814, 1. , 1. , 0.15901814, 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 0.98984772, 0.04451683, 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 0.98984772, 0.04451683, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 1. , 1. , 1. , 0.98984772, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0. , 1. , 1. , 1. , 1. , 1. , 1. , 0.15901814, 0.04451683, 1. , 1. , 0.98984772, 1. ]), 'preds': array([1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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1, 1, 1, 1]), 'Precision_train': np.float64(0.9209039548022598), 'Precision_test': np.float64(0.015552099533437015), 'Recall_train': np.float64(0.9889364739471805), 'Recall_test': np.float64(0.45454545454545453), 'Accuracy_train': 0.9521777777777778, 'Accuracy_test': 0.12244897959183673, 'ROC_AUC_test': np.float64(0.23084278974882058), 'F1_train': np.float64(0.9537084839098262), 'F1_test': np.float64(0.03007518796992481), 'MCC_test': np.float64(-0.22309912384470268), 'Cohen_kappa_test': np.float64(-0.029516833411874055), 'Confusion_matrix': array([[ 80, 633], [ 12, 10]])}
In [49]:
import numpy as np
from sklearn import model_selection
parameters = {
"criterion": ["squared_error", "absolute_error", "friedman_mse", "poisson"],
"max_depth": np.arange(1, 21).tolist()[0::2],
"min_samples_split": np.arange(2, 20).tolist()[0::2],
}
grid = model_selection.GridSearchCV(
tree.DecisionTreeRegressor(random_state=random_state), parameters, n_jobs=-1
)
grid.fit(X_train, y_train)
grid.best_params_
c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\model_selection\_validation.py:540: FitFailedWarning: 450 fits failed out of a total of 1800. The score on these train-test partitions for these parameters will be set to nan. If these failures are not expected, you can try to debug them by setting error_score='raise'. Below are more details about the failures: -------------------------------------------------------------------------------- 450 fits failed with the following error: Traceback (most recent call last): File "c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\model_selection\_validation.py", line 888, in _fit_and_score estimator.fit(X_train, y_train, **fit_params) File "c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\base.py", line 1473, in wrapper return fit_method(estimator, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\tree\_classes.py", line 1377, in fit super()._fit( File "c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\tree\_classes.py", line 269, in _fit raise ValueError( ValueError: Some value(s) of y are negative which is not allowed for Poisson regression. warnings.warn(some_fits_failed_message, FitFailedWarning) c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\model_selection\_search.py:1103: UserWarning: One or more of the test scores are non-finite: [-4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.17872998 -4.17872998 -4.1794137 -4.18153603 -4.18153603 -4.18153603 -4.18153603 -4.18153603 -4.18153603 -4.29228886 -4.27823005 -4.27378326 -4.27592366 -4.27917056 -4.23213065 -4.22140931 -4.2203268 -4.216674 -4.22464637 -4.33616785 -4.27369282 -4.25199473 -4.31065536 -4.27417069 -4.24259248 -4.2377248 -4.2476017 -4.32852991 -4.31823651 -4.28553229 -4.34177931 -4.30250957 -4.28753163 -4.28076012 -4.25271136 -4.25347712 -4.3969495 -4.36131695 -4.32082651 -4.31057625 -4.33653274 -4.3316473 -4.29354845 -4.30058389 -4.2945977 -4.33373406 -4.38833647 -4.35267217 -4.33498752 -4.4013588 -4.32169037 -4.30799648 -4.30856117 -4.35205443 -4.38832687 -4.37876963 -4.36158341 -4.33729531 -4.36291152 -4.34227116 -4.33926356 -4.31523722 -4.31237617 -4.42139371 -4.35079669 -4.40994949 -4.35999754 -4.37508884 -4.39768089 -4.36889387 -4.31988427 -4.30750986 0.2652827 0.2652827 0.2652827 0.2652827 0.2652827 0.2652827 0.2652827 0.2652827 0.2652827 -1.9287676 -1.9287676 -1.9287676 -1.9287676 -1.9287676 -1.9287676 -1.9287676 -1.9287676 -1.9287676 -1.97020055 -1.97020055 -1.97042935 -1.97042935 -1.97004014 -1.97004014 -1.9700399 -1.99709038 -1.99715081 -2.00301099 -1.99717467 -1.9930943 -1.99098557 -1.98702489 -1.98700995 -1.98699179 -2.01302486 -2.01501513 -2.10894255 -2.10994722 -2.09952227 -2.08394081 -2.08202479 -2.07188081 -2.07112545 -2.09099692 -2.09034787 -2.15392376 -2.1403913 -2.14641712 -2.12768247 -2.11768777 -2.11183283 -2.03846712 -2.12716538 -2.1272445 -2.16307528 -2.17486177 -2.19327604 -2.16449752 -2.08104295 -2.13803493 -2.08495978 -2.14818606 -2.14245762 -2.18712444 -2.20432741 -2.18741809 -2.17137267 -2.16649999 -2.09136755 -2.16386709 -2.17518238 -2.15065344 -2.24366056 -2.14969955 -2.12174719 -2.11054372 -2.17740208 -2.15664086 -2.16327196 -2.1757021 -2.16935629 -2.20600588 -2.21021639 -2.20224851 -2.19087629 -2.18339126 -2.16722284 -2.16000468 -2.18148192 -2.17331625 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.40722975 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.02547375 -4.17872998 -4.17872998 -4.1794137 -4.18153603 -4.18153603 -4.18153603 -4.18153603 -4.18153603 -4.18153603 -4.31301085 -4.27817322 -4.27362102 -4.27576142 -4.27917056 -4.23213065 -4.22140931 -4.2203268 -4.216674 -4.308967 -4.33406277 -4.27031206 -4.25187889 -4.30964117 -4.27417069 -4.24259248 -4.23837042 -4.2476017 -4.30938294 -4.35432558 -4.28424212 -4.3429458 -4.3011881 -4.28789821 -4.28076012 -4.25271136 -4.25347712 -4.36334041 -4.36324461 -4.32002519 -4.3118746 -4.33707678 -4.33137066 -4.29320914 -4.30010728 -4.2945977 -4.38237244 -4.38340433 -4.34774631 -4.33676135 -4.40583985 -4.33138452 -4.30737588 -4.30825185 -4.35224119 -4.38925553 -4.35711249 -4.35484652 -4.34829607 -4.3661772 -4.35261695 -4.33890487 -4.31498202 -4.31236225 -4.43228313 -4.38839311 -4.40088779 -4.35168819 -4.38046828 -4.39657208 -4.36830249 -4.31947573 -4.30727193 nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan] warnings.warn(
Out[49]:
{'criterion': 'absolute_error', 'max_depth': 1, 'min_samples_split': 2}
In [50]:
old_metrics = {
"RMSE_test": models["decision_tree"]["RMSE_test"],
"RMAE_test": models["decision_tree"]["RMAE_test"],
"R2_test": models["decision_tree"]["R2_test"],
}
new_metrics = run_regression(grid.best_estimator_, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test)
display(old_metrics)
display(new_metrics)
c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\base.py:493: UserWarning: X does not have valid feature names, but DecisionTreeRegressor was fitted with feature names warnings.warn( c:\Users\user\Projects\python\fuzzy\.venv\Lib\site-packages\sklearn\base.py:493: UserWarning: X does not have valid feature names, but DecisionTreeRegressor was fitted with feature names warnings.warn(
{'RMSE_test': 1.1620245531837428, 'RMAE_test': 0.793506853815132, 'R2_test': 0.32558767171344627}
{'fitted': DecisionTreeRegressor(criterion='absolute_error', max_depth=1, random_state=9), 'train_preds': array([0.40003, 2.4023 , 2.4023 , ..., 0.40003, 0.40003, 0.40003]), 'preds': array([0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 2.4023 , 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 0.40003, 2.4023 , 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 0.40003, 2.4023 , 0.40003, 0.40003, 2.4023 , 2.4023 , 0.40003, 2.4023 ]), 'RMSE_train': 2.6924305509873223, 'RMSE_test': 1.198383084740795, 'RMAE_test': 0.8489844401433057, 'R2_test': 0.2827241118036927}
In [93]:
rules = tree.export_text(
fitted_model,
feature_names=X_train.columns.values.tolist(),
)
print(rules)
|--- x81 <= -0.00 | |--- x81 <= -0.44 | | |--- x4 <= 0.58 | | | |--- x58 <= -0.08 | | | | |--- class: 0 | | | |--- x58 > -0.08 | | | | |--- class: 1 | | |--- x4 > 0.58 | | | |--- x38 <= 0.04 | | | | |--- x81 <= -0.48 | | | | | |--- class: 1 | | | | |--- x81 > -0.48 | | | | | |--- class: 0 | | | |--- x38 > 0.04 | | | | |--- class: 1 | |--- x81 > -0.44 | | |--- x44 <= -0.07 | | | |--- x40 <= 0.33 | | | | |--- x3 <= 0.72 | | | | | |--- x23 <= 0.17 | | | | | | |--- class: 0 | | | | | |--- x23 > 0.17 | | | | | | |--- x15 <= 1.22 | | | | | | | |--- class: 0 | | | | | | |--- x15 > 1.22 | | | | | | | |--- class: 1 | | | | |--- x3 > 0.72 | | | | | |--- x41 <= 9.63 | | | | | | |--- x81 <= -0.40 | | | | | | | |--- class: 0 | | | | | | |--- x81 > -0.40 | | | | | | | |--- class: 1 | | | | | |--- x41 > 9.63 | | | | | | |--- class: 0 | | | |--- x40 > 0.33 | | | | |--- x75 <= 41.70 | | | | | |--- x23 <= 0.21 | | | | | | |--- class: 1 | | | | | |--- x23 > 0.21 | | | | | | |--- class: 0 | | | | |--- x75 > 41.70 | | | | | |--- x10 <= -0.19 | | | | | | |--- class: 1 | | | | | |--- x10 > -0.19 | | | | | | |--- class: 0 | | |--- x44 > -0.07 | | | |--- x57 <= 0.18 | | | | |--- x7 <= 0.74 | | | | | |--- class: 0 | | | | |--- x7 > 0.74 | | | | | |--- x15 <= 3.68 | | | | | | |--- x28 <= -0.12 | | | | | | | |--- class: 1 | | | | | | |--- x28 > -0.12 | | | | | | | |--- class: 0 | | | | | |--- x15 > 3.68 | | | | | | |--- class: 1 | | | |--- x57 > 0.18 | | | | |--- x49 <= 2.63 | | | | | |--- class: 0 | | | | |--- x49 > 2.63 | | | | | |--- x71 <= 45.23 | | | | | | |--- class: 1 | | | | | |--- x71 > 45.23 | | | | | | |--- class: 0 |--- x81 > -0.00 | |--- x25 <= -812.33 | | |--- class: 1 | |--- x25 > -812.33 | | |--- x36 <= 0.08 | | | |--- x47 <= 17.10 | | | | |--- x43 <= 0.15 | | | | | |--- x13 <= 0.07 | | | | | | |--- class: 0 | | | | | |--- x13 > 0.07 | | | | | | |--- class: 1 | | | | |--- x43 > 0.15 | | | | | |--- x75 <= 33.09 | | | | | | |--- x42 <= -0.92 | | | | | | | |--- class: 1 | | | | | | |--- x42 > -0.92 | | | | | | | |--- class: 0 | | | | | |--- x75 > 33.09 | | | | | | |--- x60 <= 0.66 | | | | | | | |--- class: 0 | | | | | | |--- x60 > 0.66 | | | | | | | |--- class: 1 | | | |--- x47 > 17.10 | | | | |--- class: 1 | | |--- x36 > 0.08 | | | |--- x53 <= -0.02 | | | | |--- class: 1 | | | |--- x53 > -0.02 | | | | |--- x46 <= 0.03 | | | | | |--- x46 <= 0.03 | | | | | | |--- class: 0 | | | | | |--- x46 > 0.03 | | | | | | |--- class: 1 | | | | |--- x46 > 0.03 | | | | | |--- x26 <= 11.02 | | | | | | |--- x79 <= -17.05 | | | | | | | |--- class: 1 | | | | | | |--- x79 > -17.05 | | | | | | | |--- class: 0 | | | | | |--- x26 > 11.02 | | | | | | |--- x24 <= 0.82 | | | | | | | |--- class: 0 | | | | | | |--- x24 > 0.82 | | | | | | | |--- class: 0
In [13]:
import pickle
pickle.dump(
models["decision_tree"]["fitted"], open("data-distress/vtree.model.sav", "wb")
)