-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_scores.py
50 lines (38 loc) · 2.01 KB
/
predict_scores.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
import json, os
def predict_score(user_id, predict_based_on):
with open('json/' + str(user_id) + '.json') as json_file:
this_user_dict = json.loads(json_file.readline())
with open('user_id_score_vector_dict.json') as json_file:
all_users_scores_dict = json.loads(json_file.readline())
if user_id not in all_users_scores_dict:
return 50, 50, 0
actual_score = all_users_scores_dict[str(user_id)][-1]
scores_sum = 0
if not predict_based_on in this_user_dict:
return 50, actual_score, 0 # data was not public. can make no assessment based on this criteria
friend_count = len(this_user_dict[predict_based_on])
count = 0
for friend in this_user_dict[predict_based_on]:
if str(friend) in all_users_scores_dict:
count += 1
scores_sum += all_users_scores_dict[str(friend)][-1]
if count == 0:
return 50, actual_score, 0
predicted_score = scores_sum / float(count)
return predicted_score, actual_score, count / float(friend_count)
def predict_all_scores(predict_based_on):
correctCount = 0
predictedCount = 0
for file_name in os.listdir('json'): # grabbing all ids from json files from current directory
if file_name.endswith(".json"):
predicted_score, actual_score, confidence = predict_score(file_name[:-5], predict_based_on)
if confidence > .9 and actual_score != 50 and predicted_score != 50:
#print "predicted: %f\t\tactual: %d\t\tconfidence: %f" % (predicted_score, actual_score, confidence)
if (predicted_score < 50 and actual_score < 50) or (predicted_score > 50 and actual_score > 50):
correctCount += 1
predictedCount += 1
prediction_accuracy = correctCount / float(predictedCount)
return prediction_accuracy
#predict_score(26589987)
print "Score prediction accuracy using friends_ids: %f" % predict_all_scores("friends_ids")
print "Score prediction accuracy using followers_ids: %f" % predict_all_scores("followers_ids")