Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Weighted Graph from Julia DataFrame

Given the following DataFrame:

| a | b | c | d |
|---|---|---|---|
| 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 |

How does one efficiently construct a weighted graph, such that:

  • The nodes correspond to the column names;
  • Two vertices are connected if they both have 1's in the same line of the DataFrame (e.g. 'a' is connected to 'c' in the first row);
  • The weight is equal to the number of times two vertices are connected (e.g. edge 'a'-'c' has weight 2, while 'c'-'d' has weight 1).

Here is how to manually construct this graph using SimpleWeightedGraphs.jl and GraphPlot.jl:

g = SimpleWeightedGraph(4)
add_edge!(g,1,3,2)
add_edge!(g,1,4,2)
add_edge!(g,2,4,1)
add_edge!(g,3,4,1)
nodes = ["a","b","c","d"]
gplot(g,nodelabel=nodes,edgelinewidth=[2,2,1,1])

enter image description here

like image 365
Davi Barreira Avatar asked May 17 '26 17:05

Davi Barreira


1 Answers

Something like this should work assuming df is your data frame:

using LinearAlgebra

function gengraph(df)
    g = SimpleWeightedGraph(ncol(df))
    ew = Int[]
    for i in 1:ncol(df), j in i+1:ncol(df)
        w = dot(df[!, i], df[!, j])
        if w > 0
            push!(ew, w)
            add_edge!(g, i, j, w)
        end
    end
    gplot(g,nodelabel=names(df),edgelinewidth=ew)
end
like image 141
Bogumił Kamiński Avatar answered May 20 '26 13:05

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!