forked from aman64039/Data-Pre-Processing-for-ML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolydataset.py
54 lines (39 loc) · 1.18 KB
/
polydataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 14 14:52:37 2019
@author: aman
"""
#imorting the packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#importing the dataset
dataset = pd.read_csv("/home/aman/Desktop/ML/Datasets/Position_Salaries.csv")
#creating the feature of matrix
X = dataset.iloc[:,1].values
#label vector
y = dataset.iloc[:,2].values
#converting the array to matrix
X=X.reshape(-1,1)
#visulize the problem or data set
plt.scatter(X,y)
plt.show()
#using linear regression find the best fit line and again plot it and check the best fit line
from sklearn.linear_model import LinearRegression
lin=LinearRegression()
lin.fit(X,y)
#importing polynomial feature
from sklearn.preprocessing import PolynomialFeatures
#passing the degree acc to the requirment
poly=PolynomialFeatures(degree=4)
#fitting the polynomial
X_poly=poly.fit_transform(X)
poly.fit(X_poly,y)
#creating the another object of linear regression class
lin2=LinearRegression()
lin2.fit(X_poly,y)
#ploting the graph by using the polynomial fiunctio and get the best fit line
plt.scatter(X,y,color="red")
plt.plot(X,lin2.predict(X_poly),color="blue")
plt.show()