forked from TannerGilbert/Machine-Learning-Explained
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial_regression.py
More file actions
79 lines (63 loc) · 2.86 KB
/
Copy pathpolynomial_regression.py
File metadata and controls
79 lines (63 loc) · 2.86 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
from __future__ import annotations
from typing import Tuple
from itertools import combinations_with_replacement
import numpy as np
# from https://github.com/eriklindernoren/ML-From-Scratch/blob/master/mlfromscratch/utils/data_manipulation.py#L43
def polynomial_features(X: np.ndarray, degree: float) -> np.ndarray:
n_samples, n_features = np.shape(X)
def index_combinations():
combs = [combinations_with_replacement(range(n_features), i) for i in range(0, degree + 1)]
flat_combs = [item for sublist in combs for item in sublist]
return flat_combs
combinations = index_combinations()
n_output_features = len(combinations)
X_new = np.empty((n_samples, n_output_features))
for i, index_combs in enumerate(combinations):
X_new[:, i] = np.prod(X[:, index_combs], axis=1)
return X_new
class PolynomialRegression:
"""Polynomial Regression
Parameters:
-----------
learning_rate: float
The step length used when following the negative gradient during training.
"""
def __init__(self, learning_rate: float, degree: float = 2) -> None:
self.learning_rate = learning_rate
self.degree = degree
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) -> PolynomialRegression:
x = polynomial_features(x, self.degree)
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:
x = polynomial_features(x, self.degree)
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 = PolynomialRegression(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)