def cca(X, Y): # print '#### Canonical Correlation Analyais ####' # print 'X\n', X # print 'Y\n', Y xsamples, n = X.shape ysamples, m = Y.shape # print 'xsamples=', xsamples, 'n=', n # print 'ysamples=', ysamples, 'm=', m if (xsamples != ysamples): print 'The number of samples are inconsistent' else: nsamples = xsamples XMean = sum(X) / float(nsamples) YMean = sum(Y) / float(nsamples) # print 'XMean', XMean # print 'YMean', YMean XX = X - XMean YY = Y - YMean SXX = np.dot(XX.T, XX) / float(nsamples - 1) SYY = np.dot(YY.T, YY) / float(nsamples - 1) SXY = np.dot(XX.T, YY) / float(nsamples - 1) SYYinv = np.linalg.inv(SYY) SXYYYXY = np.dot(SXY, np.dot(SYYinv, SXY.T)) Eval, Evec = klin.pencil(SXYYYXY, SXX) idx = Eval.argsort() Eval = Eval[idx][::-1] Evec = Evec[:, idx][:, ::-1] A = Evec # print 'sqrt(Eval)', np.sqrt(Eval) # print 'A\n', A # U = np.dot(XX, A) # print 'U\n', U # print 'inv(Eval)', 1 / np.sqrt(Eval) Evalinv = 1.0 / np.sqrt(Eval) B = np.dot(np.dot(SYYinv, np.dot(SXY.T, A)), np.diag(Evalinv)) # print 'B\n', B # V = np.dot(YY, B) # print 'V\n', V return (A, B, XMean, YMean)
def danal(data,label,K): nr, nc = data.shape ldim = np.minimum(nc, K-1) # Total mean and Total covrainace matrix # TMEAN = (sum(np.asarray(data,dtype=np.float))) / float(nr) TMEAN = sum(data) / float(nr) data_m = data - TMEAN St = np.dot(data_m.T, data_m)/float(nr) # print 'TMEAN' # print TMEAN # print 'Total Covaraince Matrix' # print St # # Class Means # NS = np.zeros((K, 1)) XMEAN = np.zeros((K, nc)) # # for i in range(nr): # k = label[i]; # NS[k] = NS[k] + 1 # for j in range(nc): # XMEAN[k,j] = XMEAN[k,j] + data[i,j] # # for k in range(K): # omega = NS[k] # for j in range(nc): # XMEAN[k,j] = XMEAN[k,j] / omega # # print 'Class Means' # print XMEAN NE = np.zeros((K)) for k in range(K): NE[k] = sum(label == k) for k in range(K): XMEAN[k,:] = np.dot(data[ label == k, :].T, np.ones((int(NE[k])))).T / float(NE[k]) # print 'Class Means' # print XMEAN # Between Class Covariance Matrix # Sb = np.zeros((nc, nc)) # for k in range(K): # omega = sum(label == k) / float(nr) # for i in range(nc): # for j in range(nc): # Sb[i,j] = Sb[i,j] + omega * (XMEAN[k,i] - TMEAN[i]) * (XMEAN[k,j] - TMEAN[j]) # # print 'Between Class Covariance Matrix' # print Sb # alpha = 0.001 Sb = np.zeros((nc, nc)) for k in range(K): omega = float(NE[k]) / float(nr) xmean_m = XMEAN[k,:] - TMEAN Sb = Sb + omega * np.outer(xmean_m, xmean_m) # print 'Between Class Covariance Matrix' # print Sb.dtype # print Sb Sw = St - Sb # Sw = Sw + alpha * np.eye(nc) # print 'Within Class Covariance Matrix' # print Sw.dtype # print Sw Eval, Evec = klin.pencil(Sb,Sw) return (Eval[:ldim], Evec[:,:ldim], TMEAN, XMEAN)
def danal(data, label, K): nr, nc = data.shape ldim = np.minimum(nc, K - 1) # Total mean and Total covrainace matrix # TMEAN = (sum(np.asarray(data,dtype=np.float))) / float(nr) TMEAN = sum(data) / float(nr) data_m = data - TMEAN St = np.dot(data_m.T, data_m) / float(nr) # print 'TMEAN' # print TMEAN # print 'Total Covaraince Matrix' # print St # # Class Means # NS = np.zeros((K, 1)) XMEAN = np.zeros((K, nc)) # # for i in range(nr): # k = label[i]; # NS[k] = NS[k] + 1 # for j in range(nc): # XMEAN[k,j] = XMEAN[k,j] + data[i,j] # # for k in range(K): # omega = NS[k] # for j in range(nc): # XMEAN[k,j] = XMEAN[k,j] / omega # # print 'Class Means' # print XMEAN NE = np.zeros((K)) for k in range(K): NE[k] = sum(label == k) for k in range(K): XMEAN[k, :] = np.dot(data[label == k, :].T, np.ones( (int(NE[k])))).T / float(NE[k]) # print 'Class Means' # print XMEAN # Between Class Covariance Matrix # Sb = np.zeros((nc, nc)) # for k in range(K): # omega = sum(label == k) / float(nr) # for i in range(nc): # for j in range(nc): # Sb[i,j] = Sb[i,j] + omega * (XMEAN[k,i] - TMEAN[i]) * (XMEAN[k,j] - TMEAN[j]) # # print 'Between Class Covariance Matrix' # print Sb # alpha = 0.001 Sb = np.zeros((nc, nc)) for k in range(K): omega = float(NE[k]) / float(nr) xmean_m = XMEAN[k, :] - TMEAN Sb = Sb + omega * np.outer(xmean_m, xmean_m) # print 'Between Class Covariance Matrix' # print Sb.dtype # print Sb Sw = St - Sb # Sw = Sw + alpha * np.eye(nc) # print 'Within Class Covariance Matrix' # print Sw.dtype # print Sw Eval, Evec = klin.pencil(Sb, Sw) return (Eval[:ldim], Evec[:, :ldim], TMEAN, XMEAN)
import klin m = 5 data = [ [0.011, 0.012, 0.013, 0.014, 0.015], [0.012, 0.022, 0.023, 0.024, 0.025], [0.013, 0.023, 0.033, 0.034, 0.035], [0.014, 0.024, 0.034, 0.044, 0.045], [0.015, 0.025, 0.035, 0.045, 0.055], ] A = np.array(data) B = np.zeros((m, m)) for i in range(m): B[i, i] = i + 1.0 print "A" print A print "B" print B Eval, Evec = klin.pencil(A, B) print "Eval is" print Eval print "Evec is" print Evec
import numpy as np import klin m = 5 data = [[0.011, 0.012, 0.013, 0.014, 0.015], [0.012, 0.022, 0.023, 0.024, 0.025], [0.013, 0.023, 0.033, 0.034, 0.035], [0.014, 0.024, 0.034, 0.044, 0.045], [0.015, 0.025, 0.035, 0.045, 0.055]] A = np.array(data) B = np.zeros((m, m)) for i in range(m): B[i, i] = i + 1.0 print 'A' print A print 'B' print B Eval, Evec = klin.pencil(A, B) print 'Eval is' print Eval print 'Evec is' print Evec
def cca(X, Y): # print '#### Canonical Correlation Analyais ####' # print 'X\n', X # print 'Y\n', Y xsamples, n = X.shape ysamples, m = Y.shape # print 'xsamples=', xsamples, 'n=', n # print 'ysamples=', ysamples, 'm=', m if (xsamples != ysamples): print 'The number of samples are inconsistent' else: nsamples = xsamples XMean = sum(X) / float(nsamples) YMean = sum(Y) / float(nsamples) # print 'XMean', XMean # print 'YMean', YMean XX = X - XMean YY = Y - YMean SXX = np.dot(XX.T, XX) / float(nsamples-1) SYY = np.dot(YY.T, YY) / float(nsamples-1) SXY = np.dot(XX.T, YY) / float(nsamples-1) SYYinv = np.linalg.inv(SYY) SXYYYXY = np.dot(SXY,np.dot(SYYinv, SXY.T)) Eval, Evec = klin.pencil(SXYYYXY,SXX) idx= Eval.argsort() Eval = Eval[idx][::-1] Evec = Evec[:,idx][:,::-1] A = Evec # print 'sqrt(Eval)', np.sqrt(Eval) # print 'A\n', A # U = np.dot(XX, A) # print 'U\n', U # print 'inv(Eval)', 1 / np.sqrt(Eval) Evalinv = 1.0 / np.sqrt(Eval) B = np.dot(np.dot(SYYinv, np.dot(SXY.T, A)), np.diag(Evalinv)) # print 'B\n', B # V = np.dot(YY, B) # print 'V\n', V return (A, B, XMean, YMean)
def su3(data): EPS = 1.0e-6 m, n = data.shape if (m < n): minmn = m else: minmn = n total = np.sum(data) # R = np.zeros((m,n)) R = data / float(total) #################### P = np.dot(R, np.ones((n))) Pinv = np.zeros((m)) for i in range(m): if (P[i] >= EPS): Pinv[i] = 1.0 / P[i] Q = np.dot(np.ones((m)), R) Qinv = np.zeros((n)) for i in range(n): if (Q[i] >= EPS): Qinv[i] = 1.0 / Q[i] # print 'R\n', R # print 'P\n', P # print 'Q\n', Q # Solve the eigen problem with smaller dimension if (m <= n): # R Qinv R^T UU = np.dot(np.dot(R, np.diag(Qinv)), R.T) W = np.diag(P) Eval, Evec = klin.pencil(UU, W) E = Eval[1:] U = Evec[:,1:] Einv = np.zeros(m-1) for i in range(m-1): if (E[i] >= EPS): Einv[i] = 1.0 / np.sqrt(E[i]) # print 'R\n', R # print 'Einv\n', Einv V = np.dot(np.dot(np.dot(np.diag(Qinv), R.T), U), np.diag(Einv)) else: # VV = R^T Pinv R VV = np.dot(np.dot(R.T, np.diag(Pinv)), R) W = np.diag(Q) Eval, Evec = klin.pencil(VV, W) E = Eval[1:] V = Evec[:,1:] Einv = np.zeros(n-1) for i in range(n-1): if (E[i] >= EPS): Einv[i] = 1.0 / np.sqrt(E[i]) U = np.dot(np.dot(np.dot(np.diag(Pinv), R), V), np.diag(Einv)) # remove the first eigen value and eigen vector return (E, U, V)