forked from mikeyin97/Anime-Data-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_graphs.py
More file actions
280 lines (238 loc) · 9.89 KB
/
time_graphs.py
File metadata and controls
280 lines (238 loc) · 9.89 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import seaborn as sns
import pandas as pd
import inspect
import os
import matplotlib.pyplot as plt
import numpy as np
sns.set()
tips = sns.load_dataset("tips")
def ecdf(data):
n = len(data)
x = np.sort(data)
y = np.arange(1, n+1) / n
return x, y
def initialize_data():
module_path = inspect.getfile(inspect.currentframe())
module_dir = os.path.realpath(os.path.dirname(module_path))
data = pd.read_csv(module_dir+"/my_csv2.csv", encoding = "ISO-8859-1")
data = data.set_index("ID")
data[['Score','Popularity','ScoredBy']] = data[['Score','Popularity','ScoredBy']].apply(pd.to_numeric,errors='coerce')
tv_data = data[data["Type"] == "TV"]
ova_data = data[data["Type"] == "OVA"]
combined_data = pd.concat([tv_data, ova_data])
non_h_combined_data = combined_data[combined_data["Rating"] != "Rx - Hentai"]
cleaned_data = non_h_combined_data[non_h_combined_data["ScoredBy"] >= 50]
cleaned_data[["ScoredBy"]] = cleaned_data[["ScoredBy"]].apply(np.log)
return cleaned_data
def make_histogram(data, variable):
n_bins = int(np.sqrt(len(data[variable])))
data = data[variable].dropna()
plt.hist(data, bins = n_bins)
if variable == "ScoredBy":
plt.xlabel("log " + variable)
plt.ylabel("Frequency")
plt.title("Histogram of log " + variable)
else:
plt.xlabel(variable)
plt.ylabel("Frequency")
plt.title("Histogram of " + variable)
plt.show()
def make_exp_histogram(data, variable):
n_bins = int(np.sqrt(len(data[variable])))
data[[variable]] = data[[variable]].apply(np.exp)
plt.hist(data[variable].dropna(), bins = n_bins)
plt.xlabel(variable)
plt.ylabel("Frequency")
plt.title("Histogram of " + variable)
plt.show()
def make_ecdf(data, variable):
x,y = ecdf(data[variable].tolist())
plt.plot(x,y,marker = ".", linestyle = "none")
print(np.mean(data[variable]))
if variable == "ScoredBy":
plt.xlabel("log " + variable)
plt.ylabel("Cumulative Probability")
plt.title("ECDF of log " + variable)
else:
plt.xlabel(variable)
plt.ylabel("Cumulative Probability")
plt.title("ECDF of " + variable)
sns.plt.show()
def make_exp_ecdf(data, variable):
data[[variable]] = data[[variable]].apply(np.exp)
x,y = ecdf(data[variable].tolist())
plt.plot(x,y,marker = ".", linestyle = "none")
plt.xlabel(variable)
plt.ylabel("Cumulative Probability")
plt.title("ECDF of " + variable)
sns.plt.show()
def studio_vs_ratings(non_h_combined_data):
studio_means = []
studio_stddev = []
all_studio_data = []
non_h_combined_data['Std'] = ""
for studio in (non_h_combined_data.Studios.unique()):
if ("," not in studio) and (studio != "None found, add some") and ((non_h_combined_data['Studios'] == studio).sum() > 20):
studio_data = non_h_combined_data[non_h_combined_data['Studios'].str.contains(studio)]
(studio_data.loc[:,['Std']]) = studio
all_studio_data.append(studio_data)
studio_means.append(round(float(studio_data.loc[:,["Score"]].mean()),2))
studio_stddev.append(round(float(studio_data.loc[:,["Score"]].std()),2))
result = pd.concat(all_studio_data)
ax = sns.boxplot(x="Std", y="Score", data=result)
plt.xlabel("Studios")
plt.ylabel("Score")
plt.title("Studio vs Score for Studios with >20 Titles")
plt.xticks(rotation=90)
plt.tight_layout()
sns.plt.show()
return studio_means, studio_stddev
def studio_vs_popularity(non_h_combined_data):
studio_means = []
studio_stddev = []
all_studio_data = []
non_h_combined_data['Std'] = ""
for studio in (non_h_combined_data.Studios.unique()):
if ("," not in studio) and (studio != "None found, add some") and ((non_h_combined_data['Studios'] == studio).sum() > 20):
studio_data = non_h_combined_data[non_h_combined_data['Studios'].str.contains(studio)]
(studio_data.loc[:,['Std']]) = studio
all_studio_data.append(studio_data)
studio_means.append(round(float(studio_data.loc[:,["ScoredBy"]].mean()),2))
studio_stddev.append(round(float(studio_data.loc[:,["ScoredBy"]].std()),2))
result = pd.concat(all_studio_data)
ax = sns.boxplot(x="Std", y="ScoredBy", data=result)
plt.xlabel("Studios")
plt.ylabel("log(Number of People Scored)")
plt.title("Studio vs log(Number of People Scored) for Studios with >20 Titles")
plt.xticks(rotation=90)
plt.tight_layout()
sns.plt.show()
return studio_means, studio_stddev
def source_vs_ratings(non_h_combined_data):
source_means = []
source_stddev = []
all_source_data = []
source_means = []
source_stddev = []
for source in (non_h_combined_data.Source.unique()):
source_data = non_h_combined_data[non_h_combined_data['Source'] == source]
all_source_data.append(source_data)
source_means.append(round(float(source_data.loc[:,["Score"]].mean()),2))
source_stddev.append(round(float(source_data.loc[:,["Score"]].std()),2))
result = pd.concat(all_source_data)
ax = sns.boxplot(x="Source", y="Score", data=result)
plt.xlabel("Source")
plt.ylabel("Score")
plt.title("Source vs Score")
plt.xticks(rotation=45)
plt.tight_layout()
sns.plt.show()
return source_means, source_stddev
def source_vs_popularity(non_h_combined_data):
all_source_data = []
source_means = []
source_stddev = []
for source in (non_h_combined_data.Source.unique()):
source_data = non_h_combined_data[non_h_combined_data['Source'] == source]
all_source_data.append(source_data)
source_means.append(round(float(source_data.loc[:,["ScoredBy"]].mean()),2))
source_stddev.append(round(float(source_data.loc[:,["ScoredBy"]].std()),2))
result = pd.concat(all_source_data)
ax = sns.boxplot(x="Source", y="ScoredBy", data=result)
plt.xlabel("Source")
plt.ylabel("log(Number of People Scored)")
plt.title("Source vs log(Number of People Scored)")
plt.xticks(rotation=45)
plt.tight_layout()
sns.plt.show()
return source_means, source_stddev
def genre_vs_ratings(non_h_combined_data):
temp = non_h_combined_data
non_h_combined_data = non_h_combined_data.astype('object')
for i in non_h_combined_data.index.values.tolist():
a = [x.strip() for x in non_h_combined_data.loc[i]["Genres"].split(',')]
non_h_combined_data["Genres"][i] = a
unique_genres = []
genre_means = []
genre_stddev = []
count = 0
for genre_list in non_h_combined_data["Genres"]:
for j in range(len(genre_list)):
if genre_list[j] not in unique_genres:
unique_genres.append(genre_list[j])
count += 1
unique_genres.remove('No genres have been added y')
print(unique_genres)
non_h_combined_data = temp
non_h_combined_data['Genre'] = ""
all_genre_data = []
for genre in (unique_genres):
genre_data = non_h_combined_data[non_h_combined_data['Genres'].str.contains(genre)]
(non_h_combined_data.head())
genre_data.loc[:,['Genre']] = genre
all_genre_data.append(genre_data)
genre_means.append(round(float(genre_data.loc[:,["Score"]].mean()),2))
genre_stddev.append(round(float(genre_data.loc[:,["Score"]].std()),2))
result = pd.concat(all_genre_data)
ax = sns.boxplot(x="Genre", y="Score", data=result)
plt.xlabel("Genre")
plt.ylabel("Score")
plt.title("Genre vs Score")
plt.xticks(rotation=90)
plt.tight_layout()
sns.plt.show()
return genre_means, genre_stddev
def genre_vs_popularity(non_h_combined_data):
temp = non_h_combined_data
non_h_combined_data = non_h_combined_data.astype('object')
for i in non_h_combined_data.index.values.tolist():
a = [x.strip() for x in non_h_combined_data.loc[i]["Genres"].split(',')]
non_h_combined_data["Genres"][i] = a
genre_means = []
genre_stddev = []
unique_genres = []
count = 0
for genre_list in non_h_combined_data["Genres"]:
for j in range(len(genre_list)):
if genre_list[j] not in unique_genres:
unique_genres.append(genre_list[j])
count += 1
unique_genres.remove('No genres have been added y')
non_h_combined_data = temp
non_h_combined_data['Genre'] = ""
all_genre_data = []
for genre in (unique_genres):
genre_data = non_h_combined_data[non_h_combined_data['Genres'].str.contains(genre)]
(non_h_combined_data.head())
genre_data.loc[:,['Genre']] = genre
all_genre_data.append(genre_data)
genre_means.append(round(float(genre_data.loc[:,["ScoredBy"]].mean()),2))
genre_stddev.append(round(float(genre_data.loc[:,["ScoredBy"]].std()),2))
result = pd.concat(all_genre_data)
ax = sns.boxplot(x="Genre", y="ScoredBy", data=result)
plt.xlabel("Genre")
plt.ylabel("log(Number of People Scored)")
plt.title("Genre vs log(Number of People Scored)")
plt.xticks(rotation=90)
plt.tight_layout()
sns.plt.show()
return genre_means, genre_stddev
def testing(non_h_combined_data):
#print((non_h_combined_data.Source.value_counts()))
return
if __name__ == "__main__":
data = initialize_data()
#mean, stddev = studio_vs_popularity(data)
#mean, stddev = studio_vs_ratings(data)
#mean, stddev = source_vs_popularity(data)
#mean, stddev = source_vs_ratings(data)
#mean, stddev = genre_vs_popularity(data)
#mean, stddev = genre_vs_ratings(data)
make_ecdf(data, "Score")
make_ecdf(data, "ScoredBy")
#make_exp_ecdf(data, "ScoredBy")
#make_histogram(data, "Score")
#make_histogram(data, "ScoredBy")
#make_exp_histogram(data, "ScoredBy")
testing(data)
print(mean, stddev)