LDA test

788 days 전, jhlee2chn 작성

data1 = matrix(RDF, [[4, 1], [2, 4], [2, 3], [3, 6], [4, 4]]) data2 = matrix(RDF, [[9, 10], [6, 8], [9, 5], [8, 7], [10, 8]]) I = vector([1, 1, 1, 1, 1]) mu1 = 1/5*I*data1 mu2 = 1/5*I*data2 print mu1 print mu2 
       
(3.0, 3.6)
(8.4, 7.6)
(3.0, 3.6)
(8.4, 7.6)
A = column_matrix([data1[i] - mu1 for i in range(0,5)]) S1 = A*A.transpose() print S1 
       
[ 4.0 -2.0]
[-2.0 13.2]
[ 4.0 -2.0]
[-2.0 13.2]
B = column_matrix([data2[i] - mu2 for i in range(0,5)]) S2 = B*B.transpose() print S2 
       
[ 9.2 -0.2]
[-0.2 13.2]
[ 9.2 -0.2]
[-0.2 13.2]
S_W = S1 + S2 print S_W 
       
[13.2 -2.2]
[-2.2 26.4]
[13.2 -2.2]
[-2.2 26.4]
C = column_matrix([mu1 - mu2]) S_B = C*C.transpose() print S_B 
       
[29.16  21.6]
[ 21.6  16.0]
[29.16  21.6]
[ 21.6  16.0]
print S_W.inverse()*S_B 
       
[ 2.37848911652  1.76184379001]
[ 1.01638924456 0.752880921895]
[ 2.37848911652  1.76184379001]
[ 1.01638924456 0.752880921895]
(S_W.inverse()*S_B).eigenvalues() 
       
[3.13137003841, -1.11022302463e-16]
[3.13137003841, -1.11022302463e-16]
(S_W.inverse()*S_B).eigenvectors_right() 
       
[(3.13137003841, [(0.919559317646, 0.39295122004)], 1),
(-1.11022302463e-16, [(-0.595227550626, 0.803557193345)], 1)]
[(3.13137003841, [(0.919559317646, 0.39295122004)], 1), (-1.11022302463e-16, [(-0.595227550626, 0.803557193345)], 1)]
w = vector([0.919559317646, 0.39295122004]) w*S_W*w 
       
13.6483161241950
13.6483161241950
w*S_B*w 
       
42.7379281860836
42.7379281860836
42.7379281860836/13.6483161241950 
       
3.13137003841229
3.13137003841229
p1 = point(data1.rows()) p2 = point(data2.rows(), color = 'red') var('t') p3 = parametric_plot(t*w, (t, 2, 13), color = 'black') p1 + p2 + p3