-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSGDLinearRegression.py
executable file
·51 lines (38 loc) · 1.59 KB
/
SGDLinearRegression.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
#!/usr/bin/env python3
import numpy as np
import pandas as pd
from sklearn.metrics import r2_score
data_train = pd.read_csv('/datasets/train_data_n.csv')
features_train = data_train.drop(['target'], axis=1)
target_train = data_train['target']
data_test = pd.read_csv('/datasets/test_data_n.csv')
features_test = data_test.drop(['target'], axis=1)
target_test = data_test['target']
class SGDLinearRegression(object):
def __init__(self, step_size, epochs, batch_size):
self.step_size=step_size
self.epochs=epochs
self.batch_size=batch_size
def fit(self, train_features, train_target):
X = np.concatenate((np.ones(train_features.shape[0],1), train_features), axis=1)
y = train_target
w = np.zeros(X.shape[1])
for _ in range(self.epochs):
batches_count = X.shape[0] // self.batch_size
for i in range(batches_count):
begin = i * self.batch_size
end = (i + 1) * self.batch_size
X_batch = X[begin:end, :]
y_batch = y[begin:end]
gradient = 2 * X_batch.T.dot(X_batch.dot(w) - y_batch) / X_batch.shape[0]
w -= self.step_size * gradient
self.w = w[1:]
self.w0 = w[0]
def predict(self,test_features):
return test_features.dot(self.w) + self.w0
model = SGDLinearRegression(0.01, 10, 100)
model.fit(features_train, target_train)
pred_train = model.predict(features_train)
pred_test = model.predict(features_test)
print(r2_score(target_train, pred_train).round(5))
print(r2_score(target_test, pred_test).round(5))