preloader

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_timestd_fit_timemean_score_timestd_score_timeparam_n_neighborsparam_weightsparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.00160.0004900.00400.0021911uniform{'n_neighbors': 1, 'weights': 'uniform'}0.099121-0.4380210.146658-0.109432-0.226945-0.1057240.21484399
10.00200.0006330.00360.0010201distance{'n_neighbors': 1, 'weights': 'distance'}0.099121-0.4380210.146658-0.109432-0.226945-0.1057240.21484399
20.00160.0004900.00240.0004902uniform{'n_neighbors': 2, 'weights': 'uniform'}0.409014-0.0402040.4001780.1891740.0559450.2028210.18015997
30.00180.0007480.00500.0028292distance{'n_neighbors': 2, 'weights': 'distance'}0.403785-0.0551870.4095230.1948020.0541730.2014190.18538198
40.00120.0004000.00280.0007483uniform{'n_neighbors': 3, 'weights': 'uniform'}0.4223020.1524620.3811680.2050340.1481030.2618140.11671796
................................................
950.00300.0035220.00400.00089548distance{'n_neighbors': 48, 'weights': 'distance'}0.4355590.3170120.4968450.4187960.3707630.4077950.06072060
960.00140.0004900.00420.00098049uniform{'n_neighbors': 49, 'weights': 'uniform'}0.4241440.3110430.4889890.4157190.3709910.4021770.05912773
970.00160.0004900.00380.00040049distance{'n_neighbors': 49, 'weights': 'distance'}0.4338100.3160190.4964240.4201520.3738710.4080550.06042858
980.00160.0008000.00460.00102050uniform{'n_neighbors': 50, 'weights': 'uniform'}0.4185760.3158530.4862730.4141480.3703560.4010410.05646777
990.00140.0004900.00380.00098050distance{'n_neighbors': 50, 'weights': 'distance'}0.4290790.3203040.4943320.4188780.3735200.4072230.05812564

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_testy_pred
0104.0137.650311
177.086.602816
268.0146.797549
368.0117.532073
483.085.141512
559.080.687647
6288.0190.096514
7259.0211.419570
863.091.851842
9135.0105.447488
10262.0137.475692
1172.0105.657106
12131.0233.695395
1343.089.422818
14220.0247.676580
1553.092.214988
1685.0100.687193
17303.0238.321790
18237.0178.943330
19276.0139.171765
20128.0127.893729
2178.0120.137293
2253.087.562168
23155.0141.806335
2490.0113.663286
25131.0109.867953
26281.0220.808215
27161.0140.254437
28308.0258.388522
2970.0146.095996
3097.0145.519671
31178.0121.900573
3298.096.823925
33134.0123.844996
3491.0168.661938
35121.0137.260953
36170.098.884172
37138.0181.461382
3890.089.205088
39144.0134.550482
40184.0146.286218
41202.0189.983647
42144.0197.776875
43139.0198.772766
44174.0192.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_
{}
comments powered by Disqus