I have a problem and I was hoping you can help me. So I'm writing a code to calculate the shortest path length using networkx and numpy, so I create a function to do this,
def costos(red, precios):
pathlength = []
for i in redes: #3d array with 100 2D arrays
graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
pathlength.append(nx.shortest_path_length(graph, 0, 1, weight = 'weight'))
y = np.array(pathlenght)
z = np.shape(y)
return y, z
and when I print the result I got the next output
[25, 10, 32, ..., 20] #A 1D array with 100 elements (shortest path length)
(100,) #Shape of the 1D array
What I want is to transform this 1D array with size of (100,) to a 2D array of size (10, 10), I know I can use np.reshape but when I add this to my function like this
for i in redes: #3d array with 100 arrays
graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
pathlength.append(nx.shortest_path_length(graph, 0, 1, weight = 'weight'))
y = np.array(pathlenght)
z = np.shape(y)
w = np.reshape(y, (10,10))
I get the next Value Error
cannot reshape array of size 1 into shape (10,10)
What I'm doing wrong? I tried different things but nothing seems to work, so any help will be appreciated, thank you!
pathlengthis probably a list of arrays. Which cannot be reshapedpathlenght, and (100,) shapeyis the result of the last loop (the previousyvalues are thrown away). In the 2nd case you try toreshapebeforeycontains all 100 samples. All of they,z,wcode should be outside of theforloop.