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:
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])

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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With