250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- TensorFlow
- GCP
- gather_nd
- UDF
- chatGPT
- flask
- integrated gradient
- Airflow
- login crawling
- requests
- 상관관계
- subdag
- 공분산
- API Gateway
- session 유지
- Counterfactual Explanations
- GenericGBQException
- hadoop
- correlation
- Retry
- tensorflow text
- BigQuery
- youtube data
- airflow subdag
- top_k
- spark udf
- XAI
- 유튜브 API
- grad-cam
- API
Archives
- Today
- Total
데이터과학 삼학년
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]:
In [19]:
G.edges.data() # default data is {} (empty dict)
Out[19]:
In [20]:
G.edges.data('weight', default=1)
Out[20]:
In [21]:
G.edges(keys=True) # default keys are integers
Out[21]:
In [22]:
G.edges.data(keys=True)
Out[22]:
In [23]:
G.edges.data('weight', default=1, keys=True)
Out[23]:
In [24]:
G.edges([0, 3])
Out[24]:
In [25]:
G.edges(0)
Out[25]:
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]:
In [28]:
nx.average_clustering(G_symmetric)
Out[28]:
In [ ]:
Distance
In [29]:
nx.shortest_path(G_symmetric, 'Dev Anand', 'Akshay Kumar')
Out[29]:
In [30]:
T = nx.bfs_tree(G_symmetric, 'Dev Anand')
In [31]:
T
Out[31]:
In [34]:
G_fb = nx.read_edgelist("facebook_combined.txt", create_using = nx.Graph(), nodetype=int)
In [35]:
print(nx.info(G_fb))
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]:
In [40]:
sorted(betCent, key=betCent.get, reverse=True)[:5]
Out[40]:
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