此示例(来自Networkx的手册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 (hagberg@lanl.gov)"""
__credits__ = """"""
# Copyright (C) 2004-2006 by
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Pieter Swart <swart@lanl.gov>
# 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
我该怎么去才能让它发挥作用?
解决方法:
这个例子肯定会被更新版本的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()
标签:python,networkx 来源: https://codeday.me/bug/20190528/1172727.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。