-
Notifications
You must be signed in to change notification settings - Fork 1
/
learn.py
64 lines (51 loc) · 1.64 KB
/
learn.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
# Machine learning for perception neuron data.
import numpy as np
from scipy.optimize import minimize
class LinearModel(object):
def __init__(self):
"""
Produce a separate linear moel for each dimension (correpsonding to column) of target Y.
"""
pass
def fit(self,X,Y):
"""
Simple linear model.
2016-02-20
Params:
-------
"""
nSamples,ndimX = X.shape
# A simple linear model.
linCoeffs = np.zeros((ndimX+1,3))
for i in range(Y.shape[1]):
def f(params):
return ( (Y[:,i] - np.hstack((X,np.ones((nSamples,1)))).dot(params))**2 ).sum()
linCoeffs[:,i] = minimize(f,np.random.normal(size=ndimX+1))['x']
self.linCoeffs = linCoeffs
def predict(self,X):
return X.dot(self.linCoeffs[:-1]) + self.linCoeffs[-1][None,:]
def pca(Y,grouped=False):
"""
PCA on input data with option for doing PCA on groups of columns independently.
2017-02-09
Params:
-------
Y (ndarray)
n_samples x n_dim
grouped (int=False)
If integer, will do PCA on columns grouped in subsets of grouped's value.
"""
if grouped:
L,v,c = [],[],[]
for i in range(Y.shape[1]//grouped):
result = pca(Y[:,i*grouped:(i+1)*grouped])
L.append(result[0])
v.append(result[1])
c.append(result[2])
else:
c = np.cov(Y.T)
L,v = np.linalg.eig(c)
sortix = np.argsort(abs(L))
v = v[:,sortix[::-1]]
L = L[sortix[::-1]]
return L,v,c