<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wikitechia.top/index.php?action=history&amp;feed=atom&amp;title=Machine_learning_algorithms</id>
	<title>Machine learning algorithms - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wikitechia.top/index.php?action=history&amp;feed=atom&amp;title=Machine_learning_algorithms"/>
	<link rel="alternate" type="text/html" href="http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;action=history"/>
	<updated>2026-10-26T04:39:52Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;diff=1831&amp;oldid=prev</id>
		<title>Root at 18:41, 29 October 2025</title>
		<link rel="alternate" type="text/html" href="http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;diff=1831&amp;oldid=prev"/>
		<updated>2025-10-29T18:41:18Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 19:41, 29 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l22&quot;&gt;Line 22:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 22:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;plt.show()&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;plt.show()&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[File:Python linear regression04.png|none|thumb|531x531px]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[File:Python linear regression04.png|none|thumb|531x531px]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;correlation heatmap&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;correlation heatmap&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l54&quot;&gt;Line 54:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 52:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(X.head())&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(X.head())&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(y.head())&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(y.head())&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;splitting data for training and testing&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;splitting data for training and testing&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l73&quot;&gt;Line 73:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 69:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Training samples: 3500&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Training samples: 3500&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Testing samples: 1500&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Testing samples: 1500&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;training the model&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;training the model&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l520&quot;&gt;Line 520:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 514:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(predictions[:10])&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(predictions[:10])&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l759&quot;&gt;Line 759:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 751:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Both should look very similar, showing that the algorithm has correctly identified the structure of the data.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Both should look very similar, showing that the algorithm has correctly identified the structure of the data.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;&lt;/del&gt;understanding k-means&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== &lt;/ins&gt;understanding k-means &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;K-Means minimizes the &amp;#039;&amp;#039;&amp;#039;intra-cluster variance&amp;#039;&amp;#039;&amp;#039;, meaning that points within the same cluster are as close as possible to each other, and as far as possible from points in other clusters.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;K-Means minimizes the &amp;#039;&amp;#039;&amp;#039;intra-cluster variance&amp;#039;&amp;#039;&amp;#039;, meaning that points within the same cluster are as close as possible to each other, and as far as possible from points in other clusters.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l773&quot;&gt;Line 773:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 764:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;4. Repeat until centroids stabilize.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;4. Repeat until centroids stabilize.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;&lt;/del&gt;choosing k&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== &lt;/ins&gt;choosing k &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Choosing the correct number of clusters (*k*) is essential.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Choosing the correct number of clusters (*k*) is essential.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l842&quot;&gt;Line 842:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 832:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;- &amp;#039;&amp;#039;&amp;#039;48 wines of type 3&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;- &amp;#039;&amp;#039;&amp;#039;48 wines of type 3&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l860&quot;&gt;Line 860:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 851:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(&amp;quot;Target shape:&amp;quot;, y.shape)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(&amp;quot;Target shape:&amp;quot;, y.shape)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;splitting data for training and testing&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;splitting data for training and testing&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l879&quot;&gt;Line 879:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 868:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Now, 70% of the data is used for training and 30% for testing.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Now, 70% of the data is used for training and 30% for testing.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l903&quot;&gt;Line 903:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 891:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Each prediction corresponds to the &amp;#039;&amp;#039;&amp;#039;predicted wine type&amp;#039;&amp;#039;&amp;#039; for a test sample.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Each prediction corresponds to the &amp;#039;&amp;#039;&amp;#039;predicted wine type&amp;#039;&amp;#039;&amp;#039; for a test sample.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;evaluating the model&#039;&#039;&#039;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l927&quot;&gt;Line 927:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 916:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;plt.show()&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;plt.show()&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Interpretation:&amp;#039;&amp;#039;&amp;#039;   &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Interpretation:&amp;#039;&amp;#039;&amp;#039;   &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Root</name></author>
	</entry>
	<entry>
		<id>http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;diff=1830&amp;oldid=prev</id>
		<title>Root at 18:37, 29 October 2025</title>
		<link rel="alternate" type="text/html" href="http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;diff=1830&amp;oldid=prev"/>
		<updated>2025-10-29T18:37:27Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;amp;diff=1830&amp;amp;oldid=1823&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Root</name></author>
	</entry>
	<entry>
		<id>http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;diff=1823&amp;oldid=prev</id>
		<title>Root: Created page with &quot; = linear regression = Linear regression helps predict continuous outcomes, such as house prices, based on input features like size, income, or number of rooms.  For this linear regression example, we use a real dataset of &#039;&#039;&#039;U.S. housing data&#039;&#039;&#039; available on https://www.kaggle.com/datasets/vedavyasv/usa-housing  The dataset, called &#039;&#039;&#039;USA Housing&#039;&#039;&#039;, contains information about house prices and their related features such as area population, number of rooms, and average...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wikitechia.top/index.php?title=Machine_learning_algorithms&amp;diff=1823&amp;oldid=prev"/>
		<updated>2025-10-28T08:47:00Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; = linear regression = Linear regression helps predict continuous outcomes, such as house prices, based on input features like size, income, or number of rooms.  For this linear regression example, we use a real dataset of &amp;#039;&amp;#039;&amp;#039;U.S. housing data&amp;#039;&amp;#039;&amp;#039; available on https://www.kaggle.com/datasets/vedavyasv/usa-housing  The dataset, called &amp;#039;&amp;#039;&amp;#039;USA Housing&amp;#039;&amp;#039;&amp;#039;, contains information about house prices and their related features such as area population, number of rooms, and average...&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;
