python - 与 Julia 稀疏矩阵函数中的 to_scipy_sparse_matrix 类似的函数

标签 python julia sparse-matrix

请问Julia语言及其functions是否有等价的功能对于稀疏矩阵 to_scipy_sparse_matrixnetworkx 中。

我正在寻找等效于在 eigenvector centrality algorithm 中调用此函数的函数.

是否有可能如上所述在 Julia 的特征向量中心链接中运行此函数以产生相同的输出?

感谢您的任何建议。我为此苦苦挣扎了几个小时,但无法取得任何结果。

编辑:

Python version :
import networkx as nx
import scipy

G = nx.Graph()
G.add_edge(1, 2, w=1.0 )
G.add_edge(1, 3, w=0.5 )
G.add_edge(2, 3, w=2.5 )

M = nx.to_scipy_sparse_matrix(G, nodelist=list(G), weight='w',dtype=float)

print(M)

Output:
(0, 1)  1.0
(0, 2)  0.5
(1, 0)  1.0
(1, 2)  2.5
(2, 0)  0.5
(2, 1)  2.5

Julia version:
using Graphs

g1 = Graphs.graph(Graphs.ExVertex[], Graphs.ExEdge{Graphs.ExVertex}[],     is_directed=false)
d = "dist"

v1 = add_vertex!(g1, "a")
v2 = add_vertex!(g1, "b")
v3 = add_vertex!(g1, "c")

e12 = add_edge!(g1, v1, v2)
e12.attributes[d]=1.0

e13 = add_edge!(g1, v1, v3)
e13.attributes[d]=0.5

e23 = add_edge!(g1, v2, v3)
e23.attributes[d]=2.5

最佳答案

尝试(遵循 OP Julia 代码):

julia> triple(e,d) = (e.source.index,e.target.index,e.attributes[d])
triple (generic function with 1 method)

julia> M = sparse(map(collect,zip([triple(e,d) for e in edges(g1)]...))...,length(g1.vertices),length(g1.vertices))
    2x3 sparse matrix with 3 Float64 entries:
    [1, 2]  =  1.0
    [1, 3]  =  0.5
    [2, 3]  =  2.5

triple返回一个 (source,target,d-attribute) 三元组,它也可能在其他地方有用。

稀疏矩阵是用 sparse(I,J,D,rows,cols) 创建的构造函数在哪里 I,J,D都是相同长度的向量,对于每个索引 i对于他们来说,矩阵有一个 D[i]位置 I[i],J[i] 的值.

如果需要对称权重矩阵,请使用以下内容:

julia> M = M+M'
3x3 sparse matrix with 6 Float64 entries:
    [2, 1]  =  1.0
    [3, 1]  =  0.5
    [1, 2]  =  1.0
    [3, 2]  =  2.5
    [1, 3]  =  0.5
    [2, 3]  =  2.5

关于python - 与 Julia 稀疏矩阵函数中的 to_scipy_sparse_matrix 类似的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36011590/

相关文章:

python - 如何在Python中使用elasticsearch来搜索所有索引?

python - 在 tkinter GUI 类的方法中运行 while 循环,同时仍然允许 UI 运算符(operator)

python - 为什么Python的curve_fit没有完成优化?

random - 如何在 Julia 中生成一系列随机 float ?

matrix - Julia 中矩阵列的平均值

python - 初始化高维稀疏矩阵

python - 获取数组中字典值的向量,python

java - 如何在 SparseDoubleMatrix2D (Java Colt 库)中找到最大值?甚至在一维矩阵中

python - AWS boto 和 boto3 有什么区别

Julia - 根据向量 reshape 数组