-
Notifications
You must be signed in to change notification settings - Fork 2
/
leastsquares.py
77 lines (63 loc) · 2.39 KB
/
leastsquares.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
from ..base import BaseClassifier,BaseRegressor
from itertools import combinations_with_replacement
class BaseLeastSquares:
def __init__(self,degree=2):
self.beta = 0
self.degree = degree
def X_to_poly(self,X):
X_poly = X
if self.degree >= 2 :
for deg in range(2,self.degree):
for com in combinations_with_replacement(range(X.shape[1]), deg):
Xd = np.ones(X.shape[0])
for c in com :
Xd = Xd * X[:,c]
X_poly = np.concatenate((X_poly,Xd.reshape(-1,1)),axis=1)
X_poly = np.concatenate((np.ones((X.shape[0],1)),X_poly),axis=1) # add column of 1 for the bias
return X_poly
def fit(self,X,y):
X_poly = self.X_to_poly(X)
self.beta = np.linalg.pinv(X_poly.T @ X_poly) @ X_poly.T @ y
def predict(self,X):
X_poly = self.X_to_poly(X)
return (X_poly @ self.beta)
class PolynomialRegression(BaseLeastSquares,BaseRegressor):
''' Polynomial Least Square Regressor
Parameters
----------
degree : int >=1
degrees of features to consider,ex for 2 features x1,x2 with degree =2,
the input features are going to be x1,x2,x1x2,x1^2,x2^2
'''
pass
class PolynomialClassification(BaseLeastSquares,BaseClassifier):
''' Polynomial Least Square Classifier
Parameters
----------
degree : int >=1
degrees of features to consider,ex for 2 features x1,x2 with degree =2,
the input features are going to be x1,x2,x1^2,x2^2
'''
def fit(self,X,y):
self.labels = np.unique(y)
assert len(self.labels) == 2, "Only 2 class can be given to this classifier"
# Renamed labels as 1 and -1
y_new = np.zeros(y.shape)
y_new[y == self.labels[0]] = 1
y_new[y == self.labels[1]] = -1
super().fit(X,y_new)
def predict(self,X):
pred = super().predict(X)
y_hat = np.where(pred >0,self.labels[0],self.labels[1])
return y_hat
class LinearRegression(PolynomialRegression):
'''Least Square Linear Regressor'''
def __init__(self):
self.beta = 0
self.degree = 1
class LinearClassification(PolynomialClassification):
'''Least Square Linear Classifier'''
def __init__(self):
self.beta = 0
self.degree = 1