Created
January 31, 2016 20:21
-
-
Save zettamax/235add1c47b1b83737e8 to your computer and use it in GitHub Desktop.
iPython Notebook file - Boston
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from sklearn.neighbors import KNeighborsRegressor\n", | |
"from sklearn.datasets import load_boston\n", | |
"from sklearn.cross_validation import KFold\n", | |
"from sklearn.cross_validation import cross_val_score\n", | |
"import pandas as pn\n", | |
"import sklearn.preprocessing\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"boston = sklearn.datasets.load_boston()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"data = boston.data\n", | |
"data = sklearn.preprocessing.scale(data)\n", | |
"target = boston.target" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"kfold = KFold(len(data), n_folds=5, shuffle=True, random_state=42)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.135678391959799" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores = []\n", | |
"p_values = []\n", | |
"\n", | |
"p_arr = np.linspace(1, 10, 200)\n", | |
"for p in p_arr:\n", | |
" kn_regressor = KNeighborsRegressor(metric='minkowski', p=p, weights='distance')\n", | |
" score = cross_val_score(kn_regressor, data, y=target, scoring='mean_squared_error', cv=kfold)\n", | |
" \n", | |
" scores.append(score.max())\n", | |
" p_values.append(p)\n", | |
" \n", | |
"opt_p = p_values[np.array(scores).argmax()]\n", | |
"opt_p" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment