
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Implementation of Polynomial Regression
Polynomial Regression is a form of linear regression in which the relationship between the independent variable x and dependent variable y is modeled as an nth degree polynomial. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x)
Example
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset datas = pd.read_csv('data.csv') datas # divide the dataset into two components X = datas.iloc[:, 1:2].values y = datas.iloc[:, 2].values # Fitting Linear Regression to the dataset from sklearn.linear_model import LinearRegression lin = LinearRegression() lin.fit(X, y) # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree = 4) X_poly = poly.fit_transform(X) poly.fit(X_poly, y) lin2 = LinearRegression() lin2.fit(X_poly, y) # Visualising the Linear Regression results plt.scatter(X, y, color = 'blue') plt.plot(X, lin.predict(X), color = 'red') plt.title('Linear Regression') plt.xlabel('Temperature') plt.ylabel('Pressure') plt.show() # Visualising the Polynomial Regression results plt.scatter(X, y, color = 'blue') plt.plot(X, lin2.predict(poly.fit_transform(X)), color = 'red') plt.title('Polynomial Regression') plt.xlabel('Temperature') plt.ylabel('Pressure') plt.show() # Predicting a new result with Linear Regression lin.predict(110.0) # Predicting a new result with Polynomial Regression lin2.predict(poly.fit_transform(110.0))
- Related Articles
- Python Polynomial Regression in Machine Learning
- How to create polynomial regression model in R?
- C program to compute the polynomial regression algorithm
- How can a polynomial regression model be fit to understand non-linear trends in data in Python?
- Linear Regression using Python?
- Implementation of Dynamic Array in Python
- Python Alternate repr() implementation
- Understanding Logistic Regression in Python?
- Correlation and Regression in Python
- The implementation of import in Python (importlib)
- Which is the fastest implementation of Python
- Decision tree implementation using Python
- Demonstrate a basic implementation of ‘tf.keras.layers.Dense’ in Python
- Locally Weighted Linear Regression in Python
- Page Rank Algorithm and Implementation using Python

Advertisements