2014-09-22 43 views
4

這個例子(從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 

我應該怎麼去讓它工作?

回答

5

這個例子肯定與newe斷了r版本的NetworkX。 這是一個工程:

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() 
1

我覺得在this commit移除該名稱:

Deprecate non-"matrix" names in laplacian. 

Use laplacian_matrix, directed_laplacian_matrix, and normalized_laplacian_matrix only and deprecate other name aliases. 

networkx/linalg/laplacianmatrix.py更改的行包括

-combinatorial_laplacian=laplacian_matrix 
-generalized_laplacian=normalized_laplacian_matrix 
-normalized_laplacian=normalized_laplacian_matrix 
-laplacian=laplacian_matrix 

所以我認爲你可以使用normalized_laplacian_matrix代替:

>>> normalized_laplacian_matrix(G) 
<1000x1000 sparse matrix of type '<type 'numpy.float64'>' 
    with 11000 stored elements in Compressed Sparse Row format> 
+0

謝謝!我現在得到這個錯誤,雖然「追溯(最近呼叫最後): 文件」無標題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