sophiadw
sophiadw

Reputation: 567

networkx calculating numeric assortativity requires int?

I am trying to use networkx to calculate numeric assortativity based on a numeric attribute that I set to nodes. My node attributes are floats. When I call the assortativity function:

assort = nx.numeric_assortativity_coefficient(G,'float_attr')

I got the following errors.

File "/some dir.../networkx/algorithms/assortativity/correlation.py", line 229, in numeric_assortativity_coefficient
a = numeric_mixing_matrix(G,attribute,nodes)
File "/some dir.../networkx/algorithms/assortativity/mixing.py", line 193, in numeric_mixing_matrix
mapping=dict(zip(range(m+1),range(m+1)))
TypeError: range() integer end argument expected, got float.

I checked the documentation page of networkx assortativity algorithm and it did not say the numeric attributes have to be int. Anyone knows if that's required?

BTW, I used the same network and a gender attribute (set to 0 and 1) to calculate both the attribute and the numeric assortativity. I had no problem with that. So it seems that the problem is with the int/float type of the node attribute.

Upvotes: 3

Views: 783

Answers (1)

sophiadw
sophiadw

Reputation: 567

problem solved by converting the float variable into int using the following method

int(round(float_attr*1000, 0))

submitted an issue here and got a confirmatory answer that it only deals with discrete int values.

Peformance-wise, since my network is not huge (200+ nodes), it still takes <1 min to do the calculation.

Upvotes: 2

Related Questions