김한준

가중연결중심성

This diff is collapsed. Click to expand it.
1 +#-*-encoding utf-8 -*-
2 +import matplotlib.pyplot as plt
3 +import networkx as nx
4 +from openpyxl import load_workbook
5 +
6 +
7 +movie_director_list = []
8 +movie_audience_list = []
9 +movie_edge_list = []
10 +movie_graph_list = []
11 +movie_check_list = []
12 +max_audience=-1
13 +load_wb = load_workbook("movie_top500.xlsx", data_only=True)
14 +load_ws = load_wb['Sheet1']
15 +
16 +
17 +def make_edge(movielist):
18 + edge_list = []
19 + for i in range(0,len(movielist)):
20 + for j in range(0, len(movielist)):
21 + if i == j:
22 + break
23 + else:
24 + edge_list.append((movielist[i],movielist[j]))
25 + return edge_list
26 +
27 +
28 +
29 +for i in range(7,507,1):
30 + distributor_name='L'+str(i)
31 + director_name='K'+str(i)
32 + name3=(str(load_ws[distributor_name].value) +','+ str(load_ws[director_name].value)).split(',')
33 + movie_audience_list.append(int(load_ws['F'+str(i)].value))
34 + movie_director_list.append(name3)
35 +
36 +
37 +
38 +
39 +for i in range(0, 500):
40 + if movie_director_list[i] in movie_check_list:
41 + movie_audience_list[i]=movie_audience_list[i]+movie_audience_list[movie_check_list.index(movie_director_list[i])]
42 + movie_audience_list[movie_check_list.index(movie_director_list[i])]=movie_audience_list[i]
43 + movie_check_list.append(movie_director_list[i])
44 + else:
45 + movie_check_list.append(movie_director_list[i])
46 + if movie_audience_list[i] > max_audience:
47 + max_audience=movie_audience_list[i]
48 +
49 +
50 +for i in range(0, 500):
51 + movie_graph = nx.Graph()
52 + movie_edge = make_edge(movie_director_list[i])
53 + movie_edge_list.append(movie_edge)
54 + movie_graph.add_nodes_from(movie_director_list[i])
55 + movie_graph.add_edges_from(movie_edge_list[i], weight = movie_audience_list[i]/(max_audience+1))
56 + movie_graph_list.append(movie_graph)
57 +
58 +
59 +movie_total = nx.Graph()
60 +
61 +for i in range(0,500):
62 + movie_total = nx.compose(movie_total, movie_graph_list[i])
63 +
64 +degree = movie_total.degree()
65 +nx.write_gexf(movie_total, "movie_weight.gexf")
66 +
67 +
68 +
69 +
70 +plt.figure(figsize=(20,20))
71 +pos = nx.spring_layout(movie_total, iterations = 10)
72 +nx.draw(movie_total, pos, alpha = 1, line_color= 'black', linewidths = 0, width = 0.05 ,node_size = 100, node_color = 'w', with_labels = True, font_family='NanumGothic', font_color='black', font_size=8)
73 +plt.show()
74 +
75 +