-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnormEquations.py
More file actions
45 lines (35 loc) · 814 Bytes
/
normEquations.py
File metadata and controls
45 lines (35 loc) · 814 Bytes
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
'''
Created on May 29, 2014
@author Rainicy
'''
from numpy import *
def normEquations(X, y, options):
'''
Description: Use Normal Equations Linear Regression to calculate the theta.
theta = (X.T * X).Inverse * X.T * y
@param:
X: training features
y: training labels
options: 1) regularized: True regularization
2) lambda: regularization parameter
@return:
theta: the parameters
'''
m, n = shape(X)
X = mat(X)
y = mat(y).T
# X.T * X
XTX = X.T * X
# update theta
### 1) Nomal:
##### theta = (X.T * X).Inv * X.T * y
### 2) Regilarization:
##### theta = (X.T*X + lambda*eye[]
if not options['regularized']:
theta = (XTX.I * X.T) * y
else:
diag = eye(n, n)
diag[0, 0] = 0
theta = (XTX + options['lambda']*diag).I * X.T * y
# print options['lambda']
return theta