question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH : about scipy.sparse.csgraph.laplacian

See original GitHub issue

In this issue and associated PR it is said that the function scipy.sparse.csgraph.laplacian with normalization is the symmetric normalized laplacian L_sym = D^-1/2 L D^-1/2, where D is the degree matrix, L is the unnormalized lapalcian (D - A, A being the adjacency matrix).

However, from the toy example below, it doesn’t seem to be the case.While networkx’s output matches with the L_sym formula’s, scipy’s output is quite different.

It looks like in the source code of scipy’s normalized laplacian there’s some extra processing probably to deal with inversion of possible zeros in the diagonal, but it doesn’t seem to make the result right. Following the formula, I modified the source code a bit and it can produce output mostly consistent with networkx’s (my ver part below), though it definitely needs extra handling to make it robust.

Reproducing code example:

from scipy.sparse import csgraph
import numpy as np
import networkx as nx

graph = np.array([[10, 8, 5], [8, 10, 3], [5, 3, 10]]).astype(np.float32)
print("-------------graph-----------\n", graph)

# scipy
L = csgraph.laplacian(graph, normed=True)
print("\n-------------scipy------------\n", L)

# networkx
L_nx = nx.normalized_laplacian_matrix(nx.from_numpy_array(graph)).todense()
print("\n-------------networkx:-------------\n", L_nx)

# ------------ scipy my ver
A = graph.copy()
w = A.sum(axis=0)
w = np.sqrt(w)
n, m = A.shape
diags = A.sum(axis=1).flatten()
A /= w
A /= w[:, np.newaxis]
I = np.identity(3)
my_lap = I - A
print("\n-----------my ver-------------\n", my_lap)

Output of the code above

-------------graph-----------
 [[10.  8.  5.]
 [ 8. 10.  3.]
 [ 5.  3. 10.]]

-------------scipy------------
 [[ 1.         -0.6689936  -0.49029034]
 [-0.6689936   1.         -0.3198011 ]
 [-0.49029034 -0.31980106  1.        ]]

-------------networkx:-------------
 [[ 0.56521739 -0.3640126  -0.24573659]
 [-0.3640126   0.52380952 -0.15430335]
 [-0.24573659 -0.15430335  0.44444444]]

-----------my ver-------------
 [[ 0.56521744 -0.36401257 -0.2457366 ]
 [-0.3640126   0.52380958 -0.15430336]
 [-0.2457366  -0.15430336  0.44444436]]

Scipy/Numpy/Python version information:

scipy:  1.7.0
numpy:  1.19.1
networkx:  2.5

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
czgdp1807commented, Aug 7, 2021

I am able to reproduce your issue on development versions of scipy and netwrokx with numpy as 1.21.1. IMO, for similar APIs, the results from scipy and networkx should match. Though there might be reasons to use a different implementation in scipy as opposed to networkx.

cc @rgommers @fuglede

0reactions
snapfingercommented, Dec 21, 2021

@snapfinger please change the title to “ENH” from “BUG”. This is not a bug.

Thanks, changed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.sparse.csgraph.laplacian — SciPy v1.9.3 Manual
The N x N Laplacian of csgraph. It will be a NumPy array (dense) if the input was dense, or a sparse matrix...
Read more >
ENH: Update laplacian function introducing the new ... - GitHub
Please describe. https://docs.scipy.org/doc/scipy-1.3.0/reference/generated/scipy.sparse.csgraph.laplacian.html does not allow making the ...
Read more >
SciPy 0.18.0 Release Notes — SciPy v0.18.1 Reference Guide
The functions sum, max, mean, min, transpose, and reshape in scipy.sparse ... for a dtype in sparse.random; #5772: Update sparse.csgraph.laplacian docstring ...
Read more >
SciPy Reference Guide - SAVOIR
1.11 Compressed Sparse Graph Routines (scipy.sparse.csgraph) . ... Jacobian corresponding to the Laplace operator part: we know that in one ...
Read more >
Mailman 3 ANN: SciPy 1.9.0 - NumPy-Discussion - python.org
Add a ``dtype`` parameter to `scipy.sparce.csgraph.laplacian` for type casting ... For sparse usage, `scipy.sparse.linalg.expm` needs to be used explicitly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found