행위

김어진 과제.lst

KHS DH wiki

김어진 (토론 | 기여)님의 2024년 3월 20일 (수) 01:39 판

import networkx as nx import matplotlib.pyplot as plt

  1. 데이터 로드

data = """

  1. Nodes

001 사람 김어진 002 사람 신현빈 003 사람 임서연 004 사람 변혜빈 005 사람 강혜리 006 사람 박도연 007 사람 김수현 008 사람 선은혁 009 사람 이종욱 010 음식 샐러드 011 음식 샤브샤브 012 음식 구움과자 013 음식 집밥 014 미디어 DUNE:PART2 015 미디어 닭강정 016 미디어 눈물의 여왕 017 미디어 웨딩파서블 018 취미 고양이랑놀기 019 취미 맛집찾기 020 취미 음식사진찍기 021 취미 멍때리기 022 취미 영화보기 023 취미 노래듣기

  1. Links

001 024 belong to 002 024 belong to 003 024 belong to 004 024 belong to 005 024 belong to 006 024 belong to 007 024 belong to 008 024 belong to 009 024 belong to 001 010 likes 001 011 likes 001 012 likes 001 013 likes 001 014 watched 002 014 watched 003 014 watched 005 014 watched 006 014 watched 007 014 watched 008 014 watched 001 018 used to 001 019 used to 001 020 used to 001 021 used to 001 022 used to 001 023 used to """

  1. 데이터 처리

nodes = {} for line in data.split('\n'):

   if line.strip() == :
       continue
   parts = line.split('\t')
   node_id = parts[0]
   node_class = parts[1].strip()
   node_name = parts[2].strip()
   nodes[node_id] = {'class': node_class, 'name': node_name}

G = nx.Graph()

  1. 노드 추가

for node_id, node_data in nodes.items():

   G.add_node(node_id, class=node_data['class'], name=node_data['name'])
  1. 링크 추가

links = [

   (link.split('\t')[0], link.split('\t')[1])
   for link in data.split('\n') if 'Links' not in link and link.strip() != 

]

for link in links:

   G.add_edge(link[0], link[1])
  1. 그래프 시각화

plt.figure(figsize=(10, 8))

pos = nx.spring_layout(G) # 그래프의 위치 결정

node_colors = {'사람': 'blue', '음식': 'green', '미디어': 'red', '취미': 'yellow'} node_color = [node_colors[data['class']] for node, data in G.nodes(data=True)] nx.draw(G, pos, with_labels=True, node_color=node_color, node_size=500, font_size=10)

plt.title("네트워크 그래프") plt.show()

Visualisation