-
[네트워크 분석] Pyvis 패키지를 이용한 유저 간 거래 네트워크 분석 (1)코딩 및 데이터분석/데이터 분석(pandas, numpy 등) 2023. 12. 20. 22:19
유저가 부정한 방법으로 재화를 취득한 경우 그 재화를 조사해 회수해야 하는데,
재화를 템창에 고이 모셔놓는 게 아니라 이를 거래, 강화, 분해 등 행위를 통해 계속 변형시키기 때문에 그걸 추적하는 게 쉽지 않다.
지금 내가 관심 있는 주제는 BOT 캐릭터들이 취득한 재화가 과연 어디로 흐르는가이다.
이 주제를 위해 여러가지 찾다가 Pyvis라는 네트워크 분석 패키지를 발견. 매우 유용해보였다.
https://pyvis.readthedocs.io/en/latest/introduction.html
Introduction — pyvis 0.1.3.1 documentation
Introduction The goal of this project is to build a python based approach to constructing and visualizing network graphs in the same space. A pyvis network can be customized on a per node or per edge basis. Nodes can be given colors, sizes, labels, and oth
pyvis.readthedocs.io
이에 샘플로 간단한 데이터를 만든 후 연습해보았다.
import pandas as pd import networkx as nx from pyvis.network import Network
In [10]:data = { 'seller': ['a', 'b', 'c', 'd', 'e', 'f'], 'buyer': ['c', 'z', 'b', 'a', 'f', 'g'], 'item': ['ring', 'boots', 'shirt', 'weapon', 'sword', 'diamond'], 'amount': [100, 150, 300, 120, 280, 50] } df = pd.DataFrame(data)
In [11]:df
Out[11]:sellerbuyeritemamount012345a c ring 100 b z boots 150 c b shirt 300 d a weapon 120 e f sword 280 f g diamond 50 In [39]:G = Network(notebook=True, height='500px', width='800px', directed=True)
Warning: When cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.
In [40]:for _, row in df.iterrows(): G.add_node(row['seller'], label=row['seller']) G.add_node(row['buyer'], label=row['buyer'])
In [41]:for _, row in df.iterrows(): G.add_edge(row['seller'], row['buyer'], width=row['amount']/100, color= , label=row['amount'])
In [42]:G.show('trade_network.html')
결과는 아래 화면.
인터랙티브 UI라 html로 출력된 결과창에서 node를 만지작거릴 수 있다. 그럼 그에 따라 그래프가 움직인다.
https://www.youtube.com/watch?v=jNfkbkHzZ5s
나중에는 이렇게 버튼도 넣고, 간단한 웹페이지로 만들어 배포도 가능하다고 한다.
2024년 한 해동안 차근차근 연습해서 그럴싸한 결과물을 하나 만들어보고 싶다.
'코딩 및 데이터분석 > 데이터 분석(pandas, numpy 등)' 카테고리의 다른 글
[유저 이탈 지표 구축 프로젝트]K-means Clustering 예제 (1) 2024.01.26 [유저 이탈 지표 구축 프로젝트][스크랩] (0) 2024.01.24 [스크랩] 250+ Python and Data Science Tips — Covering Pandas, NumPy, ML Basics, Skl (0) 2023.09.20 파이썬으로 데이터 주무르기 - 챕터 1 실습 (0) 2023.07.23