<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python &#8211; Ghazi Hudeihed</title>
	<atom:link href="https://ghazi.hudeihed.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://ghazi.hudeihed.com</link>
	<description>About Me</description>
	<lastBuildDate>Mon, 10 Jun 2024 02:51:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://ghazi.hudeihed.com/wp-content/uploads/2021/03/cropped-outline-32x32.png</url>
	<title>Python &#8211; Ghazi Hudeihed</title>
	<link>https://ghazi.hudeihed.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Mitigating Overfitting with Ridge Regression: A Step-by-Step Guide Using Polynomial Regression</title>
		<link>https://ghazi.hudeihed.com/2024/06/01/mitigating-overfitting-with-ridge-regression-a-step-by-step-guide-using-polynomial-regression/</link>
		
		<dc:creator><![CDATA[Ghazi Hudeihed]]></dc:creator>
		<pubDate>Sat, 01 Jun 2024 23:05:40 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[Data Analysis]]></category>
		<category><![CDATA[Data Science]]></category>
		<category><![CDATA[L2 Regularization]]></category>
		<category><![CDATA[Model Generalization]]></category>
		<category><![CDATA[Overfitting]]></category>
		<category><![CDATA[Polynomial Regression]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ridge Regression]]></category>
		<category><![CDATA[scikit-learn]]></category>
		<guid isPermaLink="false">https://ghazi.hudeihed.com/?p=1389</guid>

					<description><![CDATA[Introduction One of the simplest ways to simulate overfitting is to use polynomial regression on a small dataset. We can fit a high-degree polynomial to a small dataset, which will lead to overfitting. Then we can see how regularization techniques like Ridge Regression (L2 regularization) help to mitigate the overfitting. Step 1: Generate a Small...<p><a class="read-more" href="https://ghazi.hudeihed.com/2024/06/01/mitigating-overfitting-with-ridge-regression-a-step-by-step-guide-using-polynomial-regression/">Read More</a></p>]]></description>
										<content:encoded><![CDATA[
<h3 class="wp-block-heading">Introduction</h3>



<p>One of the simplest ways to simulate overfitting is to use polynomial regression on a small dataset. We can fit a high-degree polynomial to a small dataset, which will lead to overfitting. Then we can see how regularization techniques like Ridge Regression (L2 regularization) help to mitigate the overfitting.</p>



<h3 class="wp-block-heading">Step 1: Generate a Small Dataset</h3>



<p>First, let&#8217;s generate a small dataset and train a neural network that will likely overfit the data.</p>



<p>We will generate a small dataset where <code>y = X^2 + noise</code>. This is a simple quadratic relationship with some added noise.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="import numpy as np
import matplotlib.pyplot as plt

# Generate a small dataset
np.random.seed(42)
X = np.linspace(-1, 1, 60)
y = X**2 + np.random.randn(60) * 2

print(f'X Shape: {X.shape}')
print(f'y Shape: {y.shape}\n')

print(f'X: {X}')
print(f'y: {y}')" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">import</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">numpy</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">as</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">np</span></span>
<span class="line"><span style="color: #8FBCBB">import</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">matplotlib</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">pyplot</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">as</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">plt</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF"># </span><span style="color: #8FBCBB">Generate</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">a</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">small</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">dataset</span></span>
<span class="line"><span style="color: #8FBCBB">np</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">random</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">seed</span><span style="color: #D8DEE9FF">(42)</span></span>
<span class="line"><span style="color: #8FBCBB">X</span><span style="color: #D8DEE9FF"> = </span><span style="color: #8FBCBB">np</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">linspace</span><span style="color: #D8DEE9FF">(-1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> 1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> 60)</span></span>
<span class="line"><span style="color: #8FBCBB">y</span><span style="color: #D8DEE9FF"> = </span><span style="color: #8FBCBB">X</span><span style="color: #81A1C1">**</span><span style="color: #D8DEE9FF">2 + </span><span style="color: #8FBCBB">np</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">random</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">randn</span><span style="color: #D8DEE9FF">(60) </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9FF"> 2</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8FBCBB">print</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">f</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">X Shape: {X.shape}</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #8FBCBB">print</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">f</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">y Shape: {y.shape}</span><span style="color: #EBCB8B">\n</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8FBCBB">print</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">f</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">X: {X}</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #8FBCBB">print</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">f</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">y: {y}</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span></code></pre></div>


<pre class="wp-block-code"><span><code class="hljs">X Shape: (60,)
y Shape: (60,)

X: &#91;-1.         -0.96610169 -0.93220339 -0.89830508 -0.86440678 -0.83050847
 -0.79661017 -0.76271186 -0.72881356 -0.69491525 -0.66101695 -0.62711864
 -0.59322034 -0.55932203 -0.52542373 -0.49152542 -0.45762712 -0.42372881
 -0.38983051 -0.3559322  -0.3220339  -0.28813559 -0.25423729 -0.22033898
 -0.18644068 -0.15254237 -0.11864407 -0.08474576 -0.05084746 -0.01694915
  0.01694915  0.05084746  0.08474576  0.11864407  0.15254237  0.18644068
  0.22033898  0.25423729  0.28813559  0.3220339   0.3559322   0.38983051
  0.42372881  0.45762712  0.49152542  0.52542373  0.55932203  0.59322034
  0.62711864  0.66101695  0.69491525  0.72881356  0.76271186  0.79661017
  0.83050847  0.86440678  0.89830508  0.93220339  0.96610169  1.        ]
y: &#91; 1.99342831  0.65682388  2.16438024  3.85301174  0.27889233  0.22147041
  3.79301339  2.11659885 -0.40777957  1.5680273  -0.48989198 -0.53818171
  0.83583491 -3.51371935 -3.17376557 -0.88297782 -1.81623966  0.80804077
 -1.66408033 -2.69791967  3.03500337 -0.36853048  0.19969301 -2.8009471
 -1.05400532  0.24511435 -2.28791074  0.75857788 -1.19869192 -0.58310023
 -1.20312595  3.70714183 -0.01981261 -2.10134544  1.668359   -2.40692717
  0.46627646 -3.85470365 -2.57334998  0.4974283   1.60362089  0.49470439
 -0.05175046 -0.39278481 -2.71544674 -1.16361832 -0.6084364   2.46615482
  1.08051437 -3.0891369   1.13107515 -0.23899536 -0.77211461  1.85794034
  2.75174337  2.60975932 -0.87148302  0.25057841  1.59587935  2.95109025]</code></span></pre>


