def fit(self, X): M, Xm = self._compute_m(X) U = linalg.svds(M, k=2)[0] normal = np.cross(U.T[0], U.T[1]) #normal = numpy.linalg.svd(M)[0][:, 2] if normal[2] < 0: normal *= -1 dist = np.dot(normal, Xm) return dist, normal, M
def SVD(): user_list, user_brand_matrix = generate_userbrand_matrix() U, Sigma, VT = la.svds(user_brand_matrix, k=71) Sigma = np.array(Sigma) U = np.mat(U) VT = np.mat(VT) ''' print Sigma sig2 = Sigma**2 print sum(sig2) print sum(sig2)*0.9 print sum(sig2[-10:])/sum(sig2) print sum(sig2[-15:])/sum(sig2) print sum(sig2[-20:])/sum(sig2) ''' Sig20 = np.mat(np.eye(20) * Sigma[-20:]) print(user_brand_matrix * (VT[-20:, :].T) * Sig20).shape SVD_matrix = user_brand_matrix * (VT[-20:, :].T) * Sig20.I Dict = dict() index = 0 for u in user_list: Dict[u] = np.array(SVD_matrix[index]).reshape(20) index += 1 persistence(Dict, 'SVD_dict.txt')
def SVD(): user_list,user_brand_matrix = generate_userbrand_matrix() U,Sigma,VT = la.svds(user_brand_matrix,k=71) Sigma = np.array(Sigma) U = np.mat(U) VT = np.mat(VT) ''' print Sigma sig2 = Sigma**2 print sum(sig2) print sum(sig2)*0.9 print sum(sig2[-10:])/sum(sig2) print sum(sig2[-15:])/sum(sig2) print sum(sig2[-20:])/sum(sig2) ''' Sig20 = np.mat(np.eye(20)*Sigma[-20:]) print (user_brand_matrix*(VT[-20:,:].T)*Sig20).shape SVD_matrix = user_brand_matrix*(VT[-20:,:].T)*Sig20.I Dict = dict() index = 0 for u in user_list: Dict[u] = np.array(SVD_matrix[index]).reshape(20) index +=1 persistence(Dict,'SVD_dict.txt')
for row in X: mu = np.add(mu, row) mu = np.divide(mu, N) centeredX = np.zeros((N, d)) j = 0 for row in X: centeredX[j, :] = row - mu j = j + 1 # print("centeredX",centeredX[0,:]) # print("mu",mu) # print("X",X[0,:]) #================================================================== # Singular Value decomposition of the centered Data #================================================================== p = 10 u, s, vt = sla.svds(centeredX, k=p) Vp = vt.T Z = np.matmul(centeredX, Vp) #================================================================== # Reconstruction #================================================================== newX = np.zeros((N, d)) newX = np.matmul(Z, Vp.T) j = 0 for row in newX: newX[j, :] = row + mu j = j + 1 print(newX.shape) print(Vp.shape) #================================================================== # Save images