Question: I need help converting a few 2D array's into 1D arrays in Python please! The 2D arrays below in the calc function are the ones

I need help converting a few 2D array's into 1D arrays in Python please! The 2D arrays below in the calc function are the ones I need coverted to 1D for p, tildeP and G.

import networkx as nx import numpy as np from copy import deepcopy # #The PageTrust algorithm: #How to rank web pages when negative links are allowed? # # *** warning *** # this code is NOT tested yet. #debug? debug_flag = 1 def visualize(name,array): if debug_flag == 0: return print "===== " + name + " =====" print array def initialize_P(g,negative): N = len(g) P = np.zeros([N,N]) P.flatten() for item in negative: P[item[0]][item[1]] = 1 tildeP = deepcopy(P) visualize('initial P',P) return P,tildeP def build_transition_matrix(alpha,x,g,G,M): N = len(g) T = np.zeros([N,N]) for i in range(N): s = 0 for k in range(N): if (k,i) not in g.edges(): continue s += x[k] / g.out_degree(k,weight='weight') denominator = alpha * s + (1 - alpha) * 1/float(N) for j in range(N): if i == j: continue if (j,i) not in g.edges(): continue numerator = alpha * g[j][i]['weight'] * x[j] / g.out_degree(j,weight='weight') + M * (1 - alpha)*(1/float(N))*x[j] T[i][j] = numerator/denominator visualize('T',T) return T def is_converged(x1,x2): m = 0 for i in range(len(x1)): if (x1[i] - x2[i])**2 > m: m = (x1[i] - x2[i])**2 return m def calc(g,negative,alpha,M,beta=1): epsilon = 0.000000001 print "start calc pagetrust, epsilon =",epsilon N = len(g) print("HERE") print(N) x = np.ones(N) x = x * 1/N visualize("x",x) P,tildeP = initialize_P(g,negative) t = 0 G = nx.google_matrix(g) G = G.flatten() #G = np.reshape(G, (N*N,)) print("MAYBE") print(G) pagerank = nx.pagerank(g,alpha=alpha) visualize("Google matrix",G) t = 0 while True: t += 1 #build the transition matrix T print "***" print "*** iteration start, time = ",t print "***" T = build_transition_matrix(alpha,x,g,G,M) tildeP = np.dot(T,P) visualize("P",P) visualize("tildeP",tildeP) x2 = np.zeros(N) 
#The 2D arrays below are the ones I need coverted to 1D for p, tildeP and G.
 for i in range(N): p = 0 for k in range(N): print(G[i*N+k]*x[k]) p += G[i*N+k]*x[k] print(beta) x2[i] = (1 - tildeP[i*N+i])**beta*p for j in range(N): if (i,j) in negative: P[i,j] = 1 elif i == j: P[i*N+j] = 0 else: P[i*N+j] = tildeP[i*N+j]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!