-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinearDisc.py
More file actions
78 lines (58 loc) · 2.38 KB
/
linearDisc.py
File metadata and controls
78 lines (58 loc) · 2.38 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
import numpy as np
from numpy.linalg import inv
def inputMat(row, col):
mat = []
for i in range(row):
mat.append([])
for j in range(col):
mat[i].append(j)
print("element[", i+1,"][", j+1, "]")
mat[i][j] = float(input())
return mat
def matrixMul(A, B):
res = []
for i in range(len(A)):
res.append([])
for j in range(len(B[0])):
res[i].append(j)
for k in range(len(B)):
res[i][j] += A[i][k] * B[k][j]
return res
'''
linearDisc() computes the linear discriminant function value
for a given data point in d-dimension denoted by 'dim'
'''
def linearDisc(x, prior, mean, cov):
XminusM = [] #to store the matrix of (x-mean)
XminusM_ = [] #to store the matrix of (x-mean)'
dim = len(x)
for i in range(dim):
XminusM.append([])
for j in range(1):
XminusM[i].append(j)
XminusM[i][0] = x[i][0] - mean[i][0]
XminusM_ = np.transpose(XminusM) #transpose of (x-mean) matrix
inv_cov = inv(cov) #inv_cov holds inverse of covariance matrix
#temp stores the multiplication matrix of (x-mean)' and inverse of covariance
temp = matrixMul(XminusM_, inv_cov)
#tempTwo stores the multiplication result of temp and (x-mean) matrix
tempTwo = matrixMul(temp, XminusM)
value = ((-1/2) * tempTwo[0][0]) + np.log(prior)
return value
def quadDisc(x, prior, mean, cov):
XminusM = [] #to store the matrix of (x-mean)
XminusM_ = [] #to store the matrix of (x-mean)'
dim = len(x)
for i in range(dim):
XminusM.append([])
for j in range(1):
XminusM[i].append(j)
XminusM[i][0] = x[i][0] - mean[i][0]
XminusM_ = np.transpose(XminusM) #transpose of (x-mean) matrix
inv_cov = inv(cov) #inv_cov holds inverse of covariance matrix
#temp stores the multiplication matrix of (x-mean)' and inverse of covariance
temp = matrixMul(XminusM_, inv_cov)
#tempTwo stores the multiplication result of temp and (x-mean) matrix
tempTwo = matrixMul(temp, XminusM)
value = ((-1/2) * tempTwo[0][0]) - ((1/2)*np.log(np.linalg.det(cov))) + np.log(prior)
return value