def dice_sim_matrix_po(X): sumX = pnp.sum(X, axis = 1) sumX = pnp.reshape(pnp.tile(sumX, len(X)), (len(X), len(X))) sumX = sumX + pnp.transpose(sumX) cmpX = pnp.zeros((len(X), len(X))) for i in range(len(X)): cmpX[i] = pnp.sum(X * pnp.reshape(pnp.tile(X[i], len(X)), (len(X), len(X[0]))), axis = 1) result = 2 * cmpX * myreciprocal(sumX * sumX, 0.0) result = sumX * result return result
def R2(x, y): if (np.size(x) != np.size(y)): print('Warning: input vectors for R2 must be same length!') return -100.0 else: avx = pnp.mean(x) avy = pnp.mean(y) num = pnp.sum((x - avx) * (y - avy)) num = num * num denom = pnp.sum((x - avx) * (x - avx)) * pnp.sum((y - avy) * (y - avy)) res = num * pp.reciprocal(denom) return res
def fdot(A, B): # flatten dot. Regard A and B as long vector A = pp.farr(A) B = pp.farr(B) A = A * (1.0/A.shape[0]) B = B * (1.0/B.shape[0]) return pnp.sum(A * B)
def jaccard_sim_po(mat): intersect = pnp.dot(mat, pnp.transpose(mat)) union = pnp.sum(mat, axis = 1) union = pnp.reshape(pnp.tile(union, len(mat)), (len(mat), len(mat))) union = union + pnp.transpose(union) - intersect + 0.0 sim = intersect * myreciprocal(union,0.0) for i in range(sim.shape[0]): sim[i, i] = pp.sfixed(1.0) return sim
def RWR_po(A, maxiter, restartProb): n = len(A) # normalize the adjacency matrix A = A + 0.0 tmp_var = pnp.sum(A,axis=0) tmp_var = myreciprocal(tmp_var,0.0) tmp_var = pnp.tile(tmp_var,(A.shape[0],1)) P = A * tmp_var # Personalized PageRank restart = pnp.eye(n) * restartProb Q = pnp.eye(n) for i in range(maxiter): Q = (1 - restartProb) * pnp.dot(P, Q) + restart return Q