In [46]:
import networkx as nx
import matplotlib.pyplot as plt
import nxviz as nz
In [47]:
# create graph ...
G = nx.Graph()
In [48]:
# add in users ...
G.add_nodes_from(['u-' + str(i) for i in range(1,3)], bipartite='users')
In [49]:
# add in products ...
G.add_nodes_from(['p-' + str(i) for i in range(1,2)], bipartite='products')
In [50]:
list(G.nodes(data=True))[0:3]
Out[50]:
In [51]:
# draw edge between users and products ...
G.add_edge(u='u-1', v='p-1')
G.add_edge(u='u-2', v='p-1')
G.add_edge(u='u-1', v='p-2')
G.add_edge(u='u-3', v='p-2')
In [52]:
# pull neighbors from users ...
user1 = 'u-1'
user2 = 'u-2'
user1_neighbors = set(G.neighbors(user1))
user2_neighbors = set(G.neighbors(user2))
In [53]:
# products both user1 and user2 bought ...
user1_neighbors.intersection(user2_neighbors)
Out[53]:
In [54]:
# products that user1 bought, but user2 hasn't yet ...
user1_neighbors.difference(user2_neighbors)
Out[54]: