Predicting Non-Linear Data with Polynomial Regression | My Machine Learning Project Journey

Lokesh PatidarLokesh Patidar
3 min read

Introduction:

Hey everyone!
I recently completed a project on Polynomial Regression as part of my Machine Learning learning path. This post walks you through what Polynomial Regression is, why we need it, how I implemented it using Python, and key takeaways from the project.

Whether you're new to ML or just exploring different regression techniques—this one’s for you!


→What is Polynomial Regression?

Polynomial Regression is a type of regression that models the relationship between the independent variable x and the dependent variable y as an n-th degree polynomial. It’s used when the data shows a non-linear trend.

Mathematically:

y=b0+b1x+b2x2+b3x3+...+bnxny = b_0 + b_1x + b_2x^2 + b_3x^3 + ... + b_nx^ny=b0​+b1​x+b2​x2+b3​x3+...+bn​xn

It’s essentially an extension of Linear Regression that allows us to fit curves instead of straight lines.


→Why Not Just Use Linear Regression?

Let’s say we have data that curves upward or downward. A straight line won’t capture the pattern well. Polynomial Regression helps in:

  • Better curve fitting for non-linear data

  • Capturing complexity in datasets where relationships aren't strictly linear


→ Tools & Libraries Used:

  • Python

  • NumPy

  • Matplotlib / Seaborn

  • Pandas

  • Scikit-learn (for model building)


📊 Dataset:

For this project, I used a synthetic dataset representing the relationship between position level and salary (a classic example). The goal was to predict salary based on the position level.

You can use your own dataset or one like this:

Position LevelSalary
145000
250000
360000
480000
5110000
6150000
7200000
8300000
9500000
101000000

🔍 Step-by-Step Implementation:

1. Import Libraries

pythonCopyEditimport numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

2. Load the Dataset

pythonCopyEditdataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values  # position level
y = dataset.iloc[:, 2].values    # salary

3. Create Polynomial Features

pythonCopyEditpoly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X)

4. Fit the Model

pythonCopyEditlin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)

5. Visualize the Polynomial Regression Results

pythonCopyEditplt.scatter(X, y, color='red')
plt.plot(X, lin_reg_2.predict(X_poly), color='blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

6. Predict a New Result

pythonCopyEditprint(lin_reg_2.predict(poly_reg.transform([[6.5]])))

📈 Output & Results:

  • The polynomial model fitted the data much better than the linear model.

  • It captured the curved trend of salary increases much more accurately.

  • Predicting salary for level 6.5 gave a very close estimate compared to ground truth.

You can include your model's graph here as a screenshot or embed.


→Key Takeaways:

  • Polynomial Regression is powerful when dealing with non-linear datasets.

  • It’s still a linear model at its core, because it’s linear in coefficients.

  • Overfitting can occur if the degree of the polynomial is too high.

  • Visualizations are key to understanding how well your model fits.


→What I Learned:

  • Hands-on understanding of how feature transformation helps in non-linearity

  • Visualizing model performance is just as important as numbers

  • Small tweaks (like changing polynomial degree) can greatly affect model behavior



0
Subscribe to my newsletter

Read articles from Lokesh Patidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lokesh Patidar
Lokesh Patidar

Hey, I'm Lokesh Patidar! I'm a 2nd-year student at SATI Vidisha, passionate about AI, Machine Learning, Full-Stack Development , and DSA. What I'm Learning: Currently Exploring Machine Learning 🤖 Completed DSA & Frontend Development 🌐 Now exploring Backend Development 💡 Interests: I love solving problems, building projects, and integrating AI into real-world applications. Excited to contribute to tech communities and share my learning journey! 📌 Follow my blog for insights on AI, ML, and Full-Stack projects!