Reputation: 5345
This example (from Networkx's manual http://networkx.github.io/documentation/latest/examples/advanced/eigenvalues.html):
#!/usr/bin/env python
"""
Create an G{n,m} random graph and compute the eigenvalues.
Requires numpy or LinearAlgebra package from Numeric Python.
Uses optional pylab plotting to produce histogram of eigenvalues.
"""
__author__ = """Aric Hagberg ([email protected])"""
__credits__ = """"""
# Copyright (C) 2004-2006 by
# Aric Hagberg <[email protected]>
# Dan Schult <[email protected]>
# Pieter Swart <[email protected]>
# All rights reserved.
# BSD license.
from networkx import *
try:
import numpy.linalg
eigenvalues=numpy.linalg.eigvals
except ImportError:
raise ImportError("numpy can not be imported.")
try:
from pylab import *
except:
pass
n=1000 # 1000 nodes
m=5000 # 5000 edges
G=gnm_random_graph(n,m)
L=generalized_laplacian(G)
e=eigenvalues(L)
print("Largest eigenvalue:", max(e))
print("Smallest eigenvalue:", min(e))
# plot with matplotlib if we have it
# shows "semicircle" distribution of eigenvalues
try:
hist(e,bins=100) # histogram with 100 bins
xlim(0,2) # eigenvalues between 0 and 2
show()
except:
pass
Raises the following error with networkx's latest version:
Traceback (most recent call last):
File "Untitled 2.py", line 36, in <module>
L=generalized_laplacian(G)
NameError: name 'generalized_laplacian' is not defined
How should I go to make it work?
Upvotes: 4
Views: 908
Reputation: 25299
That example is definitely broken with newer versions of NetworkX. Here is one that works:
import networkx as nx
import numpy.linalg
import matplotlib.pyplot as plt
n = 1000 # 1000 nodes
m = 5000 # 5000 edges
G = nx.gnm_random_graph(n,m)
L = nx.normalized_laplacian_matrix(G)
e = numpy.linalg.eigvals(L.A)
print("Largest eigenvalue:", max(e))
print("Smallest eigenvalue:", min(e))
plt.hist(e,bins=100) # histogram with 100 bins
plt.xlim(0,2) # eigenvalues between 0 and 2
plt.show()
Upvotes: 5
Reputation: 353389
I think that name was removed in this commit:
Deprecate non-"matrix" names in laplacian.
Use laplacian_matrix, directed_laplacian_matrix, and normalized_laplacian_matrix only and deprecate other name aliases.
The lines it changed from networkx/linalg/laplacianmatrix.py
include
-combinatorial_laplacian=laplacian_matrix
-generalized_laplacian=normalized_laplacian_matrix
-normalized_laplacian=normalized_laplacian_matrix
-laplacian=laplacian_matrix
So I think you can use normalized_laplacian_matrix
instead:
>>> normalized_laplacian_matrix(G)
<1000x1000 sparse matrix of type '<type 'numpy.float64'>'
with 11000 stored elements in Compressed Sparse Row format>
Upvotes: 1