#!/usr/bin/python2
#-*-coding:UTF-8-*-
from numpy import *
#这里的posSet,negSet也是没有标签在末尾元素上的
def lda(posSet,negSet): #The last element of one row is the label ! 0 or 1
posMeanRow=mean(posSet,axis=0,keepdims=False)
negMeanRow=mean(negSet,axis=0,keepdims=False)
posSet-=posMeanRow; negSet-=negMeanRow #The rows of posSet and negSet are vectors which have been centralized !
Sw=mat(posSet).T*mat(posSet)+mat(negSet).T*mat(negSet)
U,S,VT=linalg.svd(Sw) # S is kind of array which consists of singular values
S=mat(diag(S)) # now S is of matrix type
V=VT.T
oppoS=S.I
UT=U.T
oppoSw=V*oppoS*UT # Get the Sw^-1
dfRow=posMeanRow-negMeanRow
dfVecMat=mat(dfRow).T
W=oppoSw*dfVecMat
posCentVal=posMeanRow*W
negCentVal=negMeanRow*W
return W,posCentVal,negCentVal #return the W,posMeanRow,negMeanRow
#W必须是矩阵类型
def ldaClassify(W,row,posCent,negCent):
print "The shape of row : ",shape(row)
print "The shape of W : ",shape(W)
projectVal=row*W
if abs(projectVal-posCent)<abs(projectVal-negCent):
return 1 #离正样本的中心投影更近的话,认定为正样本,返回1作为测试样本的标签
else:
return 0
#-*-coding:UTF-8-*-
from numpy import *
#这里的posSet,negSet也是没有标签在末尾元素上的
def lda(posSet,negSet): #The last element of one row is the label ! 0 or 1
posMeanRow=mean(posSet,axis=0,keepdims=False)
negMeanRow=mean(negSet,axis=0,keepdims=False)
posSet-=posMeanRow; negSet-=negMeanRow #The rows of posSet and negSet are vectors which have been centralized !
Sw=mat(posSet).T*mat(posSet)+mat(negSet).T*mat(negSet)
U,S,VT=linalg.svd(Sw) # S is kind of array which consists of singular values
S=mat(diag(S)) # now S is of matrix type
V=VT.T
oppoS=S.I
UT=U.T
oppoSw=V*oppoS*UT # Get the Sw^-1
dfRow=posMeanRow-negMeanRow
dfVecMat=mat(dfRow).T
W=oppoSw*dfVecMat
posCentVal=posMeanRow*W
negCentVal=negMeanRow*W
return W,posCentVal,negCentVal #return the W,posMeanRow,negMeanRow
#W必须是矩阵类型
def ldaClassify(W,row,posCent,negCent):
print "The shape of row : ",shape(row)
print "The shape of W : ",shape(W)
projectVal=row*W
if abs(projectVal-posCent)<abs(projectVal-negCent):
return 1 #离正样本的中心投影更近的话,认定为正样本,返回1作为测试样本的标签
else:
return 0