-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutlier_removal.py
43 lines (40 loc) · 1.61 KB
/
outlier_removal.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
import json
import numpy as np
data_file = 'data/scraper/data.json'
with open(data_file, 'r') as file:
data_content = file.read()
data = json.loads(data_content)
post_count = []
for item in data['data']:
post_count.append(len(item.get("posts")))
avg = sum(post_count) / len(post_count)
percentile_75 = np.percentile(post_count, 75)
max_threshold = avg + 1.5 * (percentile_75 - avg)
count = 0
for item in data['data']:
if len(item.get("posts")) > max_threshold:
print(data['data'][data['data'].index(item)].get("id") + " with " + str(
len(item.get("posts"))) + " post number deleted!")
del data['data'][data['data'].index(item)]
count += 1
print("Number of outliers: " + str(count))
open(data_file, "w").write(
json.dumps(data, sort_keys=True, indent=4)
)
# comment_count = []
# for item in data['data']:
# comment_count.append(len(item.get("comments")))
# avg = sum(comment_count) / len(comment_count)
# percentile_75 = np.percentile(comment_count, 75)
# max_threshold = avg + 1.5 * (percentile_75 - avg)
# count = 0
# for item in data['data']:
# if len(item.get("comments")) > max_threshold:
# print(data['data'][data['data'].index(item)].get("id") + " with " + str(
# len(item.get("comments"))) + " comment number deleted!")
# del data['data'][data['data'].index(item)]
# count += 1
# print("Number of outliers: " + str(count))
# open(data_file, "w").write(
# json.dumps(data, sort_keys=True, indent=4)
# )