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_time | std_fit_time | mean_score_time | std_score_time | param_max_depth | params | split0_test_score | split1_test_score | split2_test_score | split3_test_score | split4_test_score | mean_test_score | std_test_score | rank_test_score | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.0024 | 8.000374e-04 | 0.0012 | 0.000400 | 1 | {'max_depth': 1} | -0.060515 | 0.169450 | 0.237667 | 0.239624 | 0.277944 | 0.172834 | 0.121799 | 5 |
| 1 | 0.0020 | 9.536743e-08 | 0.0018 | 0.000400 | 2 | {'max_depth': 2} | 0.157606 | 0.345946 | 0.390015 | 0.376850 | 0.420685 | 0.338220 | 0.093442 | 2 |
| 2 | 0.0036 | 4.900376e-04 | 0.0022 | 0.000400 | 3 | {'max_depth': 3} | 0.192454 | 0.376758 | 0.450917 | 0.461504 | 0.333924 | 0.363111 | 0.097522 | 1 |
| 3 | 0.0022 | 7.483665e-04 | 0.0014 | 0.000490 | 4 | {'max_depth': 4} | 0.236153 | 0.233785 | 0.447801 | 0.481629 | 0.277903 | 0.335454 | 0.107236 | 3 |
| 4 | 0.0030 | 6.324097e-04 | 0.0010 | 0.000632 | 5 | {'max_depth': 5} | 0.114858 | 0.054706 | 0.303211 | 0.489421 | 0.224088 | 0.237257 | 0.152589 | 4 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 95 | 0.0046 | 4.900182e-04 | 0.0014 | 0.000490 | 96 | {'max_depth': 96} | -0.396410 | -0.246646 | 0.069781 | 0.320491 | -0.288523 | -0.108261 | 0.264652 | 69 |
| 96 | 0.0070 | 3.033323e-03 | 0.0026 | 0.002245 | 97 | {'max_depth': 97} | -0.359487 | -0.373346 | 0.078477 | 0.305255 | -0.261482 | -0.122117 | 0.268986 | 80 |
| 97 | 0.0050 | 6.324097e-04 | 0.0012 | 0.000400 | 98 | {'max_depth': 98} | -0.265533 | -0.368183 | 0.009950 | 0.389968 | -0.247807 | -0.096321 | 0.273225 | 54 |
| 98 | 0.0052 | 3.999949e-04 | 0.0018 | 0.000748 | 99 | {'max_depth': 99} | -0.299163 | -0.334341 | 0.092851 | 0.222707 | -0.275652 | -0.118719 | 0.230224 | 77 |
| 99 | 0.0062 | 2.482189e-03 | 0.0016 | 0.000490 | 100 | {'max_depth': 100} | -0.293294 | -0.283854 | 0.029438 | 0.284367 | -0.229963 | -0.098661 | 0.224739 | 56 |
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_test | y_pred | |
|---|---|---|
| 0 | 125.0 | 94.490323 |
| 1 | 116.0 | 94.490323 |
| 2 | 145.0 | 157.107143 |
| 3 | 200.0 | 94.490323 |
| 4 | 295.0 | 221.600000 |
| 5 | 296.0 | 208.250000 |
| 6 | 111.0 | 94.490323 |
| 7 | 196.0 | 157.107143 |
| 8 | 209.0 | 142.660714 |
| 9 | 98.0 | 94.490323 |
| 10 | 131.0 | 142.660714 |
| 11 | 109.0 | 267.114286 |
| 12 | 158.0 | 94.490323 |
| 13 | 111.0 | 157.107143 |
| 14 | 265.0 | 208.250000 |
| 15 | 279.0 | 157.107143 |
| 16 | 113.0 | 208.250000 |
| 17 | 70.0 | 94.490323 |
| 18 | 249.0 | 187.432432 |
| 19 | 89.0 | 94.490323 |
| 20 | 175.0 | 208.250000 |
| 21 | 49.0 | 94.490323 |
| 22 | 168.0 | 187.432432 |
| 23 | 88.0 | 94.490323 |
| 24 | 131.0 | 187.432432 |
| 25 | 185.0 | 142.660714 |
| 26 | 58.0 | 187.432432 |
| 27 | 63.0 | 94.490323 |
| 28 | 52.0 | 208.250000 |
| 29 | 121.0 | 157.107143 |
| 30 | 61.0 | 94.490323 |
| 31 | 89.0 | 253.000000 |
| 32 | 160.0 | 94.490323 |
| 33 | 40.0 | 94.490323 |
| 34 | 120.0 | 142.660714 |
| 35 | 214.0 | 142.660714 |
| 36 | 132.0 | 94.490323 |
| 37 | 107.0 | 187.432432 |
| 38 | 230.0 | 94.490323 |
| 39 | 259.0 | 157.107143 |
| 40 | 181.0 | 187.432432 |
| 41 | 265.0 | 208.250000 |
| 42 | 258.0 | 208.250000 |
| 43 | 283.0 | 208.250000 |
| 44 | 215.0 | 267.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'
