preloader

Decision Trees - Práctica de regresión

Índice de contenido
python
from sklearn import datasets
import graphviz
from sklearn import tree
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import pandas as pd
python
diabetes = datasets.load_diabetes()
python
diabetes.data
array([[ 0.03807591,  0.05068012,  0.06169621, ..., -0.00259226,
         0.01990842, -0.01764613],
       [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,
        -0.06832974, -0.09220405],
       [ 0.08529891,  0.05068012,  0.04445121, ..., -0.00259226,
         0.00286377, -0.02593034],
       ...,
       [ 0.04170844,  0.05068012, -0.01590626, ..., -0.01107952,
        -0.04687948,  0.01549073],
       [-0.04547248, -0.04464164,  0.03906215, ...,  0.02655962,
         0.04452837, -0.02593034],
       [-0.04547248, -0.04464164, -0.0730303 , ..., -0.03949338,
        -0.00421986,  0.00306441]])
python
diabetes.target
array([151.,  75., 141., 206., 135.,  97., 138.,  63., 110., 310., 101.,
        69., 179., 185., 118., 171., 166., 144.,  97., 168.,  68.,  49.,
        68., 245., 184., 202., 137.,  85., 131., 283., 129.,  59., 341.,
        87.,  65., 102., 265., 276., 252.,  90., 100.,  55.,  61.,  92.,
       259.,  53., 190., 142.,  75., 142., 155., 225.,  59., 104., 182.,
       128.,  52.,  37., 170., 170.,  61., 144.,  52., 128.,  71., 163.,
       150.,  97., 160., 178.,  48., 270., 202., 111.,  85.,  42., 170.,
       200., 252., 113., 143.,  51.,  52., 210.,  65., 141.,  55., 134.,
        42., 111.,  98., 164.,  48.,  96.,  90., 162., 150., 279.,  92.,
        83., 128., 102., 302., 198.,  95.,  53., 134., 144., 232.,  81.,
       104.,  59., 246., 297., 258., 229., 275., 281., 179., 200., 200.,
       173., 180.,  84., 121., 161.,  99., 109., 115., 268., 274., 158.,
       107.,  83., 103., 272.,  85., 280., 336., 281., 118., 317., 235.,
        60., 174., 259., 178., 128.,  96., 126., 288.,  88., 292.,  71.,
       197., 186.,  25.,  84.,  96., 195.,  53., 217., 172., 131., 214.,
        59.,  70., 220., 268., 152.,  47.,  74., 295., 101., 151., 127.,
       237., 225.,  81., 151., 107.,  64., 138., 185., 265., 101., 137.,
       143., 141.,  79., 292., 178.,  91., 116.,  86., 122.,  72., 129.,
       142.,  90., 158.,  39., 196., 222., 277.,  99., 196., 202., 155.,
        77., 191.,  70.,  73.,  49.,  65., 263., 248., 296., 214., 185.,
        78.,  93., 252., 150.,  77., 208.,  77., 108., 160.,  53., 220.,
       154., 259.,  90., 246., 124.,  67.,  72., 257., 262., 275., 177.,
        71.,  47., 187., 125.,  78.,  51., 258., 215., 303., 243.,  91.,
       150., 310., 153., 346.,  63.,  89.,  50.,  39., 103., 308., 116.,
       145.,  74.,  45., 115., 264.,  87., 202., 127., 182., 241.,  66.,
        94., 283.,  64., 102., 200., 265.,  94., 230., 181., 156., 233.,
        60., 219.,  80.,  68., 332., 248.,  84., 200.,  55.,  85.,  89.,
        31., 129.,  83., 275.,  65., 198., 236., 253., 124.,  44., 172.,
       114., 142., 109., 180., 144., 163., 147.,  97., 220., 190., 109.,
       191., 122., 230., 242., 248., 249., 192., 131., 237.,  78., 135.,
       244., 199., 270., 164.,  72.,  96., 306.,  91., 214.,  95., 216.,
       263., 178., 113., 200., 139., 139.,  88., 148.,  88., 243.,  71.,
        77., 109., 272.,  60.,  54., 221.,  90., 311., 281., 182., 321.,
        58., 262., 206., 233., 242., 123., 167.,  63., 197.,  71., 168.,
       140., 217., 121., 235., 245.,  40.,  52., 104., 132.,  88.,  69.,
       219.,  72., 201., 110.,  51., 277.,  63., 118.,  69., 273., 258.,
        43., 198., 242., 232., 175.,  93., 168., 275., 293., 281.,  72.,
       140., 189., 181., 209., 136., 261., 113., 131., 174., 257.,  55.,
        84.,  42., 146., 212., 233.,  91., 111., 152., 120.,  67., 310.,
        94., 183.,  66., 173.,  72.,  49.,  64.,  48., 178., 104., 132.,
       220.,  57.])
