-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_Regression_A-Z.py
186 lines (88 loc) · 2.54 KB
/
Linear_Regression_A-Z.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
# coding: utf-8
# In[25]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
# In[26]:
data=pd.read_csv(Salary_Data.csv')
# In[27]:
data.head()
# In[29]:
data.shape
# In[30]:
target=data['Salary']
# In[31]:
data=data.drop(['Salary'],axis='columns')
# In[32]:
data.head()
# In[33]:
plt.scatter(data,target,color='blue',marker='*')
plt.xlabel('YearsExperience')
plt.ylabel('Salary')
# In[34]:
from sklearn.model_selection import train_test_split
# In[48]:
x_train,x_test,y_train,y_test=train_test_split(data,target,test_size=1/3,random_state=0)
# In[49]:
len(x_train)
# In[50]:
from sklearn.linear_model import LinearRegression
# In[51]:
model=LinearRegression()
# In[52]:
model.fit(x_train,y_train)
# In[61]:
model.predict([[1.2]])
# In[53]:
model.score(data,target)
# In[57]:
plt.plot(x_train,model.predict(x_train),color='blue',marker='*')
plt.scatter(x_train,y_train,color='red')
# In[56]:
plt.plot(x_test,model.predict(x_test),color='blue',marker='*')
plt.scatter(x_test,y_test,color='red')
# In[46]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv(Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)
# Feature Scaling
"""from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
sc_y = StandardScaler()
y_train = sc_y.fit_transform(y_train)"""
# Fitting Simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Predicting the Test set results
y_pred = regressor.predict(X_test)
regressor.score(X_test,y_test)
# Visualising the Training set results
plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
# Visualising the Test set results
plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
# In[47]:
regressor.score(X_test,y_test)
# In[60]:
regressor.predict([[1.2]])
# In[ ]: