#encoding:latin-1 from numpy import * from nnma_utils import * def lnmf(V, rdim): V /= V.max() #scale(V) vdim, samples = V.shape W=random.rand(vdim, rdim)*2 #W=V[:,:rdim] H=random.rand(rdim, samples)*2 balance_matrixpair(W,H) # zeilen auf 1 normieren W /= ones((vdim,1)) * sum(W, axis=0) #while True: for _ in range(1155): # update H XWH = V / (dot(W,H)) H = sqrt( H * dot(W.T, XWH)) # update W ZAEHLER = W * dot(XWH, H.T) NENNER1 = tile(sum(W,axis=1).reshape(-1,1),(1,rdim)) NENNER2 = tile(sum(H.T,axis=0), (vdim,1)) W = ZAEHLER / (NENNER1+ NENNER2) # scale cols of W to 1 W /= ones((vdim,1)) * sum(W, axis=0) #print sum(W, axis=0) print linalg.norm(V-dot(W,H)), sum(dot(W.T,W)), sum(diag(dot(H,H.T))) #print V #print dot(W,H) #print W #print return W,H ONES=ones((3,2)) ZEROS=zeros((3,2)) W =vstack( ( hstack((ONES,ZEROS)) , hstack((ZEROS, ONES)) )) print W print W.shape H = random.rand(2,6) V = dot(W,H) scale(V) Wn, Hn = lnmf(V,4) print dot(Wn,Hn) print V