-
Notifications
You must be signed in to change notification settings - Fork 1
/
LLSR.py
34 lines (29 loc) · 1.01 KB
/
LLSR.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
# 2020.01.28
# @yifan
#
import numpy as np
class LLSR():
def __init__(self, onehot=True, normalize=False):
self.onehot = onehot
self.normalize = normalize
self.weight = []
def fit(self, X, Y):
if self.onehot == True:
Y = np.eye(len(np.unique(Y)))[Y.reshape(-1)]
A = np.ones((X.shape[0], 1))
X = np.concatenate((X, A), axis=1)
self.weight, _, _, _ = np.linalg.lstsq(X, Y, rcond=None)
return self
def predict(self, X):
pred = self.predict_proba(X)
return np.argmax(pred, axis=1)
def predict_proba(self, X):
A = np.ones((X.shape[0], 1))
X = np.concatenate((X, A), axis=1)
pred = np.matmul(X, self.weight)
if self.normalize == True:
pred = (pred - np.min(pred, axis=1, keepdims=True))/ np.sum((pred - np.min(pred, axis=1, keepdims=True) + 1e-15), axis=1, keepdims=True)
return pred
def score(self, X, Y):
pred = self.predict(X)
return accuracy_score(Y, pred)