# Bipartite weighted network

**URL:** https://discourse.julialang.org/t/bipartite-weighted-network/35985
**Category:** Graphs
**Tags:** lightgraphs, metagraphs
**Created:** [March 14, 2020, 7:50pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985 "2020-03-14T19:50:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mp-crypto](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mp-crypto](https://discourse.julialang.org/u/mp-crypto)
#### Post date: [March 14, 2020, 7:50pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/1 "2020-03-14T19:50:33Z")

</div>

Hello,

I am new to Julia (moving from Python). I am trying to generate a bipartite weighted network from a data frame. The data frame contains the “edgelist” (i.e., a column for bottom nodes, and a column for top nodes), and a column for the weights of the edge. I found the GraphDataFrameBridge package that creates a network from two columns of the data frame, but it creates a unipartite network, and it is not clear how to import the weights. Any suggestions?

Thank you very much!

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [March 14, 2020, 8:55pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/2 "2020-03-14T20:55:27Z")

</div>

If you have an edgelist representing a bipartite network (and you’ve verified that the graph represented by those edges is bipartite), I don’t know why GraphDataFrameBridge would be creating something that’s unipartite. Do you have a MWE you can share?

---

<div class="post-metadata">

### Author: ![mp-crypto](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mp-crypto](https://discourse.julialang.org/u/mp-crypto)
#### Post date: [March 15, 2020, 3:10pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/3 "2020-03-15T15:10:51Z")

</div>

Thank you very much,  
I was able to decompose the problem. Now I can import the edgelist as a bipartite network. The problem was that (if I understand correctly) GraphDataFrameBridge accepts only nodes’ labels of the same type. I initially had top nodes’ labels as “Floats” and bottom nodes as “Strings” and when I converted to the same type, some top and bottom nodes turned out to have the same label.

However, I still have 2 issues:

a) (even if is\_bipartite(mg) now gives true), when I try to export the adjacency matrix from the example below, I still obtain a 7X7 unweighted matrix instead of a 4X3 weighted matrix.

```julia
using Distances
test=DataFrame(A = ["i", "j", "k", "z"], B = ["1", "2", "2", "3"], weights = [2,3,4,1])
mg = MetaGraph(test, :A, :B, weight=:weights)
is_bipartite(mg)
w=Array(adjacency_matrix(mg))
#R = pairwise(Euclidean(), w, dims=2)

```

b) I cannot figure out how to create a list of correspondence between the indexes assigned by MetaGraph and the labels of the original nodes. Something like:

1 “i”  
2 “j”  
…  
6 “2”

Thank you very much for your help.

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [March 16, 2020, 1:25pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/4 "2020-03-16T13:25:59Z")

</div>

- Don’t use `adjacency_matrix`. Use `weights`. You’re getting a 7x7 because you have 7 distinct vertex labels (i, j, k, z, 1, 2, 3). Adjacency matrices are square.

- this will require further exploration.

---

<div class="post-metadata">

### Author: ![mp-crypto](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mp-crypto](https://discourse.julialang.org/u/mp-crypto)
#### Post date: [March 16, 2020, 2:28pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/5 "2020-03-16T14:28:09Z")

</div>

> [@anon94023334](#):
>
> weights

Thank you. I tried weights(mg) but I got the following error in the example above:

MethodError: objects of type Array{Float64,1} are not callable  
Use square brackets for indexing an Array.

Any suggestion on how I can output the nXm weighted matrix?

Regarding the second issue, I found the graft.jl that seems to produce the vertex table I was looking for (although it does not seem to be supported anymore). Basically I was wondering if it is possible to output the vertexid and the label of the id. My labels are i,j,k,z,1,2,3 but they are indexed and I do not know what node gets what index. For example, when I output the adjacency matrix I do not know whether the first row is for i,j,…etc. I need this information to merge the output of the network analysis with other variables. Hope I was able to explain myself.

Thank you again for any help.

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [March 16, 2020, 2:49pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/6 "2020-03-16T14:49:56Z")

</div>

```julia
julia> weights(mg) .* adjacency_matrix(mg)
7×7 Array{Float64,2}:
 0.0 0.0 0.0 2.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 3.0 4.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 1.0
 2.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 3.0 0.0 0.0 0.0 0.0 0.0
 0.0 4.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 1.0 0.0 0.0 0.0 0.0

julia> [props(mg, x)[:name] for x in vertices(mg)]
7-element Array{String,1}:
 "1"
 "2"
 "3"
 "i"
 "j"
 "k"
 "z"

```

---

<div class="post-metadata">

### Author: ![mp-crypto](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mp-crypto](https://discourse.julialang.org/u/mp-crypto)
#### Post date: [March 16, 2020, 2:59pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/7 "2020-03-16T14:59:18Z")

</div>

Thanks! For some reason I still get the same error when I run  
weights(mg) .\* adjacency\_matrix(mg):

MethodError: objects of type Array{Float64,1} are not callable  
Use square brackets for indexing an Array.

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [March 16, 2020, 3:17pm UTC](https://discourse.julialang.org/t/bipartite-weighted-network/35985/8 "2020-03-16T15:17:31Z")

</div>

That’s probably because you’ve locally defined a vector called `weights` that is overriding the function `LightGraphs.weights`.
