<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python-for-Machine-Learning%2FC2%2FK-Nearest-Neighbor-Regression%2FEnglish</id>
		<title>Python-for-Machine-Learning/C2/K-Nearest-Neighbor-Regression/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python-for-Machine-Learning%2FC2%2FK-Nearest-Neighbor-Regression%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-for-Machine-Learning/C2/K-Nearest-Neighbor-Regression/English&amp;action=history"/>
		<updated>2026-05-13T07:59:40Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python-for-Machine-Learning/C2/K-Nearest-Neighbor-Regression/English&amp;diff=56959&amp;oldid=prev</id>
		<title>Nirmala Venkat: Created page with &quot;  &lt;div style=&quot;margin-left:1.27cm;margin-right:0cm;&quot;&gt;&lt;/div&gt; {| border=&quot;1&quot; |- || '''Visual Cue''' || '''Narration''' |- | style=&quot;border-top:none;border-bottom:0.75pt solid #0000...&quot;</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-for-Machine-Learning/C2/K-Nearest-Neighbor-Regression/English&amp;diff=56959&amp;oldid=prev"/>
				<updated>2025-06-09T13:12:20Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;  &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; {| border=&amp;quot;1&amp;quot; |- || &amp;#039;&amp;#039;&amp;#039;Visual Cue&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Narration&amp;#039;&amp;#039;&amp;#039; |- | style=&amp;quot;border-top:none;border-bottom:0.75pt solid #0000...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|| '''Visual Cue'''&lt;br /&gt;
|| '''Narration'''&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide&lt;br /&gt;
&lt;br /&gt;
'''Welcome '''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Welcome to the Spoken Tutorial on '''K Nearest Neighbor Regression.'''&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''Learning Objectives'''&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | In this tutorial, we will learn about&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Distance metrics''' for nearest neighbor identification.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Applying '''KNN''' regression to predict the petal length of '''iris''' flowers.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Evaluation using '''MSE''' and '''Adjusted R squared score.'''&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|- style=&amp;quot;border:0.5pt solid #000000;padding:0.106cm;&amp;quot;&lt;br /&gt;
|| Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''System Requirements'''&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Ubuntu Linux OS 24.04'''&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Jupyter Notebook IDE'''&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|| To record this tutorial, I am using&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Ubuntu Linux OS version 24.04'''&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Jupyter Notebook IDE'''&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''Pre-requisites'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | To follow this tutorial,&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;The learner must have basic knowledge of '''Python.'''&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;For pre-requisite '''Python''' tutorials, please visit this website.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''Code files'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; |&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;The files used in this tutorial are provided in the Code files link.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Please download and extract the files.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Make a copy and then use them while practicing.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide&lt;br /&gt;
&lt;br /&gt;
'''KNN Regression'''&lt;br /&gt;
| style=&amp;quot;border:0.75pt solid #000000;padding:0.106cm;&amp;quot; |&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''KNN regression''' is an algorithm that predicts values using nearby data points.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;The prediction in regression is based on the average value of the target variable.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''K''' indicates the''' number of neighboring points '''to be considered for prediction.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide: &lt;br /&gt;
&lt;br /&gt;
'''Distance Metrics'''&lt;br /&gt;
| style=&amp;quot;border:0.75pt solid #000000;padding:0.106cm;&amp;quot; | The various distance metrics used in '''KNN''' for finding Nearest Neighbors are&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Euclidean distance '''measures the shortest path between two points in space.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Scikit-learn '''library uses '''Euclidean '''as the default distance metric.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide: &lt;br /&gt;
&lt;br /&gt;
'''Distance Metrics'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; |&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Manhattan distance '''is the sum of absolute differences between coordinates.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;'''Minkowski '''and '''Chebyshev '''distances are other common distance metrics used in KNN.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Hover over the file&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | I have created the required file for the demonstration of '''KNN regression.'''&amp;lt;span style=&amp;quot;color:#ff0000;&amp;quot;&amp;gt; &amp;lt;/span&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Point to the '''KNNregression.ipynb'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | '''KNNregression dot ipynb '''is the ipython notebook file for this demonstration.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Press '''Ctrl,Alt'''+'''T '''keys&lt;br /&gt;
&lt;br /&gt;
Type '''conda activate ml'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Let us open the Linux terminal by pressing '''Ctrl,Alt''' and '''T '''keys together.&lt;br /&gt;
&lt;br /&gt;
Activate the machine learning environment as shown.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Go to the '''Downloads '''folder&lt;br /&gt;
&lt;br /&gt;
Type '''cd Downloads'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter '''&lt;br /&gt;
&lt;br /&gt;
Type '''jupyter notebook'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter '''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | I have saved my code file in the '''Downloads''' folder. &lt;br /&gt;
&lt;br /&gt;
Please navigate to the respective folder of your code file location.&lt;br /&gt;
&lt;br /&gt;
Then type, '''jupyter space notebook and''' press''' Enter.'''&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border:0.5pt solid #000000;padding:0.106cm;&amp;quot; | Show '''Jupyter Notebook Home page'''&lt;br /&gt;
&lt;br /&gt;
Click on&lt;br /&gt;
&lt;br /&gt;
'''KNNregression.ipynb '''file&lt;br /&gt;
| style=&amp;quot;border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;&amp;quot; | We see the '''Jupyter Notebook''' Home page.&lt;br /&gt;
&lt;br /&gt;
Let us open the '''KNNregression dot ipynb''' file by '''clicking '''it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;color:#000000;&amp;quot;&amp;gt;Note that each cell will have the output displayed in this file.&amp;lt;/div&amp;gt;&lt;br /&gt;
|- style=&amp;quot;border:0.5pt solid #000000;padding:0.106cm;&amp;quot;&lt;br /&gt;
|| Highlight''' '''the lines:'''import pandas as pd '''&lt;br /&gt;
&lt;br /&gt;
'''import matplotlib.pyplot as '''&lt;br /&gt;
&lt;br /&gt;
'''import seaborn as sns '''&lt;br /&gt;
&lt;br /&gt;
|| These are the necessary libraries to be imported for '''KNN '''regression.&lt;br /&gt;
&lt;br /&gt;
Please remember to Execute the cell by pressing '''Shift and Enter''' to get output.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the line:''' '''&lt;br /&gt;
&lt;br /&gt;
'''iris = load_iris()'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | First, we load the dataset into a variable named '''iris.''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;background-color:#ffffff;&amp;quot;&amp;gt;We are using the &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#ffffff;&amp;quot;&amp;gt;'''Iris dataset'''&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#ffffff;&amp;quot;&amp;gt;, loading it from the &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#ffffff;&amp;quot;&amp;gt;'''sklearn library.'''&amp;lt;/span&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)'''&lt;br /&gt;
&lt;br /&gt;
'''iris_df['target'] = iris.target'''&lt;br /&gt;
&lt;br /&gt;
Press '''Shift '''and''' Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We create a '''DataFrame''' with feature names as '''columns'''.&lt;br /&gt;
&lt;br /&gt;
Then we create a new column '''target''' and assign class labels to it.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''iris_df'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now we display the '''Dataframe''' showing all the feature values and target labels.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''print(&amp;quot;Length of Dataset:&amp;quot;,'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Then we print the dataset length, shape, and the names of all features.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight''' '''the lines:&lt;br /&gt;
&lt;br /&gt;
'''target_feature = 'petal length (cm)''''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and''' Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | After that we store petal length as the target feature name.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''X = iris_df.drop(columns=[target_feature, 'target'],axis=1)'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now we separate the '''features X''' and the '''target variable y'''.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the line:&lt;br /&gt;
&lt;br /&gt;
'''X '''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We see that feature set '''X''' contains all features except the petal length.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the line:&lt;br /&gt;
&lt;br /&gt;
'''y '''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | The target set '''y''' contains the target feature petal length.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''plt.figure(figsize=(10, 6))'''&lt;br /&gt;
&lt;br /&gt;
'''sns.boxplot(data=iris_df.drop(columns=['target']))'''&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now we create a '''boxplot''' to visualize feature distributions '''before scaling'''.&lt;br /&gt;
&lt;br /&gt;
The boxplot shows how the features vary, their range, and any unusual values.&lt;br /&gt;
|- style=&amp;quot;border:0.5pt solid #000000;padding:0.106cm;&amp;quot;&lt;br /&gt;
|| Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''scaler = StandardScaler()'''&lt;br /&gt;
&lt;br /&gt;
'''X_scaled = scaler.fit_transform(X) '''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
|| Next, we apply Standard Scaling to normalize features using '''StandardScaler.'''&lt;br /&gt;
&lt;br /&gt;
'''X underscore scaled''' is the transformed data.&lt;br /&gt;
&lt;br /&gt;
It contains the data with '''mean 0''' and '''standard deviation 1'''.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''plt.figure(figsize=(10, 6))'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Then, we plot a '''boxplot''' to show feature distributions '''after scaling.'''&lt;br /&gt;
&lt;br /&gt;
This helps us visualize how '''standardization''' affects the data.&lt;br /&gt;
&lt;br /&gt;
We can observe in the output that all features are scaled to a mean of 0.&lt;br /&gt;
&lt;br /&gt;
The standard deviation of all the features is now 1.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Next, we split the data into '''training''' and '''testing sets'''. &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:''' '''&lt;br /&gt;
&lt;br /&gt;
'''mse_scores = []'''&lt;br /&gt;
&lt;br /&gt;
'''K_range = range(1, 15)'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We use the '''Elbow''' method to help identify the optimal number of neighbors.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''plt.figure(figsize=(10, 6))'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now, we visualize the '''Elbow''' method for KNN regression using a plot.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the output:&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We can observe that the error decreases initially and then increases after a point.&lt;br /&gt;
&lt;br /&gt;
In the plot, the lowest '''MSE''' value appears to be at '''K''' '''equals''' '''5 '''and '''2.'''&lt;br /&gt;
&lt;br /&gt;
So, we infer that the model performs best at '''K equals 5 '''or '''2.'''&lt;br /&gt;
&lt;br /&gt;
After '''K''' equals '''5''', the '''MSE''' increases, suggesting no further improvement.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''knn = KNeighborsRegressor(n_neighbors=5) '''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Then we initialize the '''KNN Regressor''' using the '''KNeighborsRegressor '''function.&lt;br /&gt;
&lt;br /&gt;
Our ideal '''K '''value is '''5''', so we initialize '''KNN regressor''' with''' 5''' neighbors.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''knn.fit(X_train, y_train)'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Further, we train the '''KNN regressor '''using the '''fit''' method on train data.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''y_train_pred = knn.predict(X_train)'''&lt;br /&gt;
&lt;br /&gt;
Press '''Shift''' and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now, we predict labels for the '''training set.''' &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''training_mse = mean_squared_error(y_train, y_train_pred)'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We then calculate the '''Mean Squared Error '''for the training set.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''def adjusted_r2_score(y_true, y_pred, n, p):'''&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Then, we define the function for '''Adjusted R squared score '''for regression.&lt;br /&gt;
&lt;br /&gt;
It adjusts for the number of predictors.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''n_train = X_train.shape[0]'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | The '''training underscore adj underscore r2''' computes the adjusted R square score.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We print the '''MSE''' and '''Adjusted R squared score''' of the training set.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the output:&lt;br /&gt;
&lt;br /&gt;
'''Training Mean Squared Error: 0.079'''&lt;br /&gt;
&lt;br /&gt;
'''Training Adjusted R^2 Score: 0.973'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Training '''MSE''' of '''0.079''' indicates the model has low error.&lt;br /&gt;
&lt;br /&gt;
It implies good performance.&lt;br /&gt;
&lt;br /&gt;
The training '''Adjusted R squared score''' is '''0.973.''' &lt;br /&gt;
&lt;br /&gt;
It means the model does a great job of predicting the data accurately.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''y_pred&amp;amp;nbsp;= knn.predict(X_test) '''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Let us predict '''labels''' for the '''test set'''. &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''print(comparison_df) '''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We compare '''actual''' and '''predicted values''' to assess model accuracy.&lt;br /&gt;
&lt;br /&gt;
This helps evaluate how well the model generalizes to new data.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''plt.figure(figsize=(6, 4))'''&lt;br /&gt;
&lt;br /&gt;
'''sns.scatterplot(x=y_test, y=y_pred)'''&lt;br /&gt;
&lt;br /&gt;
'''plt.show()'''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now, we plot a scatter plot which compares '''actual vs. predicted values.'''&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | '''Show the output:'''&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We can observe in the output that most points align with the red dashed line.&lt;br /&gt;
&lt;br /&gt;
The red dashed line represents a perfect prediction match.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''test_mse = mean_squared_error(y_test, y_pred)'''&lt;br /&gt;
&lt;br /&gt;
Press''' Shift '''and '''Enter'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Now we calculate '''Mean Squared Error''' of the regression model for the test set.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''n_test = X_test.shape[0]'''&lt;br /&gt;
&lt;br /&gt;
'''p_test = X_test.shape[1]'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Then we calculate the '''Adjusted R squared score''' for the test set.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the lines:&lt;br /&gt;
&lt;br /&gt;
'''print(&amp;quot;Test Mean Squared Error:&amp;quot;, format(test_mse, &amp;quot;.3f&amp;quot;)) '''&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | We print the '''MSE '''and '''Adjusted R square score '''of the '''test set'''.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Highlight the output lines:&lt;br /&gt;
&lt;br /&gt;
'''Test Mean Squared Error: 0.105'''&lt;br /&gt;
&lt;br /&gt;
'''Test Adjusted R² Score: 0.964'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | The test '''MSE''' score indicates the model has low error on test data.&lt;br /&gt;
&lt;br /&gt;
It implies good generalization on test data.&lt;br /&gt;
&lt;br /&gt;
We get the '''adjusted R squared score '''of '''0.964 '''for test data.&lt;br /&gt;
&lt;br /&gt;
The model fits the test data well, explaining '''96.4 percent''' of its '''variance'''.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''Summary'''&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | This brings us to the end of the tutorial. Let us summarize.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we have learnt about&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Distance metrics for nearest neighbor identification. &amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Applying '''KNN''' regression to predict the petal length of '''iris''' flowers.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Evaluation using '''MSE''' and '''Adjusted R squared score.'''&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''Assignment'''&lt;br /&gt;
&lt;br /&gt;
As an assignment, please do the following:&lt;br /&gt;
&lt;br /&gt;
| style=&amp;quot;border-top:0.5pt solid #000000;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | As an assignment, please do the following:&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Let’s use Manhattan distance.&amp;lt;/div&amp;gt;&lt;br /&gt;
* &amp;lt;div style=&amp;quot;margin-left:1.27cm;margin-right:0cm;&amp;quot;&amp;gt;Modify '''knn = KNeighborsRegressor(n_neighbors=5, metric='manhattan')'''&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now observe the change in '''MSE''' and '''Adjusted R squared score''' &lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide:''' '''&lt;br /&gt;
&lt;br /&gt;
'''Assignment Solution '''&lt;br /&gt;
&lt;br /&gt;
'''Show Man.JPG'''&lt;br /&gt;
| style=&amp;quot;border-top:0.75pt solid #000000;border-bottom:0.5pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;color:#000000;&amp;quot; | After completing the assignment, the output should match the expected result.&lt;br /&gt;
|- style=&amp;quot;border:0.5pt solid #000000;padding:0.106cm;&amp;quot;&lt;br /&gt;
|| Show Slide:&lt;br /&gt;
&lt;br /&gt;
'''FOSSEE Forum'''&lt;br /&gt;
| style=&amp;quot;color:#000000;&amp;quot; | For any general or technical questions on '''Python for Machine Learning''', visit the '''FOSSEE forum''' and post your question.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:0.75pt solid #000000;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | Show Slide: &lt;br /&gt;
&lt;br /&gt;
'''Thank you'''&lt;br /&gt;
| style=&amp;quot;border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:0.75pt solid #000000;padding:0.106cm;&amp;quot; | &amp;lt;div style=&amp;quot;color:#000000;&amp;quot;&amp;gt;This is '''Harini Theiveegan''',a FOSSEE Summer Fellow 2025, IIT Bombay signing off&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;color:#000000;&amp;quot;&amp;gt;Thanks for joining.&amp;lt;/div&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Nirmala Venkat</name></author>	</entry>

	</feed>