sknetwork-team / scikit-network

Graph Algorithms
Other
606 stars 66 forks source link

Louvain function fails with ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() #580

Closed vikram223 closed 1 month ago

vikram223 commented 2 months ago

Description

Trying to run the Louvain algo as follows.

louvain = Louvain(resolution=0.5,tol_optimization=0.0001,tol_aggregation=-0.0001,n_aggregations=1000,modularity="newman")
labels = louvain.fit_predict(adj_mat)
print(labels)

It works for a different input in the same format but fails for another input type. (Attached csv here) failed_matrice.csv

Below is the traceback error

$ python GetClusters.py
Traceback (most recent call last):
  File "..../GetClusters.py", line 138, in <module>
    labels = louvain.fit_predict(adj_mat)
  File "..../anaconda3/lib/python3.9/site-packages/sknetwork/clustering/base.py", line 64, in fit_predict
    self.fit(*args, **kwargs)
  File "..../anaconda3/lib/python3.9/site-packages/sknetwork/clustering/louvain.py", line 285, in fit
    self._post_processing(input_matrix, membership, index)
  File "..../anaconda3/lib/python3.9/site-packages/sknetwork/clustering/louvain.py", line 253, in _post_processing
    self._secondary_outputs(input_matrix)
  File "..../anaconda3/lib/python3.9/site-packages/sknetwork/clustering/base.py", line 151, in _secondary_outputs
    self.probs_ = normalize(input_matrix.dot(probs))
  File "..../anaconda3/lib/python3.9/site-packages/sknetwork/linalg/normalizer.py", line 82, in normalize
    norms = get_norms(matrix, p)
  File "..../anaconda3/lib/python3.9/site-packages/sknetwork/linalg/normalizer.py", line 49, in get_norms
    input_matrix = sparse.csr_matrix(matrix)
  File "..../anaconda3/lib/python3.9/site-packages/scipy/sparse/_compressed.py", line 86, in __init__
    self._coo_container(arg1, dtype=dtype)
  File "..../anaconda3/lib/python3.9/site-packages/scipy/sparse/_coo.py", line 90, in __init__
    row, col = M.nonzero()
  File "/..../anaconda3/lib/python3.9/site-packages/scipy/sparse/_base.py", line 387, in __bool__
    raise ValueError("The truth value of an array with more than one "
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

My suspicion is that its related to this issue

tbonald commented 1 month ago

Hello, Could you please specify the format of your input matrix? Thanks.

tbonald commented 1 month ago

Hello again, I guess the input matrix was a numpy array. Please use a sparse format of scipy for now. The next release will allow a numpy array (#626). Thanks.