= linear regression =&lt;br /&gt;
Linear regression helps predict continuous outcomes, such as house prices, based on input features like size, income, or number of rooms.&lt;br /&gt;
&lt;br /&gt;
For this linear regression example, we use a real dataset of &amp;#039;&amp;#039;&amp;#039;U.S. housing data&amp;#039;&amp;#039;&amp;#039; available on https://www.kaggle.com/datasets/vedavyasv/usa-housing&lt;br /&gt;
&lt;br /&gt;
The dataset, called &amp;#039;&amp;#039;&amp;#039;USA Housing&amp;#039;&amp;#039;&amp;#039;, contains information about house prices and their related features such as area population, number of rooms, and average income.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import pandas as pd&lt;br /&gt;
import numpy as np&lt;br /&gt;
import matplotlib.pyplot as plt&lt;br /&gt;
import seaborn as sns&lt;br /&gt;
&lt;br /&gt;
# Load the dataset&lt;br /&gt;
houses = pd.read_csv(&amp;quot;USA_Housing.csv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Display the first 20 rows&lt;br /&gt;
print(houses.head(20))&lt;br /&gt;
# General information&lt;br /&gt;
print(houses.info())&amp;lt;/syntaxhighlight&amp;gt;Before modeling, it is useful to visualize distributions and correlations to understand relationships between variables.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;distribution plot&amp;#039;&amp;#039;&amp;#039;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;sns.histplot(houses[&amp;quot;Price&amp;quot;], bins=30, kde=True)&lt;br /&gt;
plt.title(&amp;quot;Distribution of House Prices&amp;quot;)&lt;br /&gt;
plt.show()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:Python linear regression04.png|none|thumb|531x531px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;correlation heatmap&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;heatmap&amp;#039;&amp;#039;&amp;#039; helps identify which features are most correlated with the target variable (`Price`).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
corr_matrix = houses.corr()&lt;br /&gt;
sns.heatmap(corr_matrix, annot=True, cmap=&amp;quot;coolwarm&amp;quot;)&lt;br /&gt;
plt.title(&amp;quot;Correlation Heatmap&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:Python linear regression03.png|none|thumb|522x522px]]&lt;br /&gt;
&lt;br /&gt;
The strongest correlation is typically between &amp;#039;&amp;#039;&amp;#039;Average Area Income&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Price&amp;#039;&amp;#039;&amp;#039;, meaning wealthier areas tend to have more expensive houses.&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;preparing the data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;preparing the data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Before training the model, we split the dataset into &amp;#039;&amp;#039;&amp;#039;features (X)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;target (y)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;# Define features (independent variables)&lt;br /&gt;
X = houses[[&amp;quot;Avg. Area Income&amp;quot;, &amp;quot;Avg. Area House Age&amp;quot;,&lt;br /&gt;
            &amp;quot;Avg. Area Number of Rooms&amp;quot;, &amp;quot;Avg. Area Number of Bedrooms&amp;quot;,&lt;br /&gt;
            &amp;quot;Area Population&amp;quot;]]&lt;br /&gt;
