-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegression.py
More file actions
200 lines (128 loc) · 4.89 KB
/
Regression.py
File metadata and controls
200 lines (128 loc) · 4.89 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
"""Linear_reg.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1xC6Nxb_zg_RQNhOtl6cNGR_kG6BxFx-8
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
"""### **Simple Linear Regression**"""
from google.colab import files
uploaded = files.upload()
dataset = pd.read_csv('Salary_Data.csv')
dataset.head()
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,-1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)
# Feature Scaling
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)
model = linear_model.LinearRegression()
model.fit(X_train, y_train)
y_predicted = model.predict(X_test)
print("Mean squared error is: ", mean_squared_error(y_test, y_predicted))
print("Weights: ", model.coef_)
print("Intercept: ", model.intercept_)
score=r2_score(y_test,y_predicted)
print("Score: ", score)
# Visualising the Training set results
plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, model.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test 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, model.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
a = np.savetxt('res.txt',y_predicted,delimiter=',')
files.download('res.txt')
"""### **Multiple Linear Regression**"""
uploaded = files.upload()
dataset = pd.read_csv('50_Startups.csv')
# dataset.head()
X = dataset.iloc[:,:-1]
y = dataset.iloc[:,-1]
#Convert the column into categorical columns
states=pd.get_dummies(X['State'],drop_first=False)
# Drop the state coulmn
X = X.drop('State',axis=1)
# concat the dummy variables
X=pd.concat([X,states],axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
model = linear_model.LinearRegression()
model.fit(X_train, y_train)
y_predicted = model.predict(X_test)
print("Mean squared error is: ", mean_squared_error(y_test, y_predicted))
print("Weights: ", model.coef_)
print("Intercept: ", model.intercept_)
score=r2_score(y_test,y_predicted)
print("Score: ", score)
"""# *Other Dataset *"""
uploaded = files.upload()
dataset = pd.read_csv('Social_Network_Ads.csv')
dataset.head()
X = dataset.iloc[:,:-1]
y = dataset.iloc[:,-1]
#Convert the column into categorical columns
gender = pd.Categorical(pd.factorize(X.Gender)[0])
# Drop the state coulmn
X = X.drop('Gender',axis=1)
# concat the dummy variables
X=pd.concat([X,pd.DataFrame({'Gender': gender})],axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
model = linear_model.LinearRegression(fit_intercept=True, normalize=True)
model.fit(X_train, y_train)
y_predicted = model.predict(X_test)
print("Mean squared error is: ", mean_squared_error(y_test, y_predicted))
print("Weights: ", model.coef_)
print("Intercept: ", model.intercept_)
score=r2_score(y_test,y_predicted)
print("Score: ", score)
res = pd.DataFrame(y_predicted, columns=['Prediction'])
res.to_csv('res.csv')
files.download('res.csv')
"""## **Reading txt**"""
uploaded = files.upload()
dataset= np.loadtxt('res.txt',delimiter=",")
dataset
"""## **Filling NANs**"""
uploaded = files.upload()
dataset = pd.read_csv('Data.csv')
dataset = dataset.interpolate(method ='linear', limit_direction ='forward')
country = pd.Categorical(pd.factorize(dataset.Country)[0])
purchased = pd.Categorical(pd.factorize(dataset.Purchased)[0])
dataset= dataset.drop(['Country','Purchased'],axis=1)
dataset= pd.concat([dataset,pd.DataFrame(country),pd.DataFrame(purchased)],axis=1)
X = dataset.iloc[:,:-1]
y = dataset.iloc[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.5, random_state = 0)
model = linear_model.LinearRegression(fit_intercept=True, normalize=True)
model.fit(X_train, y_train)
y_predicted = model.predict(X_test)
print("Mean squared error is: ", mean_squared_error(y_test, y_predicted))
print("Weights: ", model.coef_)
print("Intercept: ", model.intercept_)
score=r2_score(y_test,y_predicted)
print("Score: ", score)
"""## **SVR**"""
# Create and train the Support Vector Machine (Regressor)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_rbf.fit(x_train, y_train)
# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
svm_confidence = svr_rbf.score(x_test, y_test)
print("svm confidence: ", svm_confidence)