原文:
towardsdatascience.com/the-history-of-bodybuilding-through-network-visualization-2527f81c1f01
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/bd146591ffce8b7b7ca859a76f5d9738.png
所有图像和代码均由作者创建,除非另有说明来源。
我对举重已经热情了大约十年,所以是时候用数据驱动的方法来描绘这项运动最伟大的传奇人物,特别是由阿诺德·施瓦辛格象征化的竞技健美了。因此,在这里,我首先收集了每个欧普利亚先生比赛前三名的数据,并创建了获胜者的共享领奖台网络。这个网络后来在 Gephi 中可视化,应该会突出显示这个运动的不同时代及其关键人物。
此外,本文旨在说明如何使用数据科学和网络可视化来描绘任何社会生态系统中隐藏的联系,无论其规模大小,来自体育界还是艺术界,具有微小的或巨大的商业价值。
1. 数据准备
1.1. 收集数据
我通常从数据收集开始,比如编写爬虫或调用 API。然而,对于这次练习,一个极其简单的解决方案效果最好——选择正确的Wikipedia 网站(也感谢 Wiki 的开放使用许可),向下滚动查看前三名位置表,如下所示,并将其复制粘贴到名为olimpia.xlsx的 Excel 电子表格中。
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/ccaaad01abd357ed3c3f310ec3fe114f.png
欧普利亚先生前三名的最初几年。来源:en.wikipedia.org/wiki/Mr._Olympia
1.2. 解析和清理数据
现在让我们转到 Python 终端,解析、清理并显示电子表格作为 Pandas DataFrame:
importpandasaspd# parsing the data framedf=pd.read_excel('olimpia.xlsx')# getting ride of some data cleaning issues resulted from the manual copy-pastingdf=df.apply(lambdax:x.str.strip()ifx.dtype=="object"elsex)# getting some stats:print('The start of the data set:',min(df.year))print('The end of the data set:',max(df.year))# taking a quick look:df.head(3)输出:
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/107e42db2fd99a2aa4d328729e5ab272.png
现在让我们创建一个所有曾进入前三名的所有人的列表:
athletes=pd.unique(df.drop(columns=['year']).values.ravel())len(set(athletes))athletes[0:15]输出:
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/984cd1dc9f95843b4c169f4e91207f1d.png
这里可能会注意到,关于数据清理还有一些工作要做,因为在某些年份,同一位置由两个人并列获得——例如 1976 年的第三名,迈克·卡特和弗兰克·赞恩。
因此,我现在创建了一个新的、清理过的运动员列表,通过将并列情况拆分到列表中,去除 nan 值,并将所有人打包到名为 athletes_clean 的新列表中:
athletes_clean=[]forathleteinathletes:iftype(athlete)isstr:athletes_clean+=[a.strip().rstrip()forainathlete.replace('xa0',' ').split('&')]athletes_clean=list(set(athletes_clean))print(len(athletes_clean))这个过程将导致一个包含 56 名运动员的列表。
1.3. 概述统计
因此,我现在创建了一个新的、清理过的运动员列表,通过将并列情况拆分到列表中,去除 nan 值,并将所有人打包到名为 athletes_clean 的新列表中:
athlete_cnt={athlete:df.applymap(lambdax:athleteinstr(x)).sum().sum()forathleteinathletes_clean}pd.DataFrame(athlete_cnt.items(),columns=['Name','Number_of_Times']).sort_values(by='Number_of_Times',ascending=False).head(10)此代码块的输出显示了所有时间排名前 10 的知名运动员:
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/34f742663ee3f46ae7534098355ccb32.png
史上最频繁出现在奥林匹亚先生比赛前三名的 10 位运动员。
2. 网络
2.1. 构建网络
现在,我将逐行遍历 DataFrame,将给定年份的所有名字存储在一个列表中,并在每对年份之间添加一个强度为 1 的边。这意味着节点将被定义为运动员,并且他们将与共享领奖台相连。一个共享领奖台计为一个宽度,无论他们达到的位置如何,而且他们共同出演的频率越高,链接就越强。
importnetworkxasnx edges={}foriinrange(len(df)):# get the row valuesrow=df.iloc[i].dropna().to_list()# keep the names (strings)top3=[tfortinrowiftype(t)isstr]# transform the tiestie=[tfortintop3if'xa0'int]iflen(tie)>0:tie=[a.strip().rstrip()foraintie[0].replace('xa0',' ').split('&')]top3=[tfortintop3if'xa0'notint]top3+=tie# now link the athletes taking the top 3 positionsforidx,t1inenumerate(top3):fort2intop3[idx+1:]:edge='t'.join(sorted([t1,t2]))ifedgenotinedges:edges[edge]=1else:edges[edge]+=1# initiatie an empty graphG=nx.Graph()# pack the edges into the Graphforedge,weightinedges.items():e1,e2=edge.split('t')G.add_edge(e1,e2,weight=weight)# Check the basic stats of the graph - number of nodes and linksG.number_of_nodes(),G.number_of_edges()此代码段的输出显示,我们构建的网络包含 56 个节点(作为运动员),通过总共 120 条链接连接。
2.2. 可视化网络
虽然我使用了 Gephi 进行可视化,以下是我用来导出和准备最终可视化文件的几行代码,您将在下面的图中找到。
# export the graphnx.write_gexf(G,'olympia.gexf')# export the count tabledf_out=pd.DataFrame(athlete_cnt.items(),columns=['Id','Cnt']).set_index('Id')df_out.to_csv('cnt.csv')这最后的视觉图像生动地展示了健美领域的演变。首先,人们可能会注意到,辉煌的日子,老派时代在完全分离的组件中,以施瓦辛格和弗兰克·赞尼为中心。我们还可以进一步确定黄金时代的其他标志性人物,特别是在银色社区中,如弗朗哥·科伦布和塞尔吉奥·奥利瓦。
然后,在第二个网络社区中,我们可以清楚地看到时间的推移,从 80 年代开始,由李·汉尼统治,然后过渡到现代时代的巨人,包括多里安·耶茨、凯文·莱弗龙,然后是过去三位长期冠军:罗尼·科尔曼、杰伊·卡特勒和菲尔·希特。
此图以及文章整体,也作为一个例子,展示了我们如何利用数据科学和网络可视化来描绘各种细分的社会结构,无论是来自体育界还是幻想世界,而方法和工具同样适用于银行或人力资源领域。
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/748f92101234f11cbd6b0480ad8ac08f.png