&lt;br /&gt;
# Define target (dependent variable)&lt;br /&gt;
y = houses[&amp;quot;Price&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
print(X.head())&lt;br /&gt;
print(y.head())&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;splitting data for training and testing&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We divide the dataset into &amp;#039;&amp;#039;&amp;#039;training data&amp;#039;&amp;#039;&amp;#039; (used to build the model) and &amp;#039;&amp;#039;&amp;#039;testing data&amp;#039;&amp;#039;&amp;#039; (used to evaluate it).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from sklearn.model_selection import train_test_split&lt;br /&gt;
&lt;br /&gt;
# Split data: 70% for training, 30% for testing&lt;br /&gt;
X_train, X_test, y_train, y_test = train_test_split(&lt;br /&gt;
    X, y, test_size=0.3, random_state=42&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Training samples:&amp;quot;, X_train.shape[0])&lt;br /&gt;
print(&amp;quot;Testing samples:&amp;quot;, X_test.shape[0])&lt;br /&gt;
&lt;br /&gt;
# Training samples: 3500&lt;br /&gt;
# Testing samples: 1500&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;training the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We now create and train a &amp;#039;&amp;#039;&amp;#039;Linear Regression model&amp;#039;&amp;#039;&amp;#039; using scikit-learn.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
# Create model&lt;br /&gt;
model = LinearRegression()&lt;br /&gt;
&lt;br /&gt;
# Train the model&lt;br /&gt;
model.fit(X_train, y_train)&lt;br /&gt;
&lt;br /&gt;
# Display coefficients and intercept&lt;br /&gt;
print(&amp;quot;Intercept:&amp;quot;, model.intercept_)&lt;br /&gt;
print(&amp;quot;Coefficients:&amp;quot;, model.coef_)&lt;br /&gt;
&lt;br /&gt;
# Create a DataFrame for better readability&lt;br /&gt;
coef_df = pd.DataFrame(model.coef_, X.columns, columns=[&amp;quot;Coefficient&amp;quot;])&lt;br /&gt;
print(coef_df)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
                                Coefficient&lt;br /&gt;
Avg. Area Income                  21.625799&lt;br /&gt;
Avg. Area House Age           165590.392746&lt;br /&gt;
Avg. Area Number of Rooms     119827.783390&lt;br /&gt;
Avg. Area Number of Bedrooms    2361.095262&lt;br /&gt;
Area Population                   15.216581&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Each coefficient represents how much the &amp;#039;&amp;#039;&amp;#039;Price&amp;#039;&amp;#039;&amp;#039; changes with one unit of change in the corresponding feature.&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;making predictions&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;making predictions&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can now predict house prices using the test data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Predict on the test set&lt;br /&gt;
predictions = model.predict(X_test)&lt;br /&gt;
&lt;br /&gt;
# Compare actual vs predicted&lt;br /&gt;
comparison = pd.DataFrame({&amp;quot;Actual&amp;quot;: y_test, &amp;quot;Predicted&amp;quot;: predictions})&lt;br /&gt;
print(comparison.head())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
            Actual     Predicted&lt;br /&gt;
1501  1.339096e+06  1.308536e+06&lt;br /&gt;
2586  1.251794e+06  1.237123e+06&lt;br /&gt;
2653  1.340095e+06  1.243836e+06&lt;br /&gt;
1055  1.431508e+06  1.229242e+06&lt;br /&gt;
705   1.042374e+06  1.059353e+06&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;visualizing predictions&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can visualize how close the predictions are to the actual prices. If the points align closely to the diagonal line, the model predictions are accurate.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;scatter plot (real vs predicted)&amp;#039;&amp;#039;&amp;#039;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
plt.scatter(y_test, predictions)&lt;br /&gt;
plt.xlabel(&amp;quot;Actual Prices&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;Predicted Prices&amp;quot;)&lt;br /&gt;
plt.title(&amp;quot;Actual vs Predicted House Prices&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:Python linear regression01.png|none|thumb|500x500px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;histogram of residuals&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Residuals (errors) are the differences between actual and predicted prices.  &lt;br /&gt;
A normal distribution of residuals indicates a good fit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sns.histplot((y_test - predictions), bins=30, kde=True)&lt;br /&gt;
plt.title(&amp;quot;Distribution of Residuals&amp;quot;)&lt;br /&gt;
plt.xlabel(&amp;quot;Prediction Error&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:Python linear regression02.png|none|thumb|495x495px]]&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We use three evaluation metrics to measure model performance:&lt;br /&gt;
&lt;br /&gt;
1. &amp;#039;&amp;#039;&amp;#039;MAE (Mean Absolute Error)&amp;#039;&amp;#039;&amp;#039; average of absolute errors.&lt;br /&gt;
&lt;br /&gt;
2. &amp;#039;&amp;#039;&amp;#039;MSE (Mean Squared Error)&amp;#039;&amp;#039;&amp;#039; average of squared errors.&lt;br /&gt;
&lt;br /&gt;
3. &amp;#039;&amp;#039;&amp;#039;RMSE (Root Mean Squared Error)&amp;#039;&amp;#039;&amp;#039; square root of MSE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn import metrics&lt;br /&gt;
&lt;br /&gt;
mae = metrics.mean_absolute_error(y_test, predictions)&lt;br /&gt;
mse = metrics.mean_squared_error(y_test, predictions)&lt;br /&gt;
rmse = np.sqrt(mse)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Mean Absolute Error (MAE):&amp;quot;, mae)&lt;br /&gt;
print(&amp;quot;Mean Squared Error (MSE):&amp;quot;, mse)&lt;br /&gt;
print(&amp;quot;Root Mean Squared Error (RMSE):&amp;quot;, rmse)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;smaller&amp;#039;&amp;#039;&amp;#039; these values are, the &amp;#039;&amp;#039;&amp;#039;better&amp;#039;&amp;#039;&amp;#039; the model performs.&lt;br /&gt;
&lt;br /&gt;
A well-trained linear regression model for this dataset typically achieves a very low error, indicating good predictive accuracy.&lt;br /&gt;
&lt;br /&gt;
= logistic regression =&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Logistic regression&amp;#039;&amp;#039;&amp;#039; is a &amp;#039;&amp;#039;&amp;#039;supervised learning algorithm&amp;#039;&amp;#039;&amp;#039; used for &amp;#039;&amp;#039;&amp;#039;classification problems&amp;#039;&amp;#039;&amp;#039;, where the target variable is &amp;#039;&amp;#039;&amp;#039;categorical,&amp;#039;&amp;#039;&amp;#039; meaning it can take a limited number of discrete values.&lt;br /&gt;
&lt;br /&gt;
It is used to model the &amp;#039;&amp;#039;&amp;#039;probability&amp;#039;&amp;#039;&amp;#039; that an event occurs based on one or more independent variables.&lt;br /&gt;
&lt;br /&gt;
Typical applications include:  &lt;br /&gt;
&lt;br /&gt;
- Detecting spam vs. non-spam emails    &lt;br /&gt;
&lt;br /&gt;
- Predicting whether a loan applicant will repay or default  &lt;br /&gt;
&lt;br /&gt;
- Determining if a patient has a certain disease or not  &lt;br /&gt;
&lt;br /&gt;
These are examples of &amp;#039;&amp;#039;&amp;#039;binary classifications&amp;#039;&amp;#039;&amp;#039;, where the result can be either &amp;#039;&amp;#039;&amp;#039;1 (yes)&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;0 (no)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== what is a logistic function ==&lt;br /&gt;
The logistic function produces an &amp;#039;&amp;#039;&amp;#039;S-shaped curve&amp;#039;&amp;#039;&amp;#039; that converts any real-valued number into a value between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
If the output is ≥ 0.5, we classify it as 1 (event occurs), otherwise as 0 (event does not occur).&lt;br /&gt;
&lt;br /&gt;
This makes it ideal for probabilistic models in classification tasks.  &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;confusion matrix&amp;#039;&amp;#039;&amp;#039;  &lt;br /&gt;
&lt;br /&gt;
A confusion matrix is a table used to evaluate classification performance.  &lt;br /&gt;
&lt;br /&gt;
It compares &amp;#039;&amp;#039;&amp;#039;predicted values&amp;#039;&amp;#039;&amp;#039; with &amp;#039;&amp;#039;&amp;#039;actual values&amp;#039;&amp;#039;&amp;#039; and shows:  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;True positives (TP):&amp;#039;&amp;#039;&amp;#039; correctly predicted positive cases  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;True negatives (TN):&amp;#039;&amp;#039;&amp;#039; correctly predicted negative cases   &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;False positives (FP):&amp;#039;&amp;#039;&amp;#039; predicted positive but actually negative  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;False negatives (FN):&amp;#039;&amp;#039;&amp;#039; predicted negative but actually positive  &lt;br /&gt;
&lt;br /&gt;
From the confusion matrix, we can calculate:  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Accuracy&amp;#039;&amp;#039;&amp;#039; = (TP + TN) / Total  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Error rate&amp;#039;&amp;#039;&amp;#039; = (FP + FN) / Total  &lt;br /&gt;
&lt;br /&gt;
For example, in a spam detection test with 200 emails:  &lt;br /&gt;
&lt;br /&gt;
TP = 30  &lt;br /&gt;
&lt;br /&gt;
- TN = 150  &lt;br /&gt;
&lt;br /&gt;
- FP = 5  &lt;br /&gt;
&lt;br /&gt;
- FN = 15  &lt;br /&gt;
&lt;br /&gt;
Then:&lt;br /&gt;
&lt;br /&gt;
Accuracy = (30 + 150) / 200 = 0.9 → 90%&lt;br /&gt;
&lt;br /&gt;
Error rate = (5 + 15) / 200 = 0.1 → 10%&lt;br /&gt;
&lt;br /&gt;
== example of logistic regression with the Titanic dataset ==&lt;br /&gt;
We will now create a logistic regression model using the &amp;#039;&amp;#039;&amp;#039;Titanic dataset&amp;#039;&amp;#039;&amp;#039; from [https://www.kaggle.com Kaggle].&lt;br /&gt;
&lt;br /&gt;
This dataset contains information about Titanic passengers, including age, class, gender, and whether they survived.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;importing required libraries&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pandas as pd&lt;br /&gt;
import numpy as np&lt;br /&gt;
import matplotlib.pyplot as plt&lt;br /&gt;
import seaborn as sns&lt;br /&gt;
&lt;br /&gt;
# Load the training dataset&lt;br /&gt;
train = pd.read_csv(&amp;quot;train.csv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Display the first rows&lt;br /&gt;
print(train.head())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;exploring the data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can check for missing values and visualize them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Check for missing values&lt;br /&gt;
print(train.isnull().sum())&lt;br /&gt;
&lt;br /&gt;
# Visualize missing data&lt;br /&gt;
sns.heatmap(train.isnull(), cbar=False, yticklabels=False)&lt;br /&gt;
plt.title(&amp;quot;Missing values heatmap&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Columns like &amp;#039;&amp;#039;&amp;#039;Age&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Cabin&amp;#039;&amp;#039;&amp;#039; often have missing values. Cabin usually has too many nulls and can be removed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;analyzing survival data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can analyze the number of survivors and non-survivors.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sns.countplot(x=&amp;quot;Survived&amp;quot;, data=train)&lt;br /&gt;
plt.title(&amp;quot;Count of survivors vs non-survivors&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&lt;br /&gt;
# Survival by gender&lt;br /&gt;
sns.countplot(x=&amp;quot;Survived&amp;quot;, hue=&amp;quot;Sex&amp;quot;, data=train)&lt;br /&gt;
plt.title(&amp;quot;Survival rate by gender&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&lt;br /&gt;
# Survival by class&lt;br /&gt;
sns.countplot(x=&amp;quot;Survived&amp;quot;, hue=&amp;quot;Pclass&amp;quot;, data=train)&lt;br /&gt;
plt.title(&amp;quot;Survival rate by passenger class&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We observe that:&lt;br /&gt;
&lt;br /&gt;
- Most &amp;#039;&amp;#039;&amp;#039;non-survivors&amp;#039;&amp;#039;&amp;#039; were &amp;#039;&amp;#039;&amp;#039;men&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
- Most &amp;#039;&amp;#039;&amp;#039;survivors&amp;#039;&amp;#039;&amp;#039; were &amp;#039;&amp;#039;&amp;#039;women&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
- Passengers in &amp;#039;&amp;#039;&amp;#039;first class&amp;#039;&amp;#039;&amp;#039; had a higher survival rate than those in third class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;age distribution&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can visualize the distribution of passengers&amp;#039; ages.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sns.histplot(train[&amp;quot;Age&amp;quot;].dropna(), bins=30, kde=False)&lt;br /&gt;
plt.title(&amp;quot;Distribution of passenger ages&amp;quot;)&lt;br /&gt;
plt.xlabel(&amp;quot;Age&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;Count&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most passengers were between &amp;#039;&amp;#039;&amp;#039;20 and 40 years old&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;cleaning the data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Before training the model, we must clean and prepare the dataset.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Fill missing ages based on passenger class (Pclass)&lt;br /&gt;
def fill_age(cols):&lt;br /&gt;
    age = cols[0]&lt;br /&gt;
    pclass = cols[1]&lt;br /&gt;
    if pd.isnull(age):&lt;br /&gt;
        if pclass == 1:&lt;br /&gt;
            return 38&lt;br /&gt;
        elif pclass == 2:&lt;br /&gt;
            return 30&lt;br /&gt;
        else:&lt;br /&gt;
            return 25&lt;br /&gt;
    else:&lt;br /&gt;
        return age&lt;br /&gt;
&lt;br /&gt;
train[&amp;quot;Age&amp;quot;] = train[[&amp;quot;Age&amp;quot;, &amp;quot;Pclass&amp;quot;]].apply(fill_age, axis=1)&lt;br /&gt;
&lt;br /&gt;
# Drop &amp;#039;Cabin&amp;#039; due to too many missing values&lt;br /&gt;
train.drop(&amp;quot;Cabin&amp;quot;, axis=1, inplace=True)&lt;br /&gt;
&lt;br /&gt;
# Drop irrelevant columns&lt;br /&gt;
train.drop([&amp;quot;Name&amp;quot;, &amp;quot;Ticket&amp;quot;, &amp;quot;PassengerId&amp;quot;], axis=1, inplace=True)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;encoding categorical variables&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We need to convert text columns into numeric values.  &lt;br /&gt;
For example, we can convert gender into 1 (male) and 0 (female).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Convert &amp;#039;Sex&amp;#039; to numeric&lt;br /&gt;
sex = pd.get_dummies(train[&amp;quot;Sex&amp;quot;], drop_first=True)&lt;br /&gt;
train = pd.concat([train, sex], axis=1)&lt;br /&gt;
train.drop(&amp;quot;Sex&amp;quot;, axis=1, inplace=True)&lt;br /&gt;
&lt;br /&gt;
# Convert &amp;#039;Embarked&amp;#039; to numeric&lt;br /&gt;
embark = pd.get_dummies(train[&amp;quot;Embarked&amp;quot;], drop_first=True)&lt;br /&gt;
train = pd.concat([train, embark], axis=1)&lt;br /&gt;
train.drop(&amp;quot;Embarked&amp;quot;, axis=1, inplace=True)&lt;br /&gt;
&lt;br /&gt;
print(train.head())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the dataset is completely numeric and ready for modeling.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;splitting the data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We separate our data into &amp;#039;&amp;#039;&amp;#039;features (X)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;target (y)&amp;#039;&amp;#039;&amp;#039;, then split it into training and testing sets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn.model_selection import train_test_split&lt;br /&gt;
&lt;br /&gt;
X = train.drop(&amp;quot;Survived&amp;quot;, axis=1)&lt;br /&gt;
y = train[&amp;quot;Survived&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
X_train, X_test, y_train, y_test = train_test_split(&lt;br /&gt;
    X, y, test_size=0.3, random_state=45&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Training samples:&amp;quot;, X_train.shape[0])&lt;br /&gt;
print(&amp;quot;Testing samples:&amp;quot;, X_test.shape[0])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;training the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;training the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We create and train a &amp;#039;&amp;#039;&amp;#039;logistic regression model&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn.linear_model import LogisticRegression&lt;br /&gt;
&lt;br /&gt;
# Initialize and train model&lt;br /&gt;
log_model = LogisticRegression(max_iter=1000)&lt;br /&gt;
log_model.fit(X_train, y_train)&lt;br /&gt;
&lt;br /&gt;
# Make predictions&lt;br /&gt;
predictions = log_model.predict(X_test)&lt;br /&gt;
print(predictions[:10])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can now evaluate the model using various metrics.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn.metrics import classification_report, confusion_matrix&lt;br /&gt;
&lt;br /&gt;
# Classification report&lt;br /&gt;
print(classification_report(y_test, predictions))&lt;br /&gt;
&lt;br /&gt;
# Confusion matrix&lt;br /&gt;
cm = confusion_matrix(y_test, predictions)&lt;br /&gt;
print(cm)&lt;br /&gt;
&lt;br /&gt;
sns.heatmap(cm, annot=True, fmt=&amp;#039;d&amp;#039;, cmap=&amp;#039;Blues&amp;#039;)&lt;br /&gt;
plt.title(&amp;quot;Confusion matrix&amp;quot;)&lt;br /&gt;
plt.xlabel(&amp;quot;Predicted&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;Actual&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Typical output:&lt;br /&gt;
&lt;br /&gt;
Precision for class 0 (did not survive): 0.88&lt;br /&gt;
Precision for class 1 (survived): 0.75&lt;br /&gt;
&lt;br /&gt;
This means the model correctly predicts non-survivors about &amp;#039;&amp;#039;&amp;#039;88%&amp;#039;&amp;#039;&amp;#039; of the time and survivors about &amp;#039;&amp;#039;&amp;#039;75%&amp;#039;&amp;#039;&amp;#039; of the time.&lt;br /&gt;
&lt;br /&gt;
= K-Nearest Neighbors (KNN) =&lt;br /&gt;
K-Nearest Neighbors (KNN) is a &amp;#039;&amp;#039;&amp;#039;supervised classification algorithm&amp;#039;&amp;#039;&amp;#039; used to estimate the probability that an element *X* belongs to a class *C*, based on its proximity to other known examples in the dataset. It is simple, intuitive, and works with any number of classes.&lt;br /&gt;
&lt;br /&gt;
The idea is to classify a new element by looking at the &amp;#039;&amp;#039;&amp;#039;K nearest points&amp;#039;&amp;#039;&amp;#039; (neighbors) in the training data and assigning it the class most common among them.&lt;br /&gt;
&lt;br /&gt;
== how it works ==&lt;br /&gt;
Imagine we want to determine the class of a &amp;#039;&amp;#039;&amp;#039;green circle&amp;#039;&amp;#039;&amp;#039; based on nearby red triangles and blue squares. &lt;br /&gt;
&lt;br /&gt;
The KNN algorithm:&lt;br /&gt;
&lt;br /&gt;
1. Calculates the &amp;#039;&amp;#039;&amp;#039;distance&amp;#039;&amp;#039;&amp;#039; between the green circle and all other points.&lt;br /&gt;
&lt;br /&gt;
2. Sorts them from &amp;#039;&amp;#039;&amp;#039;closest to farthest&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
3. Looks at the &amp;#039;&amp;#039;&amp;#039;K nearest&amp;#039;&amp;#039;&amp;#039; data points.&lt;br /&gt;
&lt;br /&gt;
4. Assigns the green circle to the class that appears most frequently among those neighbors.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
- If &amp;#039;&amp;#039;&amp;#039;K = 3&amp;#039;&amp;#039;&amp;#039; and two of the closest points are red triangles and one is a blue square, the circle is classified as a &amp;#039;&amp;#039;&amp;#039;red triangle&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
- If &amp;#039;&amp;#039;&amp;#039;K = 5&amp;#039;&amp;#039;&amp;#039; and three closest points are blue squares, then the circle is classified as a &amp;#039;&amp;#039;&amp;#039;blue square&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Choosing the right value for &amp;#039;&amp;#039;&amp;#039;K&amp;#039;&amp;#039;&amp;#039; is crucial, too small can lead to noise sensitivity, too large can oversimplify the classification.&lt;br /&gt;
&lt;br /&gt;
== example with vehicle type prediction ==&lt;br /&gt;
We will demonstrate KNN using the &amp;#039;&amp;#039;&amp;#039;Vehicle Silhouettes&amp;#039;&amp;#039;&amp;#039; dataset, which predicts the type of vehicle based on its physical characteristics.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pandas as pd&lt;br /&gt;
import numpy as np&lt;br /&gt;
import matplotlib.pyplot as plt&lt;br /&gt;
import seaborn as sns&lt;br /&gt;
&lt;br /&gt;
# Load dataset&lt;br /&gt;
data = pd.read_csv(&amp;quot;vehicles.csv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Display first rows&lt;br /&gt;
print(data.head())&lt;br /&gt;
&lt;br /&gt;
# Show the unique vehicle classes&lt;br /&gt;
print(data[&amp;quot;Vehicle Class&amp;quot;].unique())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The dataset contains various vehicle features and a target column called &amp;#039;&amp;#039;&amp;#039;Vehicle Class&amp;#039;&amp;#039;&amp;#039;, which indicates the type of vehicle (e.g., Bus, Opel, Saab, Van).&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;preparing the data&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We define the &amp;#039;&amp;#039;&amp;#039;features (X)&amp;#039;&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;&amp;#039;target variable (y)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Define features (all columns except &amp;#039;Vehicle Class&amp;#039;)&lt;br /&gt;
X = data.drop(&amp;quot;Vehicle Class&amp;quot;, axis=1)&lt;br /&gt;
&lt;br /&gt;
# Define target&lt;br /&gt;
y = data[&amp;quot;Vehicle Class&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Features shape:&amp;quot;, X.shape)&lt;br /&gt;
print(&amp;quot;Target shape:&amp;quot;, y.shape)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;splitting data into training and testing&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We divide our dataset into two parts:  &lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Training data&amp;#039;&amp;#039;&amp;#039; to train the model  &lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Testing data&amp;#039;&amp;#039;&amp;#039; to evaluate its performance  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn.model_selection import train_test_split&lt;br /&gt;
&lt;br /&gt;
X_train, X_test, y_train, y_test = train_test_split(&lt;br /&gt;
    X, y, test_size=0.3, random_state=45&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Training samples:&amp;quot;, X_train.shape[0])&lt;br /&gt;
print(&amp;quot;Testing samples:&amp;quot;, X_test.shape[0])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;training the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We train a &amp;#039;&amp;#039;&amp;#039;KNN classifier&amp;#039;&amp;#039;&amp;#039; with an initial K value of 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn.neighbors import KNeighborsClassifier&lt;br /&gt;
&lt;br /&gt;
# Create model with K=1&lt;br /&gt;
knn = KNeighborsClassifier(n_neighbors=1)&lt;br /&gt;
&lt;br /&gt;
# Train model&lt;br /&gt;
knn.fit(X_train, y_train)&lt;br /&gt;
&lt;br /&gt;
# Make predictions&lt;br /&gt;
predictions = knn.predict(X_test)&lt;br /&gt;
&lt;br /&gt;
# Display sample predictions&lt;br /&gt;
print(predictions[:10])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;evaluating the model&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To measure model performance, we use a &amp;#039;&amp;#039;&amp;#039;confusion matrix&amp;#039;&amp;#039;&amp;#039; and a &amp;#039;&amp;#039;&amp;#039;classification report&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from sklearn.metrics import confusion_matrix, classification_report&lt;br /&gt;
&lt;br /&gt;
# Confusion matrix&lt;br /&gt;
cm = confusion_matrix(y_test, predictions)&lt;br /&gt;
print(&amp;quot;Confusion matrix:&amp;quot;)&lt;br /&gt;
print(cm)&lt;br /&gt;
&lt;br /&gt;
sns.heatmap(cm, annot=True, cmap=&amp;quot;Blues&amp;quot;, fmt=&amp;quot;d&amp;quot;)&lt;br /&gt;
plt.title(&amp;quot;Confusion matrix for K=1&amp;quot;)&lt;br /&gt;
plt.xlabel(&amp;quot;Predicted&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;Actual&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&lt;br /&gt;
# Classification report&lt;br /&gt;
print(&amp;quot;Classification report:&amp;quot;)&lt;br /&gt;
print(classification_report(y_test, predictions))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This report shows precision, recall, and F1-score for each vehicle class.  &lt;br /&gt;
&lt;br /&gt;
For example, the model might correctly predict:  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Bus:&amp;#039;&amp;#039;&amp;#039; 82% accuracy  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Opel:&amp;#039;&amp;#039;&amp;#039; 48% accuracy  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Saab:&amp;#039;&amp;#039;&amp;#039; 38% accuracy  &lt;br /&gt;
&lt;br /&gt;
- &amp;#039;&amp;#039;&amp;#039;Van:&amp;#039;&amp;#039;&amp;#039; 78% accuracy  &lt;br /&gt;
&lt;br /&gt;
These results can improve by finding a better value for &amp;#039;&amp;#039;&amp;#039;K&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;optimizing the K value&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We can test multiple K values (from 1 to 30) to find the one with the lowest error rate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
error_rate = []&lt;br /&gt;
&lt;br /&gt;
# Loop through K values from 1 to 30&lt;br /&gt;
for i in range(1, 31):&lt;br /&gt;
    knn = KNeighborsClassifier(n_neighbors=i)&lt;br /&gt;
    knn.fit(X_train, y_train)&lt;br /&gt;
    pred_i = knn.predict(X_test)&lt;br /&gt;
    error_rate.append(np.mean(pred_i != y_test))&lt;br /&gt;
&lt;br /&gt;
# Plot error rate vs K&lt;br /&gt;
plt.figure(figsize=(10, 5))&lt;br /&gt;
plt.plot(range(1, 31), error_rate, color=&amp;quot;green&amp;quot;, marker=&amp;quot;o&amp;quot;,&lt;br /&gt;
         markerfacecolor=&amp;quot;red&amp;quot;, markersize=8)&lt;br /&gt;
plt.title(&amp;quot;Error rate vs K value&amp;quot;)&lt;br /&gt;
plt.xlabel(&amp;quot;K&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;Error rate&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;optimal K&amp;#039;&amp;#039;&amp;#039; corresponds to the lowest point on this graph, typically, &amp;#039;&amp;#039;&amp;#039;K = 3&amp;#039;&amp;#039;&amp;#039; in this dataset.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;retraining with optimal K&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
We now retrain the model with the optimal &amp;#039;&amp;#039;&amp;#039;K = 3&amp;#039;&amp;#039;&amp;#039; and re-evaluate its performance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Train with K=3&lt;br /&gt;
knn_optimal = KNeighborsClassifier(n_neighbors=3)&lt;br /&gt;
knn_optimal.fit(X_train, y_train)&lt;br /&gt;
&lt;br /&gt;
# Make predictions&lt;br /&gt;
predictions_opt = knn_optimal.predict(X_test)&lt;br /&gt;
&lt;br /&gt;
# Evaluate&lt;br /&gt;
cm_opt = confusion_matrix(y_test, predictions_opt)&lt;br /&gt;
print(&amp;quot;Confusion matrix (K=3):&amp;quot;)&lt;br /&gt;
print(cm_opt)&lt;br /&gt;
&lt;br /&gt;
sns.heatmap(cm_opt, annot=True, cmap=&amp;quot;Blues&amp;quot;, fmt=&amp;quot;d&amp;quot;)&lt;br /&gt;
plt.title(&amp;quot;Confusion matrix for K=3 (optimized model)&amp;quot;)&lt;br /&gt;
plt.xlabel(&amp;quot;Predicted&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;Actual&amp;quot;)&lt;br /&gt;
plt.show()&lt;br /&gt;
&lt;br /&gt;
# Classification report&lt;br /&gt;
print(&amp;quot;Classification report for K=3:&amp;quot;)&lt;br /&gt;
print(classification_report(y_test, predictions_opt))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After optimization, the accuracy of all vehicle classes improves, and the error rate decreases significantly.&lt;br /&gt;
&lt;br /&gt;
== conclusion ==&lt;br /&gt;
K-Nearest Neighbors (KNN) is a simple yet effective algorithm for classification.&lt;br /&gt;
&lt;br /&gt;
It makes predictions based on proximity between data points, using a distance metric such as Euclidean distance.&lt;br /&gt;
&lt;br /&gt;
The key points to remember:  &lt;br /&gt;
&lt;br /&gt;
- It is &amp;#039;&amp;#039;&amp;#039;non-parametric,&amp;#039;&amp;#039;&amp;#039; no assumptions about the data distribution.  &lt;br /&gt;
&lt;br /&gt;
- It is &amp;#039;&amp;#039;&amp;#039;instance-based,&amp;#039;&amp;#039;&amp;#039; stores all training data for comparison.  &lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;choice of K&amp;#039;&amp;#039;&amp;#039; greatly affects accuracy:  &lt;br /&gt;
  - Small K → sensitive to noise.  &lt;br /&gt;
  - Large K → oversimplifies classification.&lt;br /&gt;
&lt;br /&gt;
By tuning &amp;#039;&amp;#039;&amp;#039;K&amp;#039;&amp;#039;&amp;#039; and testing error rates, we can build a reliable model for classifying objects like vehicles, customers, or any feature-based categories.&lt;br /&gt;
[[Category:KC AI technical aspects]]&lt;/div&gt;</summary>
		<author><name>Root</name></author>
	</entry>
</feed>