k-Nearest Neighbors is added along with the Linear Regression model for example 1-1

main
Amlan Dutta 2021-05-04 21:58:54 +05:30
parent cbe47c65d5
commit ae670cda67
1 changed files with 41 additions and 4 deletions

View File

@ -170,6 +170,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,
@ -771,9 +804,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": {
@ -785,7 +817,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.9.4-final"
},
"metadata": {
"interpreter": {
"hash": "22b0ec00cd9e253c751e6d2619fc0bb2d18ed12980de3246690d5be49479dd65"
}
},
"nav_menu": {},
"toc": {