<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# Plot the dataset
plt.scatter(X, y, color='blue')
plt.title('Small Dataset')
plt.xlabel('X')
plt.ylabel('y')
plt.show()" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #616E88"># Plot the dataset</span></span>
<span class="line"><span style="color: #88C0D0">plt.scatter(X,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">y,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">color=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">blue</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.title(</span><span style="color: #88C0D0">&#39;Small Dataset&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.xlabel(</span><span style="color: #88C0D0">&#39;X&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.ylabel(</span><span style="color: #88C0D0">&#39;y&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.show</span><span style="color: #ECEFF4">()</span></span></code></pre></div>



<figure class="wp-block-image size-full"><img decoding="async" width="565" height="455" loading="lazy" src="https://ghazi.hudeihed.com/wp-content/uploads/2024/06/small_dataset-2.png" alt="" class="wp-image-1429" srcset="https://ghazi.hudeihed.com/wp-content/uploads/2024/06/small_dataset-2.png 565w, https://ghazi.hudeihed.com/wp-content/uploads/2024/06/small_dataset-2-300x242.png 300w" sizes="auto, (max-width: 565px) 100vw, 565px" /></figure>



<h3 class="wp-block-heading">Step 2: Fit a High-Degree Polynomial</h3>



<h4 class="wp-block-heading">Creating Polynomial Features</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from sklearn.preprocessing import PolynomialFeatures

# Create polynomial features
poly_features = PolynomialFeatures(degree=15)
X_poly = poly_features.fit_transform(X.reshape(-1, 1))
print(f'X_poly Shape: {X_poly.shape}')" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">from</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">sklearn.preprocessing</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">import</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">PolynomialFeatures</span></span>
<span class="line"></span>
<span class="line"><span style="color: #616E88"># Create polynomial features</span></span>
<span class="line"><span style="color: #88C0D0">poly_features</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">PolynomialFeatures</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">degree</span><span style="color: #81A1C1">=</span><span style="color: #B48EAD">15</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">X_poly</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">poly_features.fit_transform</span><span style="color: #ECEFF4">(</span><span style="color: #88C0D0">X.reshape(-1,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #ECEFF4">))</span></span>
<span class="line"><span style="color: #88C0D0">print</span><span style="color: #D8DEE9FF">(f</span><span style="color: #88C0D0">&#39;X_poly Shape: {X_poly.shape}&#39;</span><span style="color: #D8DEE9FF">)</span></span></code></pre></div>


<pre class="wp-block-code"><span><code class="hljs">X_poly Shape: (60, 16)</code></span></pre>


<p><strong>1- </strong><code><strong>poly_features = PolynomialFeatures(degree=15)</strong></code>: This creates an instance of PolynomialFeatures with the degree set to 15.. This means it will generate polynomial features up to the $15th$ degree. For example, if the input feature is $x$, it will generate features $1,x,x^2,x^3,&#8230;,x^{15}$. </p>



<p><strong>2- <code>X_poly = poly_features.fit_transform(X.reshape(-1, 1))</code></strong>:</p>



<ul class="wp-block-list">
<li><code>X.reshape(-1, 1)</code>: This reshapes the input feature array <code>X</code> to a 2D array with one column <code>(60, 1)</code>. The <code>-1</code> means the number of rows is inferred based on the input size.</li>



<li><code>poly_features.fit_transform(X.reshape(-1, 1))</code>: This method first fits the <code>PolynomialFeatures</code> transformer to the data <code>X</code> and then transforms <code>X</code> into a new feature matrix <code>X_poly</code> that contains the original features and their polynomial combinations up to the specified degree (15 in this case).</li>
</ul>



<h4 class="wp-block-heading">Training and Prediction</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from sklearn.linear_model import LinearRegression

# Fit a linear regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict
y_pred = model.predict(X_poly)
print(f'y_pred Shape: {y_pred.shape}')" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">from</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">sklearn</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">linear_model</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">import</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">LinearRegression</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF"># </span><span style="color: #8FBCBB">Fit</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">a</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">linear</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">regression</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">model</span></span>
<span class="line"><span style="color: #8FBCBB">model</span><span style="color: #D8DEE9FF"> = </span><span style="color: #8FBCBB">LinearRegression</span><span style="color: #D8DEE9FF">()</span></span>
<span class="line"><span style="color: #8FBCBB">model</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">fit</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">X_poly</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">y</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF"># </span><span style="color: #8FBCBB">Predict</span></span>
<span class="line"><span style="color: #8FBCBB">y_pred</span><span style="color: #D8DEE9FF"> = </span><span style="color: #8FBCBB">model</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">predict</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">X_poly</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #8FBCBB">print</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">f</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">y_pred Shape: {y_pred.shape}</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span></code></pre></div>


<pre class="wp-block-code"><span><code class="hljs">y_pred Shape: (60,)</code></span></pre>


<p><strong>1- <code>fit(X_poly, y)</code></strong>: This is the training step. Here’s what happens during this step:</p>



<ul class="wp-block-list">
<li>The model learns the relationship between the input features (<code>X_poly</code>) and the target variable (<code>y</code>).</li>



<li>It estimates the coefficients (weights) for each feature in <code>X_poly</code> that minimize the error between the predicted values and the actual target values (<code>y</code>). This is typically done using methods like Ordinary Least Squares</li>
</ul>



<p><strong>2- <code>predict(X_poly)</code></strong>: After the model is trained, this method uses the learned coefficients to make predictions on the input data (<code>X_poly</code>). The predicted values are stored in <code>y_pred</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="print(f'y_pred: {y_pred}')" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">print</span><span style="color: #D8DEE9FF">(f</span><span style="color: #88C0D0">&#39;y_pred: {y_pred}&#39;</span><span style="color: #D8DEE9FF">)</span></span></code></pre></div>


<pre class="wp-block-code"><span><code class="hljs">y_pred: &#91; 1.78904912  1.40950038  1.88346137  2.03553646  1.94073217  1.81354631
  1.70798893  1.54640111  1.22841837  0.71288828  0.04430492 -0.66674565
 -1.28660681 -1.70320828 -1.85742608 -1.75460089 -1.45688069 -1.06187292
 -0.67527663 -0.38506519 -0.24305829 -0.25710803 -0.39432711 -0.59336913
 -0.78210711 -0.89632865 -0.89527163 -0.77081389 -0.54864915 -0.2815172
 -0.03619069  0.12282772  0.14928545  0.0272675  -0.22343637 -0.55018789
 -0.8783894  -1.12844428 -1.23530359 -1.16615971 -0.93172534 -0.58740249
 -0.2225043   0.06164288  0.18081021  0.0973818  -0.15850241 -0.48515933
 -0.72958153 -0.73292508 -0.39410056  0.26625357  1.06954225  1.70488566
  1.8456078   1.35852475  0.54440205  0.22689037  1.30077841  3.02172261]</code></span></pre>


<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# Plot the results
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label='Overfitted Model')
plt.title('Overfitting with High-Degree Polynomial')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #616E88"># Plot the results</span></span>
<span class="line"><span style="color: #88C0D0">plt.scatter(X,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">y,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">color=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">blue</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">label=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">Data</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.plot(X,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">y_pred,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">color=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">red</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">label=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">Overfitted Model</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.title(</span><span style="color: #88C0D0">&#39;Overfitting with High-Degree Polynomial&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.xlabel(</span><span style="color: #88C0D0">&#39;X&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.ylabel(</span><span style="color: #88C0D0">&#39;y&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.legend</span><span style="color: #ECEFF4">()</span></span>
<span class="line"><span style="color: #D8DEE9FF">plt.show</span><span style="color: #ECEFF4">()</span></span></code></pre></div>



<figure class="wp-block-image size-full"><img decoding="async" width="565" height="455" loading="lazy" src="https://ghazi.hudeihed.com/wp-content/uploads/2024/06/overfitting-3.png" alt="" class="wp-image-1430" srcset="https://ghazi.hudeihed.com/wp-content/uploads/2024/06/overfitting-3.png 565w, https://ghazi.hudeihed.com/wp-content/uploads/2024/06/overfitting-3-300x242.png 300w" sizes="auto, (max-width: 565px) 100vw, 565px" /></figure>



<ul class="wp-block-list">
<li>We created a polynomial features up to the 15th degree, which introduces a lot of complexity.</li>



<li>We fit a linear regression model to these polynomial features, leading to overfitting because the model is too complex for the small dataset.</li>
</ul>



<h3 class="wp-block-heading">Step 3: Apply Ridge Regression</h3>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from sklearn.linear_model import Ridge

# Fit a ridge regression model
ridge_model = Ridge(alpha=1, solver='cholesky')
ridge_model.fit(X_poly, y)

# Predict
y_ridge_pred = ridge_model.predict(X_poly)
print(f'y_ridge_pred Shape: {y_ridge_pred.shape}')" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">from</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">sklearn</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">linear_model</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">import</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">Ridge</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF"># </span><span style="color: #8FBCBB">Fit</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">a</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">ridge</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">regression</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">model</span></span>
<span class="line"><span style="color: #8FBCBB">ridge_model</span><span style="color: #D8DEE9FF"> = </span><span style="color: #8FBCBB">Ridge</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">alpha</span><span style="color: #D8DEE9FF">=1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">solver</span><span style="color: #D8DEE9FF">=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">cholesky</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #8FBCBB">ridge_model</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">fit</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">X_poly</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #8FBCBB">y</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF"># </span><span style="color: #8FBCBB">Predict</span></span>
<span class="line"><span style="color: #8FBCBB">y_ridge_pred</span><span style="color: #D8DEE9FF"> = </span><span style="color: #8FBCBB">ridge_model</span><span style="color: #D8DEE9FF">.</span><span style="color: #8FBCBB">predict</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">X_poly</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #8FBCBB">print</span><span style="color: #D8DEE9FF">(</span><span style="color: #8FBCBB">f</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">y_ridge_pred Shape: {y_ridge_pred.shape}</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span></code></pre></div>


<pre class="wp-block-code"><span><code class="hljs">y_ridge_pred Shape: (60,)</code></span></pre>


<p><strong>1- <code>Ridge(alpha=1, solver='cholesky')</code></strong>:</p>



<ul class="wp-block-list">
<li><strong><code>Ridge</code></strong>: This creates an instance of the <code>Ridge</code> regression model.</li>



<li><strong><code>alpha=1</code></strong>: This parameter controls the regularization strength. A higher value of alpha increases regularization, which helps to prevent overfitting by penalizing large coefficients.</li>



<li><strong><code>solver='cholesky'</code></strong>: This specifies the solver to use for the computations. The &#8216;cholesky&#8217; solver is efficient for small datasets and uses the Cholesky decomposition to solve the linear system.</li>
</ul>



<p><strong>2- <code>ridge_model.fit(X_poly, y)</code></strong>: This is the training step for the ridge regression model. Here&#8217;s what happens during this step:</p>



<ul class="wp-block-list">
<li>The model learns the relationship between the input features (<code>X_poly</code>) and the target variable (<code>y</code>).</li>



<li>It estimates the coefficients (weights) for each feature in <code>X_poly</code> that minimize the error between the predicted values and the actual target values (<code>y</code>), while also penalizing large coefficients to prevent overfitting. This is done by minimizing the sum of squared errors plus a penalty proportional to the sum of the squares of the coefficients.</li>
</ul>



<p><strong>3- <code>y_ridge_pred = ridge_model.predict(X_poly)</code></strong>: This uses the trained ridge regression model to predict the target variable <code>y</code> based on the polynomial features <code>X_poly</code>. The <code>predict</code> method returns the predicted values, which are stored in <code>y_ridge_pred</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="print(f'y_ridge_pred: {y_ridge_pred}')" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">print</span><span style="color: #D8DEE9FF">(f</span><span style="color: #88C0D0">&#39;y_ridge_pred: {y_ridge_pred}&#39;</span><span style="color: #D8DEE9FF">)</span></span></code></pre></div>


<pre class="wp-block-code"><span><code class="hljs">y_ridge_pred: &#91; 2.10652297  1.9861319   1.77732828  1.52723874  1.26559883  1.01034802
  0.77158023  0.55429562  0.36028847  0.18941997  0.04045897 -0.08837631
 -0.19908012 -0.29365698 -0.37400834 -0.44187572 -0.49881859 -0.5462127
 -0.5852595  -0.61700099 -0.64233653 -0.66203957 -0.67677329 -0.68710466
 -0.69351682 -0.69641962 -0.69615878 -0.69302339 -0.68725231 -0.67903929
 -0.66853721 -0.65586126 -0.64109145 -0.62427421 -0.60542335 -0.58452025
 -0.5615133  -0.53631661 -0.50880777 -0.47882471 -0.44616134 -0.41056175
 -0.37171254 -0.32923272 -0.28266021 -0.23143359 -0.17486683 -0.11211382
 -0.0421175   0.0364641   0.12536645  0.22687908  0.34408321  0.48120331
  0.64412469  0.8411513   1.08410712  1.38992383  1.78290969  2.29796329]</code></span></pre>


<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# Plot the results
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label='Overfitted Model')
plt.plot(X, y_ridge_pred, color='green', label='Ridge Model')
plt.title('Overfitting vs. Ridge Regression')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #616E88"># Plot the results</span></span>
<span class="line"><span style="color: #88C0D0">plt.scatter(X,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">y,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">color=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">blue</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">label=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">Data</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.plot(X,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">y_pred,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">color=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">red</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">label=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">Overfitted Model</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.plot(X,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">y_ridge_pred,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">color=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">green</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">label=</span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">Ridge Model</span><span style="color: #ECEFF4">&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.title(</span><span style="color: #88C0D0">&#39;Overfitting vs. Ridge Regression&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.xlabel(</span><span style="color: #88C0D0">&#39;X&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.ylabel(</span><span style="color: #88C0D0">&#39;y&#39;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #88C0D0">plt.legend</span><span style="color: #ECEFF4">()</span></span>
<span class="line"><span style="color: #D8DEE9FF">plt.show</span><span style="color: #ECEFF4">()</span></span></code></pre></div>



<figure class="wp-block-image size-full"><img decoding="async" width="565" height="455" loading="lazy" src="https://ghazi.hudeihed.com/wp-content/uploads/2024/06/l2-3.png" alt="" class="wp-image-1431" srcset="https://ghazi.hudeihed.com/wp-content/uploads/2024/06/l2-3.png 565w, https://ghazi.hudeihed.com/wp-content/uploads/2024/06/l2-3-300x242.png 300w" sizes="auto, (max-width: 565px) 100vw, 565px" /></figure>



<ul class="wp-block-list">
<li>Finally, We applied Ridge Regression (L2 regularization) with <code>alpha = 1</code>. This helps to penalize the coefficients of the polynomial features, reducing their magnitudes and hence the complexity of the model.</li>



<li>The green line (Ridge model) should follow the general trend of the data better than the red line (overfitted model), demonstrating how regularization helps to mitigate overfitting.</li>
</ul>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
