Untitled
unknown
python
a year ago
1.7 kB
9
Indexable
Never
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: 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 = int(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] self.batches_count = batches_count 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))