forked from STA220group/final_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_functions.py
150 lines (132 loc) · 5.71 KB
/
plot_functions.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
from clean import df
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotnine as p9
from wordcloud import WordCloud
from sklearn.feature_extraction import text
from sklearn.model_selection import cross_val_score
from sklearn import tree
import graphviz
def event_counts_date(request_disc=None):
'''
Plot the average timeline of a certain institution
request should be given as a dictionary
'''
request = np.ones(df.shape[0], dtype=bool)
for key in request_disc.keys():
if key == "institution":
request = request & (df[key].str.contains(request_disc[key]))
else:
request = request & (df[key] == request_disc[key])
df_selected = df[request]
df_selected["date_md"] = df_selected["admission_date"].apply(
lambda dt: dt.replace(year=1980))
df_selected["year"] = df_selected["admission_date"].apply(
lambda dt: dt.year)
samp = df[request].iloc[0]
title = ""
for key in request_disc.keys():
title += samp[key]
title += " "
gg = p9.ggplot(df_selected)
gg += p9.aes(x="date_md", y="admission_status")
gg += p9.scale_x_datetime(date_breaks='10 days',
date_labels="%m-%d",
limits=np.array([np.min(df_selected["date_md"]), pd.to_datetime("1980-4-20")]))
gg += p9.geom_count()
gg += p9.ggtitle(title)
return gg
def wordcloud_df(request_disc=None):
'''
request should be given as a dictionary
'''
request = np.ones(df.shape[0], dtype=bool)
for key in request_disc.keys():
if key == "institution":
request = request & (df[key].str.contains(request_disc[key]))
else:
request = request & (df[key] == request_disc[key])
wordcloud = WordCloud().generate(df[request]["notes"].str.cat())
samp = df[request].iloc[0]
title = ""
for key in request_disc.keys():
title += samp[key]
title += " "
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.title(title)
def prop_piechart(which="ST", request_disc=None):
'''
request should be given as a dictionary
'''
request = np.ones(df.shape[0], dtype=bool)
for key in request_disc.keys():
if key == "institution":
request = request & (df[key].str.contains(request_disc[key]))
else:
request = request & (df[key] == request_disc[key])
df_selected = df[request]
prop = df_selected[which].value_counts(sort=False)
prop /= np.sum(prop)
samp = df_selected.iloc[0]
title = ""
for key in request_disc.keys():
title += samp[key]
title += " "
if which == "ST":
plt.pie(
prop, explode=0.1*(prop.index == "I"), labels=prop.index, autopct='%1.1f%%', shadow=True)
ST_dic = {"A": "American", "U": "International, with US degree",
"I": "International, without US degree", "O": "Other"}
plt.legend([ST_dic[i] for i in prop.index], bbox_to_anchor=(1, 0.5), loc="center right",
bbox_transform=plt.gcf().transFigure)
plt.subplots_adjust(left=0.0, bottom=0.1, right=0.45)
elif which == "admission_status":
plt.pie(
prop, explode=0.1*(prop.index == "Accepted"), labels=prop.index, autopct='%1.1f%%', shadow=True)
plt.title(title)
#plt.savefig(fname=title + ".png", dpi=1000)
def notes_decision_tree_classifier():
df_sel = df[["notes", "admission_status"]].dropna()
tfidf = text.TfidfVectorizer()
X = tfidf.fit_transform(df_sel["notes"]).toarray()
feature_names = tfidf.get_feature_names()
y = df_sel["admission_status"]
clf = tree.DecisionTreeClassifier(random_state=0, ccp_alpha=0.001)
clf.fit(X, y)
print("20 Most Important Words: ", np.array(tfidf.get_feature_names())[
np.argsort(clf.feature_importances_)[::-1]][0:20])
print("10-fold Cross Validation Accuracy: ",
np.mean(cross_val_score(clf, X, y, cv=10)))
dot_data = tree.export_graphviz(clf, out_file=None,
max_depth=7, feature_names=feature_names, class_names=sorted(y.unique().tolist()),
filled=True, rounded=True,
rotate=True,
special_characters=True)
graph = graphviz.Source(dot_data)
return(graph)
if __name__ == "__main__":
event_counts_date({"institution": "Davis", "degree": "PhD"})
event_counts_date({"institution": "Stanford", "degree": "PhD"})
wordcloud_df({"degree": "PhD"})
wordcloud_df({"institution": "Davis", "degree": "PhD"})
prop_piechart("ST", {"institution": "Davis",
"admission_status": "Accepted", "degree": "PhD"})
prop_piechart("ST", {"institution": "Berkeley",
"admission_status": "Accepted", "degree": "PhD"})
def summary_ST_prop(x):
t = x["ST"].value_counts()
return t["I"]/np.sum([t[i] for i in t.index])
(df.loc[(df["admission_status"] == "Accepted") & (df["degree"] == "PhD")]).groupby(
"institution").apply(summary_ST_prop).sort_values(ascending=False)[df["institution"].value_counts() >= 50]
prop_piechart("admission_status", {
"institution": "Davis", "degree": "PhD"})
prop_piechart("admission_status", {
"institution": "Berkeley", "degree": "PhD"})
def summary_admission_prop(x):
t = x["admission_status"].value_counts()
return t["Accepted"] / (t["Accepted"] + t["Rejected"])
(df.loc[df["degree"] == "PhD"]).groupby(
"institution").apply(summary_ST_prop).sort_values()[df["institution"].value_counts() >= 50]
notes_decision_tree_classifier().render(filename="Notes_Tree")