0% found this document useful (0 votes)
56 views9 pages

Polynomial Regression

The document outlines the process of building a polynomial regression model to detect salary bluffing in candidates based on their previous salary data. It includes steps for data preprocessing, fitting linear and polynomial regression models, and visualizing the results for degrees 2 to 6. The final predictions for a candidate with a position level of 6.5 are provided for both linear and polynomial regression models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views9 pages

Polynomial Regression

The document outlines the process of building a polynomial regression model to detect salary bluffing in candidates based on their previous salary data. It includes steps for data preprocessing, fitting linear and polynomial regression models, and visualizing the results for degrees 2 to 6. The final predictions for a candidate with a position level of 6.5 are provided for both linear and polynomial regression models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

polynomial 12/01/25, 5:10 PM

Polynomial Regression:
In [9]: # Problem Description: There is a Human Resource company, which is going to
# The candidate has told his previous salary 160K per annum, and the HR have
# So to identify this, they only have a dataset of his previous company in w
# By checking the dataset available, we have found that there is a non-linea
# Our goal is to build a Bluffing detector regression model, so HR can hire
# Below are the steps to build such a model.

In [17]: #The code for pre-processing step is given below:


# importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

importing datasets
In [19]: df = pd.read_csv("data.csv")

In [21]: df

Out[21]: Position Level Salary

0 Business Analyst 1 45000

1 Junior Consultant 2 50000

2 Senior Consultant 3 60000

3 Manager 4 80000

4 Country Manager 5 110000

5 Region Manager 6 150000

6 Partner 7 200000

7 Senior Partner 8 300000

8 C - Level 9 500000

9 CEO 10 1000000

In [32]: # As we can see in the above output, there are three columns present (Positi

# Here we will predict the output for level 6.5 because the candidate has 4+

Extract Independetn & Dependent


variables
about:srcdoc Page 1 of 9
polynomial 12/01/25, 5:10 PM

In [36]: x = df.iloc[:,1:2].values
y = df.iloc[:,2].values

print(x)
print(y)

[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
[ 45000 50000 60000 80000 110000 150000 200000 300000 50
0000
1000000]

Building the Linear regression model:


In [38]: # Fitting the Lineat Regression to the dataset
from sklearn.linear_model import LinearRegression

In [40]: lin_regs= LinearRegression()


lin_regs.fit(x,y)

Out[40]: ▾ LinearRegression i ?

LinearRegression()

Build the Polynomial Regression


In [45]: # Fitting the Polynomial regression to dataset
from sklearn.preprocessing import PolynomialFeatures
ploy_regs = PolynomialFeatures(degree=2)
x_poly = ploy_regs.fit_transform(x)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly,y)

Out[45]: ▾ LinearRegression i ?

LinearRegression()

In [47]: print(x_poly)

about:srcdoc Page 2 of 9
polynomial 12/01/25, 5:10 PM

[[ 1. 1. 1.]
[ 1. 2. 4.]
[ 1. 3. 9.]
[ 1. 4. 16.]
[ 1. 5. 25.]
[ 1. 6. 36.]
[ 1. 7. 49.]
[ 1. 8. 64.]
[ 1. 9. 81.]
[ 1. 10. 100.]]

In [49]: print(lin_reg_2)

LinearRegression()

Visulaizing the result for Linear


Regression Model
In [52]: plt.scatter(x,y,color='blue')
plt.plot(x,lin_regs.predict(x), color='red')
plt.title("Linear Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")
plt.show()

visualize the Polynomial Regression


about:srcdoc Page 3 of 9
polynomial 12/01/25, 5:10 PM

In [ ]:

In [55]: plt.scatter(x,y,color='blue')
plt.plot(x,lin_reg_2.predict(ploy_regs.fit_transform(x)),color='red')
plt.title("Polynomial Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")
plt.show()

In [61]: # lets change the degree to 3

# Fitting the Polynomial regression to dataset


from sklearn.preprocessing import PolynomialFeatures
ploy_regs = PolynomialFeatures(degree=3)
x_poly = ploy_regs.fit_transform(x)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly,y)

Out[61]: ▾ LinearRegression i ?

LinearRegression()

In [63]: plt.scatter(x,y,color='blue')
plt.plot(x,lin_reg_2.predict(ploy_regs.fit_transform(x)),color='red')
plt.title("Polynomial Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")

about:srcdoc Page 4 of 9
polynomial 12/01/25, 5:10 PM

plt.show()

In [75]: # lets make degree 4

# Fitting the Polynomial regression to dataset


from sklearn.preprocessing import PolynomialFeatures
ploy_regs = PolynomialFeatures(degree=4)
x_poly = ploy_regs.fit_transform(x)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly,y)

Out[75]: ▾ LinearRegression i ?

LinearRegression()

In [77]: plt.scatter(x,y,color='blue')
plt.plot(x,lin_reg_2.predict(ploy_regs.fit_transform(x)),color='red')
plt.title("Polynomial Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")
plt.show()

about:srcdoc Page 5 of 9
polynomial 12/01/25, 5:10 PM

In [79]: # lets make degree 5


# Fitting the Polynomial regression to dataset
from sklearn.preprocessing import PolynomialFeatures
ploy_regs = PolynomialFeatures(degree=5)
x_poly = ploy_regs.fit_transform(x)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly,y)

Out[79]: ▾ LinearRegression i ?

LinearRegression()

In [81]: plt.scatter(x,y,color='blue')
plt.plot(x,lin_reg_2.predict(ploy_regs.fit_transform(x)),color='red')
plt.title("Polynomial Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")
plt.show()

about:srcdoc Page 6 of 9
polynomial 12/01/25, 5:10 PM

In [83]: # lets make degree 6

# Fitting the Polynomial regression to dataset


from sklearn.preprocessing import PolynomialFeatures
ploy_regs = PolynomialFeatures(degree=6)
x_poly = ploy_regs.fit_transform(x)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly,y)

Out[83]: ▾ LinearRegression i ?

LinearRegression()

In [106… plt.scatter(x,y,color='blue')
plt.plot(x,lin_reg_2.predict(ploy_regs.fit_transform(x)),color='red')
plt.title("Polynomial Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")
plt.show()

about:srcdoc Page 7 of 9
polynomial 12/01/25, 5:10 PM

Predict final result of Linear Regression


model
In [88]: lin_predict = lin_regs.predict([[6.5]])
print(lin_predict)

[330378.78787879]

Predict the final result of Polynomial


Regression
In [92]: ploy_pred = lin_reg_2.predict(ploy_regs.fit_transform([[6.5]]))
print(ploy_pred)

[174192.81930718]

In [112… plt.scatter(x,y,color='blue')
plt.plot(x,lin_reg_2.predict(ploy_regs.fit_transform(x)),color='red')
plt.plot(6.5, ploy_pred, color='green', marker='o', markersize=10)
plt.axvline(x=6.5, color='green', linestyle='--')
plt.title("Polynomial Regression")
plt.xlabel("Position levels")
plt.ylabel("Salary")
plt.legend(['Data', 'Polynomial Regression', 'Predicted Value'])
plt.show()

about:srcdoc Page 8 of 9
polynomial 12/01/25, 5:10 PM

In [ ]:

In [ ]:

about:srcdoc Page 9 of 9

You might also like