-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise of Salary Data_Linear Regression .py
125 lines (48 loc) · 1.1 KB
/
Exercise of Salary Data_Linear Regression .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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# coding: utf-8
# In[1]:
### importing of libraries -
import pandas as pd
import seaborn as sns ### for sytlist Visualisation
import matplotlib.pyplot as plt
# In[2]:
data =pd.read_csv("E:/Python docs/Salary_Data.csv")
print(data.head(5))
# In[3]:
print(data.columns)
# In[6]:
sns.scatterplot(x='YearsExperience',y='Salary',data=data)
# In[4]:
X =data['YearsExperience']
print(X)
# In[5]:
y =data['Salary']
print(y)
# In[6]:
from sklearn.model_selection import train_test_split
# In[7]:
X =data['YearsExperience'].values.reshape(-1,1)
# In[8]:
y =data['Salary'].values.reshape(-1,1)
# In[9]:
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
# In[16]:
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
# In[18]:
model = LinearRegression()
# In[19]:
model_train = model.fit(X_train,y_train)
print(model_train)
# In[20]:
pred = model_train.predict(X_test)
pred
# In[21]:
y_test
# In[50]:
Y_test
# In[24]:
from sklearn.metrics import r2_score
# In[25]:
r2_score(y_test,pred)
# In[ ]: