這個例子(從Networkx手冊http://networkx.github.io/documentation/latest/examples/advanced/eigenvalues.html):Networkx的「generalized_laplacian()」不再工作了嗎?
#!/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
引發與networkx的最新版本以下錯誤:
Traceback (most recent call last):
File "Untitled 2.py", line 36, in <module>
L=generalized_laplacian(G)
NameError: name 'generalized_laplacian' is not defined
我應該怎麼去讓它工作?
謝謝!我現在得到這個錯誤,雖然「追溯(最近呼叫最後): 文件」無標題2.py「,第40行,在 e = eigenvalues(L) File」/Library/Python/2.7/site-packages/ numpy的/ linalg/linalg.py」,線路886,在eigvals _assertRankAtLeast2的(a) 文件 「/Library/Python/2.7/site-packages/numpy/linalg/linalg.py」,線202,在_assertRankAtLeast2 「在最小二維'%len(a.shape)) numpy.linalg.linalg.LinAlgError:給出的0維數組。數組必須至少爲二維「 我猜L的格式不正確? –
Rodolphe
2014-09-22 19:46:54