-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmultivariate_linear_regression.py
56 lines (46 loc) · 1.99 KB
/
multivariate_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
from __future__ import annotations
from typing import Tuple
import numpy as np
class MultivariateLinearRegression:
"""Multivariate Linear Regression
Parameters:
-----------
learning_rate: float
The step length used when following the negative gradient during training.
"""
def __init__(self, learning_rate: float) -> None:
self.learning_rate = learning_rate
self.w = ""
def cost_function(self, x: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, float]:
dif = np.dot(x, self.w) - y
cost = np.sum(dif**2) / (2*np.shape(x)[0])
return dif, cost
def fit(self, x: np.ndarray, y: np.ndarray, num_iterations: int = 10000) -> MultivariateLinearRegression:
if self.w == "":
_, num_features = np.shape(x)
self.w = np.random.uniform(-1, 1, num_features)
for i in range(num_iterations):
dif, cost = self.cost_function(x, y)
gradient = np.dot(x.transpose(), dif) / np.shape(x)[0]
self.w = self.w - self.learning_rate * gradient
return self
def predict(self, x: np.ndarray) -> np.ndarray:
return np.dot(x, self.w)
# Testing functionality
if __name__ == '__main__':
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'label'])
le = LabelEncoder()
iris['label'] = le.fit_transform(iris['label'])
X = np.array(iris.drop(['petal_width'], axis=1))
y = np.array(iris['petal_width'])
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
model = MultivariateLinearRegression(0.0001)
model.fit(X_train, y_train, 10000)
predictions = model.predict(X_test)
mse = ((y_test - predictions)**2).mean(axis=0)
print('Loss:', mse)