From ae670cda67cf020f6f5b0e6900cf78c3043d0c7f Mon Sep 17 00:00:00 2001 From: Amlan Dutta Date: Tue, 4 May 2021 21:58:54 +0530 Subject: [PATCH] k-Nearest Neighbors is added along with the Linear Regression model for example 1-1 --- 01_the_machine_learning_landscape.ipynb | 45 ++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/01_the_machine_learning_landscape.ipynb b/01_the_machine_learning_landscape.ipynb index 622a80a..92d4e48 100644 --- a/01_the_machine_learning_landscape.ipynb +++ b/01_the_machine_learning_landscape.ipynb @@ -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": {