mirror of https://github.com/Yomguithereal/fog.git
Working on projections
This commit is contained in:
parent
556cd162c4
commit
83a14df93f
|
@ -2,6 +2,8 @@ language: python
|
|||
python:
|
||||
- "3.5"
|
||||
- "3.6"
|
||||
- "3.7"
|
||||
- "3.8"
|
||||
install:
|
||||
- pip install --upgrade pip
|
||||
- pip install --upgrade setuptools
|
||||
|
|
18712
data/bipartite.csv
18712
data/bipartite.csv
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,18 @@
|
|||
import csv
|
||||
from networkx import Graph
|
||||
from fog.graph import cosine_monopartite_projection
|
||||
|
||||
bipartite = Graph()
|
||||
|
||||
with open('./data/bipartite.csv') as f:
|
||||
reader = csv.DictReader(f)
|
||||
|
||||
for line in reader:
|
||||
account = 'a%s' %line['account']
|
||||
url = 'u%s' % line['url']
|
||||
|
||||
bipartite.add_node(account, type='account')
|
||||
bipartite.add_node(url, type='url')
|
||||
bipartite.add_edge(account, url, weight=int(line['weight']))
|
||||
|
||||
monopartite = cosine_monopartite_projection(bipartite, 'account', part='type')
|
|
@ -0,0 +1,3 @@
|
|||
from fog.graph.projection import (
|
||||
cosine_monopartite_projection
|
||||
)
|
|
@ -4,7 +4,30 @@
|
|||
#
|
||||
# Miscellaneous functions related to bipartite to monopartite projections.
|
||||
#
|
||||
from collections import defaultdict, Counter
|
||||
from networkx import Graph
|
||||
|
||||
|
||||
def cosine_monopartite_projection():
|
||||
pass
|
||||
def cosine_monopartite_projection(bipartite, keep, part='bipartite', weight='weight',
|
||||
threshold=None):
|
||||
monopartite = Graph()
|
||||
|
||||
vectors = defaultdict(Counter)
|
||||
|
||||
for n1, n2, w in bipartite.edges(data=weight, default=1):
|
||||
p1 = bipartite.nodes[n1][part]
|
||||
p2 = bipartite.nodes[n2][part]
|
||||
|
||||
assert p1 != p2, 'fog.graph.cosine_monopartite_projection: given graph is not truly bipartite.'
|
||||
|
||||
# Swapping so n1 is from part to keep
|
||||
if p2 == part:
|
||||
n1, n2 = n2, n1
|
||||
|
||||
vectors[n1][n2] += w
|
||||
|
||||
norms = {}
|
||||
|
||||
for node, vector in vectors.items():
|
||||
monopartite.add_node(node, bipartite.nodes[node])
|
||||
norms[node] = sum(vector.values())
|
||||
|
|
|
@ -9,6 +9,6 @@ twine==1.11.0
|
|||
|
||||
# Dependencies
|
||||
dill==0.2.7.1
|
||||
networkx==2.5
|
||||
networkx>=2
|
||||
phylactery==0.1.1
|
||||
Unidecode==1.0.22
|
||||
|
|
Loading…
Reference in New Issue