Merge pull request #424 from amlan-dutta/master

k-Nearest Neighbors is added along with the Linear Regression model for example 1-1
main
Aurélien Geron 2021-05-27 14:56:48 +12:00 committed by GitHub
commit ee4befafaf
1 changed files with 40 additions and 3 deletions

View File

@ -173,6 +173,39 @@
"print(model.predict(X_new)) # outputs [[ 5.96242338]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Replacing the Linear Regression model with k-Nearest Neighbors (in this example, k = 3) regression in the previous code is as simple as replacing these two\n",
"lines:\n",
"\n",
"import sklearn.linear_model\n",
"model = sklearn.linear_model.LinearRegression()\n",
"\n",
"with these two:\n",
"\n",
"import sklearn.neighbors\n",
"model = sklearn.neighbors.KNeighborsRegressor(n_neighbors=3)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Select a 3-Nearest Neighbors regression model\n",
"import sklearn.neighbors\n",
"model1 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=3)\n",
"\n",
"# Train the model\n",
"model1.fit(X,y)\n",
"\n",
"# Make a prediction for Cyprus\n",
"print(model1.predict(X_new)) # outputs [[5.76666667]]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -774,9 +807,8 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
"display_name": "Python 3.9.4 64-bit",
"name": "python39464bite0b9acfa2c9e400a974360d829d6c43a"
},
"language_info": {
"codemirror_mode": {
@ -790,6 +822,11 @@
"pygments_lexer": "ipython3",
"version": "3.7.10"
},
"metadata": {
"interpreter": {
"hash": "22b0ec00cd9e253c751e6d2619fc0bb2d18ed12980de3246690d5be49479dd65"
}
},
"nav_menu": {},
"toc": {
"navigate_menu": true,