This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge0.py
212 lines (198 loc) · 7.42 KB
/
challenge0.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import json
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.stats.stats
import seaborn as sns
# JSON Structure:
# prizes - list of dicts
# year
# category
# laureates - list of dicts
# id - string number
# firstname
# surname
# motivation - sentence
# share - inverted
# laureates - list of dicts
# id - string number - primary-foreign to above
# firstname
# surname
# born - "YYYY-MM-DD"
# died - "YYYY-MM-DD"
# bornCountry
# bornCountryCode
# bornCity
# diedCountry
# diedCountryCode
# diedCity
# gender - male/female
# prizes - list of dicts
# year
# category
# share
# motivation
# affiliations - list of dicts
# name - affiliation name
# city
# country
# countries - list of dicts
# name - primary-foreign to above
# code
# Questions
# What is the overall prize count for each gender - pie/bar
# Are certain categories different - bar
# How many avg awards for each gender - box/swarm
# How has % female changed over time - tsplot
# What is the avg share for each gender - box?
# Effected if:
# 1) female % changed over time
# 2) overall share % changed over time
# Does one gender collaborate more than another - box?
# Effected if:
# 1) female % changed over time
# 2) collaboration % changed over time
# % women by region - pie/bar
# Born or died as region?
# Effected if:
# 1) female % changed over time
# 2) region % changed over time
# Are there any hot-spot universities for women - bar/map
# Have to check affiliations, find good way to plot
sns.set(style="darkgrid")
json_data = json.load(open("Data/Nobel/nobel.json"))
pyear = []
pcat = []
pid = []
pfirst = []
plast = []
# pmotive = []
pshare = []
pnum = []
for prize in json_data["prizes"]:
for laureate in prize["laureates"]:
pyear.append(prize["year"])
pcat.append(prize["category"])
pid.append(laureate["id"])
pfirst.append(laureate["firstname"])
plast.append(laureate["surname"])
# pmotive.append(laureate["motivation"])
pshare.append(1 / int(laureate["share"]))
pnum.append(len(prize["laureates"]))
prizes = pd.DataFrame({"Year": pyear, "Category": pcat, "ID": pid, "First Name": pfirst, "Last Name": plast,
"Share": pshare, "Team Size": pnum})
cname = []
ccode = []
for country in json_data["countries"]:
cname.append(country["name"])
ccode.append(country["code"])
countries = pd.DataFrame({"Country": cname, "Code": ccode})
# Ignoring affiliations, one row per laureate
lid = []
lid2 = []
# lfirst = []
# llast = []
lborn = []
ldied = []
# lborncc = []
# lborncity = []
# ldiedcc = []
# ldiedcity = []
lgender = []
lgender2 = []
lnumprizes = []
lyear = []
lcat = []
lshare = []
ltshare = []
lavgshare = []
lavgteam = []
# lmotive = []
for laureate in json_data["laureates"]:
if ("year" not in laureate["prizes"][0]):
continue
share_count = 0
tshare_count = 0
team_count = 0
lid.append(laureate["id"])
# lfirst.append(laureate["firstname"])
# llast.append(laureate["surname"])
lgender.append(laureate["gender"].title())
lborn.append(laureate["born"])
ldied.append(laureate["died"])
# lborncc.append(laureate["bornCountryCode"])
# lborncity.append(laureate["bornCity"])
# ldiedcc.append(laureate["diedCountryCode"])
# ldiedcity.append(laureate["diedCity"])
num_prizes = len(laureate["prizes"])
lnumprizes.append(num_prizes)
for prize in laureate["prizes"]:
lid2.append(laureate["id"])
lgender2.append(laureate["gender"].title())
lyear.append(prize["year"])
lcat.append(prize["category"].title())
lshare.append(1 / int(prize["share"]))
# lmotive.append(prize["motivation"])
share_count += int(prize["share"])
tshare_count += 1 / int(prize["share"])
team_count += len(prizes[(prizes.Year == prize["year"]) & (prizes.Category == prize["category"])])
ltshare.append(tshare_count)
lavgshare.append(1 / (share_count / num_prizes))
lavgteam.append(team_count / num_prizes)
laureates = pd.DataFrame({"ID": lid, "Gender": lgender, "Birth Date": lborn, "Death Date": ldied, "Prizes": lnumprizes,
"Total Share": ltshare, "Average Share": lavgshare, "Average Size": lavgteam})
laureate_prizes = pd.DataFrame({"ID": lid2, "Gender": lgender2, "Year": lyear, "Category": lcat, "Share": lshare})
laureate_prizes['Team'] = np.where(laureate_prizes['Share'] == 1, 'No', 'Yes')
# What is the overall prize count for each gender
# Are certain categories different
prize_gender_bar = sns.countplot(x='Gender', data=laureate_prizes)
prize_gender_bar.set(title='Nobel Prizes Won by Gender', ylabel='Count')
for p in prize_gender_bar.patches:
prize_gender_bar.annotate('{:1.2f}'.format(p.get_height()/len(laureate_prizes)), (p.get_x()+.3, p.get_height()+5))
plt.tight_layout()
plt.show()
prize_category_bar = sns.countplot(x='Category', hue='Gender', data=laureate_prizes)
for p in prize_category_bar.patches:
prize_category_bar.annotate('{0:g}'.format(p.get_height()), (p.get_x(), p.get_height()+2))
plt.legend(loc='upper right')
prize_category_bar.set(title='Nobel Prizes Won by Gender and Category', ylabel='Count')
plt.tight_layout()
plt.show()
# How has % female changed over time
group = laureate_prizes.groupby(['Year', 'Gender']).sum() / laureate_prizes.groupby('Year').sum()
pivot = pd.pivot_table(index='Year', columns='Gender', values='Share', data=group).fillna(0)
ax = pivot.plot()
ax.set_title("Share of Nobels Over Time")
plt.tight_layout()
plt.show()
pivot = pivot.reset_index()
pivot.Year = pd.to_numeric(pivot.Year)
print(scipy.stats.stats.pearsonr(pivot.Year, pivot.Female))
x = np.arange(5.13157894737, 586, 5.13157894737)
group2 = laureate_prizes.groupby(['Year', 'Gender']).sum()
pivot2 = pd.pivot_table(index='Year', columns='Gender', values='Share', data=group2).fillna(0).cumsum()
pivot2['Total'] = x
ax = pivot2.plot()
ax.set_title("Total Nobels Over Time")
plt.tight_layout()
plt.show()
# What is the avg share for each gender
share_gender = sns.boxplot(x='Gender', y='Average Share', data=laureates)
share_gender.set_title("Career Average Nobel Share")
plt.tight_layout()
plt.show()
# Does one gender collaborate more than another
women_on = len(laureate_prizes[(laureate_prizes.Team == 'Yes') & (laureate_prizes.Gender == 'Female')]) / int(
laureate_prizes.Gender.value_counts()['Female'])
women_on = '{:.2f}%'.format(women_on * 100)
men_on = len(laureate_prizes[(laureate_prizes.Team == 'Yes') & (laureate_prizes.Gender == 'Male')]) / int(
laureate_prizes.Gender.value_counts()['Male'])
men_on = '{:.2f}%'.format(men_on * 100)
collaboration = pd.DataFrame({"Gender": ["Male", "Female"], "Percentage That Won on a Team": [men_on, women_on]})
ax = plt.subplot()
ax.axis('off')
ax.table(cellText=collaboration.values, cellLoc='center', colLabels=list(collaboration), loc='center',
bbox=[0, 0, .95, .95],)
ax.set_title('Nobel Laureate Team Participation by Gender')
plt.tight_layout()
plt.show()