python
regressor = tree.DecisionTreeRegressor()
python
param_grid = {"max_depth": range(1, 101)}
python
gs = GridSearchCV(estimator = regressor,
                 param_grid= param_grid,
                 scoring=None,
                 cv = None)
python
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target, test_size = 0.1)
python
gs.fit(X_train, y_train)
GridSearchCV(estimator=DecisionTreeRegressor(),
             param_grid={'max_depth': range(1, 101)})
python
pd.DataFrame(gs.cv_results_)

mean_fit_timestd_fit_timemean_score_timestd_score_timeparam_max_depthparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.00248.000374e-040.00120.0004001{'max_depth': 1}-0.0605150.1694500.2376670.2396240.2779440.1728340.1217995
10.00209.536743e-080.00180.0004002{'max_depth': 2}0.1576060.3459460.3900150.3768500.4206850.3382200.0934422
20.00364.900376e-040.00220.0004003{'max_depth': 3}0.1924540.3767580.4509170.4615040.3339240.3631110.0975221
30.00227.483665e-040.00140.0004904{'max_depth': 4}0.2361530.2337850.4478010.4816290.2779030.3354540.1072363
40.00306.324097e-040.00100.0006325{'max_depth': 5}0.1148580.0547060.3032110.4894210.2240880.2372570.1525894
.............................................
950.00464.900182e-040.00140.00049096{'max_depth': 96}-0.396410-0.2466460.0697810.320491-0.288523-0.1082610.26465269
960.00703.033323e-030.00260.00224597{'max_depth': 97}-0.359487-0.3733460.0784770.305255-0.261482-0.1221170.26898680
970.00506.324097e-040.00120.00040098{'max_depth': 98}-0.265533-0.3681830.0099500.389968-0.247807-0.0963210.27322554
980.00523.999949e-040.00180.00074899{'max_depth': 99}-0.299163-0.3343410.0928510.222707-0.275652-0.1187190.23022477
990.00622.482189e-030.00160.000490100{'max_depth': 100}-0.293294-0.2838540.0294380.284367-0.229963-0.0986610.22473956

100 rows × 14 columns

python
best_model = gs.best_estimator_
best_model
DecisionTreeRegressor(max_depth=3)
python
best_model.predict(X_test)
array([ 94.49032258,  94.49032258, 157.10714286,  94.49032258,
       221.6       , 208.25      ,  94.49032258, 157.10714286,
       142.66071429,  94.49032258, 142.66071429, 267.11428571,
        94.49032258, 157.10714286, 208.25      , 157.10714286,
       208.25      ,  94.49032258, 187.43243243,  94.49032258,
       208.25      ,  94.49032258, 187.43243243,  94.49032258,
       187.43243243, 142.66071429, 187.43243243,  94.49032258,
       208.25      , 157.10714286,  94.49032258, 253.        ,
        94.49032258,  94.49032258, 142.66071429, 142.66071429,
        94.49032258, 187.43243243,  94.49032258, 157.10714286,
       187.43243243, 208.25      , 208.25      , 208.25      ,
       267.11428571])
python
pd.DataFrame({"y_test": y_test, "y_pred": best_model.predict(X_test)})

y_testy_pred
0125.094.490323
1116.094.490323
2145.0157.107143
3200.094.490323
4295.0221.600000
5296.0208.250000
6111.094.490323
7196.0157.107143
8209.0142.660714
998.094.490323
10131.0142.660714
11109.0267.114286
12158.094.490323
13111.0157.107143
14265.0208.250000
15279.0157.107143
16113.0208.250000
1770.094.490323
18249.0187.432432
1989.094.490323
20175.0208.250000
2149.094.490323
22168.0187.432432
2388.094.490323
24131.0187.432432
25185.0142.660714
2658.0187.432432
2763.094.490323
2852.0208.250000
29121.0157.107143
3061.094.490323
3189.0253.000000
32160.094.490323
3340.094.490323
34120.0142.660714
35214.0142.660714
36132.094.490323
37107.0187.432432
38230.094.490323
39259.0157.107143
40181.0187.432432
41265.0208.250000
42258.0208.250000
43283.0208.250000
44215.0267.114286
python
X_test[0]
array([-0.02730979, -0.04464164, -0.03530688, -0.02977071, -0.05660707,
       -0.05862005,  0.03023191, -0.03949338, -0.04986847, -0.12948301])
python
best_model.score(X_test, y_test)
0.042966591843769275
python
diabetes.feature_names
['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
python
data = tree.export_graphviz(best_model, out_file=None,
                           feature_names = diabetes.feature_names,
                           filled = True,
                           rounded = True)
graph = graphviz.Source(data)
graph.render("C:/users/User/Desktop/diabetes_best_v2")
'C:/users/User/Desktop\\diabetes_best_v2.pdf'
comments powered by Disqus