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

Similar function to to_scipy_sparse_matrix in Julia sparse matrices functions

我想问一下 Julia 语言和它的 functions for sparse matrices to to_scipy_sparse_matrixnetworkx 中是否有等效的功能。

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

是否有可能 运行 这个函数如上所述,在特征向量中心性 link 中,在 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 returns 一个(源,目标,d-属性)三元组,可能在其他地方也有用。

稀疏矩阵是使用 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