Skip to content

Commit 385d22d

Browse files
archu5stokhos
authored andcommitted
TheAlgorithms#840 adds polymonial regression program in python (TheAlgorithms#1235)
* TheAlgorithms#840 adds polymonial regression program in python * Update polymonial_regression.py * Update polymonial_regression.py
1 parent d93e9a0 commit 385d22d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import matplotlib.pyplot as plt
2+
import pandas as pd
3+
4+
# Importing the dataset
5+
dataset = pd.read_csv('https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/position_salaries.csv')
6+
X = dataset.iloc[:, 1:2].values
7+
y = dataset.iloc[:, 2].values
8+
9+
10+
# Splitting the dataset into the Training set and Test set
11+
from sklearn.model_selection import train_test_split
12+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
13+
14+
15+
# Fitting Polynomial Regression to the dataset
16+
from sklearn.preprocessing import PolynomialFeatures
17+
from sklearn.linear_model import LinearRegression
18+
poly_reg = PolynomialFeatures(degree=4)
19+
X_poly = poly_reg.fit_transform(X)
20+
pol_reg = LinearRegression()
21+
pol_reg.fit(X_poly, y)
22+
23+
24+
# Visualizing the Polymonial Regression results
25+
def viz_polymonial():
26+
plt.scatter(X, y, color='red')
27+
plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color='blue')
28+
plt.title('Truth or Bluff (Linear Regression)')
29+
plt.xlabel('Position level')
30+
plt.ylabel('Salary')
31+
plt.show()
32+
return
33+
viz_polymonial()
34+
35+
# Predicting a new result with Polymonial Regression
36+
pol_reg.predict(poly_reg.fit_transform([[5.5]]))
37+
#output should be 132148.43750003

0 commit comments

Comments
 (0)