def slim_sqrtm(V): u, s = spm_svd(V, 0) s = np.sqrt(np.abs(np.diag(s))) m = len(s) s = np.sparse(np.arange(0, m), np.arange(0, m), s) K = u * s * u.T return K
def create_matrix(self, movies, users, sset): movie_count = int(self.movies[-1][0]) + 1 user_count = len(self.users) + 1 matrix = np.sparse((user_count, movie_count), 0) for split in sset: for rating in split: user_id = int(rating[0]) movie_id = int(rating[1]) u_rating = int(rating[2]) matrix[user_id, movie_id] = u_rating return matrix
def sample_zs_init(data_struct,dist_struct,obsModelType): #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # Define and initialize parameters % #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Kz = np.shape(dist_struct[1]['pi_z'][1]) Ks = np.shape(dist_struct[1]['pi_s'][1]) # Preallocate INDS for ii in range(0,len(data_struct)): T = len(data_struct[ii]['blockSize']) INDS[ii][obsIndzs[0:Kz,0:Ks]] = {'inds':np.sparse(1,T),'tot':0} for ii in range(0,len(data_struct)): N_init = np.zeros((Kz+1,Kz)) Ns_init = np.zeros((Kz,Ks)) if 'z_init' in data_struct[ii].keys(): z_init_ii = data_struct[ii]['z_init'] else: z_init_ii = data_struct[ii]['true_labels'] stateSeq[ii]['z'],stateSeq[ii]['s'],totSeq,indSeq,N[:,:,ii],Ns[:,:,ii] = setZtoFixedSeq(data_struct[ii],dist_struct[ii],N_init,Ns_init,z_init_ii,0) for jj in range(0,Kz): for kk in range(0,Ks): INDS[ii][obsIndzs[jj,kk]]['tot'] = totSeq[jj,kk] INDS[ii][obsIndzs[jj,kk]]['inds'] = scipy.sparse(indSeq[:,jj,kk].T) stateCounts['N'] = N stateCounts['Ns'] = Ns return (stateSeq,INDS,stateCounts)
def sample_zs_init(data_struct, dist_struct, obsModelType): #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # Define and initialize parameters % #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Kz = np.shape(dist_struct[1]['pi_z'][1]) Ks = np.shape(dist_struct[1]['pi_s'][1]) # Preallocate INDS for ii in range(0, len(data_struct)): T = len(data_struct[ii]['blockSize']) INDS[ii][obsIndzs[0:Kz, 0:Ks]] = {'inds': np.sparse(1, T), 'tot': 0} for ii in range(0, len(data_struct)): N_init = np.zeros((Kz + 1, Kz)) Ns_init = np.zeros((Kz, Ks)) if 'z_init' in data_struct[ii].keys(): z_init_ii = data_struct[ii]['z_init'] else: z_init_ii = data_struct[ii]['true_labels'] stateSeq[ii]['z'], stateSeq[ii][ 's'], totSeq, indSeq, N[:, :, ii], Ns[:, :, ii] = setZtoFixedSeq( data_struct[ii], dist_struct[ii], N_init, Ns_init, z_init_ii, 0) for jj in range(0, Kz): for kk in range(0, Ks): INDS[ii][obsIndzs[jj, kk]]['tot'] = totSeq[jj, kk] INDS[ii][obsIndzs[jj, kk]]['inds'] = scipy.sparse(indSeq[:, jj, kk].T) stateCounts['N'] = N stateCounts['Ns'] = Ns return (stateSeq, INDS, stateCounts)