Nearest Neighbors - Práctica de regresión
Índice de contenido
python
from sklearn import datasets
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import GridSearchCV, 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]]),
'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.]),
'frame': None,
'DESCR': '.. _diabetes_dataset:\n\nDiabetes dataset\n----------------\n\nTen baseline variables, age, sex, body mass index, average blood\npressure, and six blood serum measurements were obtained for each of n =\n442 diabetes patients, as well as the response of interest, a\nquantitative measure of disease progression one year after baseline.\n\n**Data Set Characteristics:**\n\n :Number of Instances: 442\n\n :Number of Attributes: First 10 columns are numeric predictive values\n\n :Target: Column 11 is a quantitative measure of disease progression one year after baseline\n\n :Attribute Information:\n - age age in years\n - sex\n - bmi body mass index\n - bp average blood pressure\n - s1 tc, T-Cells (a type of white blood cells)\n - s2 ldl, low-density lipoproteins\n - s3 hdl, high-density lipoproteins\n - s4 tch, thyroid stimulating hormone\n - s5 ltg, lamotrigine\n - s6 glu, blood sugar level\n\nNote: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\n\nSource URL:\nhttps://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\n\nFor more information see:\nBradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) "Least Angle Regression," Annals of Statistics (with discussion), 407-499.\n(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)',
'feature_names': ['age',
'sex',
'bmi',
'bp',
's1',
's2',
's3',
's4',
's5',
's6'],
'data_filename': 'C:\\Users\\user\\anaconda3\\envs\\python-385\\lib\\site-packages\\sklearn\\datasets\\data\\diabetes_data.csv.gz',
'target_filename': 'C:\\Users\\user\\anaconda3\\envs\\python-385\\lib\\site-packages\\sklearn\\datasets\\data\\diabetes_target.csv.gz'}
python
regressor = KNeighborsRegressor()
python
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target, test_size = 0.1)
python
param_grid = {"n_neighbors": range(1, 51),
"weights": ["uniform", "distance"]}
python
gs = GridSearchCV(estimator = regressor,
param_grid = param_grid,
scoring = None,
cv = None)
python
gs.fit(X_train, y_train)
GridSearchCV(estimator=KNeighborsRegressor(),
param_grid={'n_neighbors': range(1, 51),
'weights': ['uniform', 'distance']})
python
pd.DataFrame(gs.cv_results_)
| mean_fit_time | std_fit_time | mean_score_time | std_score_time | param_n_neighbors | param_weights | 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.0016 | 0.000490 | 0.0040 | 0.002191 | 1 | uniform | {'n_neighbors': 1, 'weights': 'uniform'} | 0.099121 | -0.438021 | 0.146658 | -0.109432 | -0.226945 | -0.105724 | 0.214843 | 99 |
| 1 | 0.0020 | 0.000633 | 0.0036 | 0.001020 | 1 | distance | {'n_neighbors': 1, 'weights': 'distance'} | 0.099121 | -0.438021 | 0.146658 | -0.109432 | -0.226945 | -0.105724 | 0.214843 | 99 |
| 2 | 0.0016 | 0.000490 | 0.0024 | 0.000490 | 2 | uniform | {'n_neighbors': 2, 'weights': 'uniform'} | 0.409014 | -0.040204 | 0.400178 | 0.189174 | 0.055945 | 0.202821 | 0.180159 | 97 |
| 3 | 0.0018 | 0.000748 | 0.0050 | 0.002829 | 2 | distance | {'n_neighbors': 2, 'weights': 'distance'} | 0.403785 | -0.055187 | 0.409523 | 0.194802 | 0.054173 | 0.201419 | 0.185381 | 98 |
| 4 | 0.0012 | 0.000400 | 0.0028 | 0.000748 | 3 | uniform | {'n_neighbors': 3, 'weights': 'uniform'} | 0.422302 | 0.152462 | 0.381168 | 0.205034 | 0.148103 | 0.261814 | 0.116717 | 96 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 95 | 0.0030 | 0.003522 | 0.0040 | 0.000895 | 48 | distance | {'n_neighbors': 48, 'weights': 'distance'} | 0.435559 | 0.317012 | 0.496845 | 0.418796 | 0.370763 | 0.407795 | 0.060720 | 60 |
| 96 | 0.0014 | 0.000490 | 0.0042 | 0.000980 | 49 | uniform | {'n_neighbors': 49, 'weights': 'uniform'} | 0.424144 | 0.311043 | 0.488989 | 0.415719 | 0.370991 | 0.402177 | 0.059127 | 73 |
| 97 | 0.0016 | 0.000490 | 0.0038 | 0.000400 | 49 | distance | {'n_neighbors': 49, 'weights': 'distance'} | 0.433810 | 0.316019 | 0.496424 | 0.420152 | 0.373871 | 0.408055 | 0.060428 | 58 |
| 98 | 0.0016 | 0.000800 | 0.0046 | 0.001020 | 50 | uniform | {'n_neighbors': 50, 'weights': 'uniform'} | 0.418576 | 0.315853 | 0.486273 | 0.414148 | 0.370356 | 0.401041 | 0.056467 | 77 |
| 99 | 0.0014 | 0.000490 | 0.0038 | 0.000980 | 50 | distance | {'n_neighbors': 50, 'weights': 'distance'} | 0.429079 | 0.320304 | 0.494332 | 0.418878 | 0.373520 | 0.407223 | 0.058125 | 64 |
100 rows × 15 columns
python
gs.best_params_
{'n_neighbors': 19, 'weights': 'distance'}
python
y_pred = gs.best_estimator_.predict(X_test)
python
pd.DataFrame({"y_test": y_test, "y_pred": y_pred})
| y_test | y_pred | |
|---|---|---|
| 0 | 104.0 | 137.650311 |
| 1 | 77.0 | 86.602816 |
| 2 | 68.0 | 146.797549 |
| 3 | 68.0 | 117.532073 |
| 4 | 83.0 | 85.141512 |
| 5 | 59.0 | 80.687647 |
| 6 | 288.0 | 190.096514 |
| 7 | 259.0 | 211.419570 |
| 8 | 63.0 | 91.851842 |
| 9 | 135.0 | 105.447488 |
| 10 | 262.0 | 137.475692 |
| 11 | 72.0 | 105.657106 |
| 12 | 131.0 | 233.695395 |
| 13 | 43.0 | 89.422818 |
| 14 | 220.0 | 247.676580 |
| 15 | 53.0 | 92.214988 |
| 16 | 85.0 | 100.687193 |
| 17 | 303.0 | 238.321790 |
| 18 | 237.0 | 178.943330 |
| 19 | 276.0 | 139.171765 |
| 20 | 128.0 | 127.893729 |
| 21 | 78.0 | 120.137293 |
| 22 | 53.0 | 87.562168 |
| 23 | 155.0 | 141.806335 |
| 24 | 90.0 | 113.663286 |
| 25 | 131.0 | 109.867953 |
| 26 | 281.0 | 220.808215 |
| 27 | 161.0 | 140.254437 |
| 28 | 308.0 | 258.388522 |
| 29 | 70.0 | 146.095996 |
| 30 | 97.0 | 145.519671 |
| 31 | 178.0 | 121.900573 |
| 32 | 98.0 | 96.823925 |
| 33 | 134.0 | 123.844996 |
| 34 | 91.0 | 168.661938 |
| 35 | 121.0 | 137.260953 |
| 36 | 170.0 | 98.884172 |
| 37 | 138.0 | 181.461382 |
| 38 | 90.0 | 89.205088 |
| 39 | 144.0 | 134.550482 |
| 40 | 184.0 | 146.286218 |
| 41 | 202.0 | 189.983647 |
| 42 | 144.0 | 197.776875 |
| 43 | 139.0 | 198.772766 |
| 44 | 174.0 | 192.824623 |
python
best_model = gs.best_estimator_
python
best_model.score(X_test, y_test)
0.5034957860146034
python
best_model.get_params()
{'algorithm': 'auto',
'leaf_size': 30,
'metric': 'minkowski',
'metric_params': None,
'n_jobs': None,
'n_neighbors': 19,
'p': 2,
'weights': 'distance'}
python
best_model.effective_metric_
'euclidean'
python
best_model.effective_metric_params_
{}
