I'm trying to create a population of adjacency matrices with different random weights for which I am using the code underneath. My problem however is that upon running this, all the weights are that of the last iteration of the list comprehension? On printing these adjacencylists
during generation, it works fine, however, the output of the getPopulation
function is 5 times the same parameter set.
It feels like this would be an easy fix, but something (I think possibly very basic) is missing. Maybe a problem where deep copy is needed or something?
Already tried using normal for-loops, print statements etc.
import networkx as nx
import numpy as np
G = nx.DiGraph()
G.add_nodes_from(["Sadness", "Avoidance", "Guilt"])
G.add_edges_from([("Sadness", "Avoidance")], weight=1)
G.add_edges_from([("Avoidance", "Sadness")], weight=1)
G.add_edges_from([("Avoidance", "Guilt"), ("Guilt", "Sadness")], weight=1)
parameters = nx.to_numpy_matrix(G)
def getRandParamValue(adj):
for p in np.transpose(adj.nonzero()):
adj[p[0], p[1]] = adj[p[0], p[1]] * np.random.uniform()
print(adj)
return adj
def getPopulation(size, initParam):
return [getRandParamValue(initParam) for i in range(size)]
getPopulation(5, parameters)
Upon printing the output in the getRandParamValue function it works fine:
[[0. 0.40218464 0. ]
[0.07330473 0. 0.7196376 ]
[0.53148413 0. 0. ]]
[[0. 0.34256617 0. ]
[0.01773899 0. 0.12460768]
[0.1401687 0. 0. ]]
[[0. 0.11086942 0. ]
[0.01449088 0. 0.04592752]
[0.07903259 0. 0. ]]
[[0. 0.01970867 0. ]
[0.00589168 0. 0.00860802]
[0.06942081 0. 0. ]]
[[0. 0.01045412 0. ]
[0.00084878 0. 0.00713334]
[0.0024654 0. 0. ]]
However, the output of getPopulation
isn't identical to the previous output, while this should be expected:
[matrix([[0. , 0.01045412, 0. ],
[0.00084878, 0. , 0.00713334],
[0.0024654 , 0. , 0. ]]),
matrix([[0. , 0.01045412, 0. ],
[0.00084878, 0. , 0.00713334],
[0.0024654 , 0. , 0. ]]),
matrix([[0. , 0.01045412, 0. ],
[0.00084878, 0. , 0.00713334],
[0.0024654 , 0. , 0. ]]),
matrix([[0. , 0.01045412, 0. ],
[0.00084878, 0. , 0.00713334],
[0.0024654 , 0. , 0. ]]),
matrix([[0. , 0.01045412, 0. ],
[0.00084878, 0. , 0.00713334],
[0.0024654 , 0. , 0. ]])]
The parameters matrix is just the following:
[[0. 1. 0.]
[1. 0. 1.]
[1. 0. 0.]]