데이터과학 삼학년

Graph 기초 본문

Machine Learning

Graph 기초

Dan-k 2020. 6. 2. 13:59
반응형
graph 기초
In [1]:
import networkx as nx
In [ ]:
Graph  vertex(node) edge로 구성되어 있다.
동그란 꼭지점을 vertex라고 하고, vertex를 잇는 선을 edge라고 한다.

Symmetric Networks

In [2]:
G_symmetric = nx.Graph()
G_symmetric.add_edge('Amitabh Bachchan','Abhishek Bachchan')
G_symmetric.add_edge('Amitabh Bachchan','Aamir Khan')
G_symmetric.add_edge('Amitabh Bachchan','Akshay Kumar')
G_symmetric.add_edge('Amitabh Bachchan','Dev Anand')
G_symmetric.add_edge('Abhishek Bachchan','Aamir Khan')
G_symmetric.add_edge('Abhishek Bachchan','Akshay Kumar')
G_symmetric.add_edge('Abhishek Bachchan','Dev Anand')
G_symmetric.add_edge('Dev Anand','Aamir Khan')
In [7]:
nx.draw_networkx(G_symmetric)

Asymmetric Networks

In [4]:
G_asymmetric = nx.DiGraph()
G_asymmetric.add_edge('A','B')
G_asymmetric.add_edge('A','D')
G_asymmetric.add_edge('C','A')
G_asymmetric.add_edge('D','E')
In [8]:
nx.spring_layout(G_asymmetric)
nx.draw_networkx(G_asymmetric)

Weighted Networks

In [10]:
G_weighted = nx.Graph()
G_weighted.add_edge('Amitabh Bachchan','Abhishek Bachchan', weight=25)
G_weighted.add_edge('Amitabh Bachchan','Aaamir Khan', weight=8)
G_weighted.add_edge('Amitabh Bachchan','Akshay Kumar', weight=11)
G_weighted.add_edge('Amitabh Bachchan','Dev Anand', weight=1)
G_weighted.add_edge('Abhishek Bachchan','Aaamir Khan', weight=4)
G_weighted.add_edge('Abhishek Bachchan','Akshay Kumar',weight=7)
G_weighted.add_edge('Abhishek Bachchan','Dev Anand', weight=1)
G_weighted.add_edge('Dev Anand','Aaamir Khan',weight=1)
In [11]:
nx.draw_networkx(G_weighted)

Multigraph

In [18]:
G = nx.MultiGraph()   # or MultiDiGraph
nx.add_path(G, [0, 1, 2])
key = G.add_edge(2, 3, weight=5)
[e for e in G.edges()]
Out[18]:
[(0, 1), (1, 2), (2, 3)]
In [19]:
G.edges.data() # default data is {} (empty dict)
Out[19]:
MultiEdgeDataView([(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})])
In [20]:
G.edges.data('weight', default=1)
Out[20]:
MultiEdgeDataView([(0, 1, 1), (1, 2, 1), (2, 3, 5)])
In [21]:
G.edges(keys=True) # default keys are integers
Out[21]:
MultiEdgeView([(0, 1, 0), (1, 2, 0), (2, 3, 0)])
In [22]:
G.edges.data(keys=True)
Out[22]:
MultiEdgeDataView([(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})])
In [23]:
G.edges.data('weight', default=1, keys=True)
Out[23]:
MultiEdgeDataView([(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)])
In [24]:
G.edges([0, 3])
Out[24]:
MultiEdgeDataView([(0, 1), (3, 2)])
In [25]:
G.edges(0)
Out[25]:
MultiEdgeDataView([(0, 1)])
In [ ]:
Degree
In [27]:
nx.degree(G_symmetric, 'Dev Anand') #This will return a value of 3, as Dev Anand has worked with only three actors in the network.
Out[27]:
3
In [28]:
nx.average_clustering(G_symmetric)
Out[28]:
0.8666666666666666
In [ ]:
Distance
In [29]:
nx.shortest_path(G_symmetric, 'Dev Anand', 'Akshay Kumar')
Out[29]:
['Dev Anand', 'Abhishek Bachchan', 'Akshay Kumar']
In [30]:
T = nx.bfs_tree(G_symmetric, 'Dev Anand')
In [31]:
T
Out[31]:
<networkx.classes.digraph.DiGraph at 0x7fbbc6ee2160>
In [34]:
G_fb = nx.read_edgelist("facebook_combined.txt", create_using = nx.Graph(), nodetype=int)
In [35]:
print(nx.info(G_fb))
Name: 
Type: Graph
Number of nodes: 4039
Number of edges: 88234
Average degree:  43.6910
In [37]:
nx.draw_networkx(G_fb)
In [38]:
import matplotlib.pyplot as plt
In [39]:
pos = nx.spring_layout(G_fb)
betCent = nx.betweenness_centrality(G_fb, normalized=True, endpoints=True)
node_color = [20000.0 * G_fb.degree(v) for v in G_fb]
node_size =  [v * 10000 for v in betCent.values()]
plt.figure(figsize=(20,20))
nx.draw_networkx(G_fb, pos=pos, with_labels=False,
                 node_color=node_color,
                 node_size=node_size )
plt.axis('off')
Out[39]:
(-0.9308566050006861,
 0.8176587007397182,
 -1.0643712768569509,
 1.099375713826708)
In [40]:
sorted(betCent, key=betCent.get, reverse=True)[:5]
Out[40]:
[107, 1684, 3437, 1912, 1085]
728x90
반응형
LIST

'Machine Learning' 카테고리의 다른 글

Dealing with Data Scarcity  (0) 2020.06.02
Image Classification (Linear, DNN, CNN)  (0) 2020.06.02
Isolation Forest (for anomaly detection)  (0) 2020.04.13
Image classification (Linear, DNN, CNN)  (0) 2020.03.04
ROC, AUC  (0) 2020.02.17
Comments