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




[ad_1]

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.scatter(features,labels)
plt.xlabel(‘Years of Experience’)
plt.ylabel(‘Salary’)
plt.title(‘Salary V/s Years of Experience’)
plt.show()

Step 6 – Sampling
from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(features,
labels,
test_size=0.33,
random_state=0)

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)
regressor.fit(x_poly,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)
regressor.fit(x_poly,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
https://www.youtube.com/playlist?list=PLE-8p-CwnFPuOjFcbnXLFvSQaHFK3ymUW

2. Confusion Matrix in Machine Learning, ML, AI
https://www.youtube.com/playlist?list=PLE-8p-CwnFPvXzvsEcgb0IZtNsw_0vUzr

3. Anaconda, Python Installation, Spyder, Jupyter Notebook, PyCharm, Graphviz
https://www.youtube.com/playlist?list=PLE-8p-CwnFPsBCsWwz_BvbZZHIVQ6wSZK

4. Cross Validation, Sampling, train test split in Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPsHtol5WXHhq_B3kQPggHH2

5. Drop and Delete Operations in Python Pandas
https://www.youtube.com/playlist?list=PLE-8p-CwnFPtvqVVK7QVFsMvDvp2YgCnR

6. Matrices and Vectors with python
https://www.youtube.com/playlist?list=PLE-8p-CwnFPsndwnZnL7nXW5mIrdRmgdg

7. Detect Outliers in Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPvyCX35yES5D9W7vThiUzwk

8. TimeSeries preprocessing in Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPv10bru3719xzDNIgbO6hXA

9. Handling Missing Values in Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPvOec0LZ40Bt8OQcbLFa236

10. Dummy Encoding Encoding in Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPvu7YriqMZsL9UDbqUUk90x

11. Data Visualisation with Python, Seaborn, Matplotlib
https://www.youtube.com/playlist?list=PLE-8p-CwnFPuYBYsmbfMjROOCzKjCwyMH

12. Feature Scaling in Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPtwpVV3FwzwYZYR5hT3i52G

13. Python 3 basics for Beginner
https://www.youtube.com/playlist?list=PLE-8p-CwnFPu-jseUMtc4i47jQZN4PNbf

14. Statistics with Python
https://www.youtube.com/playlist?list=PLE-8p-CwnFPta0COlxS6E5u14m5ouzbRU

15. Sklearn Scikit Learn Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPtAGb29r8F7up9ilZUXt3l1

16. Python Pandas Dataframe Operations
https://www.youtube.com/playlist?list=PLE-8p-CwnFPv_63lkT_Tztiwknv_zGTNy

17. Linear Regression, Supervised Machine Learning
https://www.youtube.com/playlist?list=PLE-8p-CwnFPslDi6sfFbFK4KXcVlLwaOM

18 Interiew Questions on Machine Learning and Data Science
https://www.youtube.com/playlist?list=PLE-8p-CwnFPt7VBhcnh82y0autSzuOrZp

19. Jupyter Notebook Operations
https://www.youtube.com/playlist?list=PLE-8p-CwnFPtqkFd67OZcoSv4BAI7ez5_

Source


[ad_2]

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