Python for Machine Learning – Polynomial Linear Regression using Scikit Learn – P56


Polynomial Linear Regression using Scikit Learn

In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable x and the dependent variable y is modelled as an nth degree polynomial in x. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x). Although polynomial regression fits a nonlinear model to the data, as a statistical estimation problem it is linear, in the sense that the regression function E(y | x) is linear in the unknown parameters that are estimated from the data. For this reason, polynomial regression is considered to be a special case of multiple linear regression.

y = 9450x + 25792
y = 16.393×2 + 9259.3x + 26215
y = -122.92×3 + 2099.4×2 – 718.71x + 38863
y = 4.9243×4 – 236.59×3 + 2979.9×2 – 3314.2x + 41165
y = 15.006×5 – 430.13×4 + 4409.7×3 – 19368×2 + 43652x + 8315

Code Starts Here
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv(‘SalaryData_Train.csv’)

features = df.iloc[:,0:1].values
labels = df.iloc[:,1:2].values

plt.xlabel(‘Years of Experience’)
plt.title(‘Salary V/s Years of Experience’)

Step 6 – Sampling
from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(features,

Create the REgression Model

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()

Create The Polynomial Features

from sklearn.preprocessing import PolynomialFeatures

poly_reg = PolynomialFeatures(degree=3)

x_poly = poly_reg.fit_transform(features),labels)

Test the model
y_pred = regressor.predict(poly_reg.fit_transform(X_test))

Calculate the Accuracy

print(‘Polynomial Linear Regression Accuracy:’,regressor.score(poly_reg.fit_transform(X_test),y_test))

for i in range(1,6):
poly_reg = PolynomialFeatures(degree=i)
x_poly = poly_reg.fit_transform(features),labels)
print(‘Degree of Equation :’, i)
print(‘Coefficient :’, regressor.coef_)
print(‘Intercept :’, regressor.intercept_)
print(‘Accuracy Score:’, regressor.score(poly_reg.fit_transform(X_test),y_test))

All Playlist of this youtube channel

1. Data Preprocessing in Machine Learning

2. Confusion Matrix in Machine Learning, ML, AI

3. Anaconda, Python Installation, Spyder, Jupyter Notebook, PyCharm, Graphviz

4. Cross Validation, Sampling, train test split in Machine Learning

5. Drop and Delete Operations in Python Pandas

6. Matrices and Vectors with python

7. Detect Outliers in Machine Learning

8. TimeSeries preprocessing in Machine Learning

9. Handling Missing Values in Machine Learning

10. Dummy Encoding Encoding in Machine Learning

11. Data Visualisation with Python, Seaborn, Matplotlib

12. Feature Scaling in Machine Learning

13. Python 3 basics for Beginner

14. Statistics with Python

15. Sklearn Scikit Learn Machine Learning

16. Python Pandas Dataframe Operations

17. Linear Regression, Supervised Machine Learning

18 Interiew Questions on Machine Learning and Data Science

19. Jupyter Notebook Operations



Comment List

  • MachineLearning with Python
    December 9, 2020

    Can u tell me how to know when to use these polynomial features in different types of problems?

  • MachineLearning with Python
    December 9, 2020

    what's wrong with your accent 😡it's awful

  • MachineLearning with Python
    December 9, 2020

    Hello, thank you for your explanation. In your video, you explain about univariate polynomial regression. I want to ask about how to earn the coefficient for multivariate polynomial regression? I really appreciate for your help.

  • MachineLearning with Python
    December 9, 2020

    Hi, thanks for your explanation. I have a question about the accuracy score you are calculating. I thought that accuracy score can only be calculated for classification problems, and that for regression problems the RMSE is often used to see how good the model is. What is meant in this case with accuracy score?

  • MachineLearning with Python
    December 9, 2020

    Helped me. Thank u!

Write a comment