forked from QuocAnhVu/LoL-Champion-Recommender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_preferences.py
executable file
·75 lines (63 loc) · 2.41 KB
/
predict_preferences.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
#!./env/bin/python
# Predict Preferences
# Algorithm: linear, gradient descent
# Generates the input user's preferences based on previously generated
# champion attribute data and user's champion masteries.
import numpy as np
import sys
from cassiopeia import baseriotapi as api
import secret_keys as keys
from model import *
# Train theta (user preferences)
def train_theta(x, y, theta, lamb, alpha, numIterations):
for i in range(numIterations):
hypothesis = np.dot(theta, np.transpose(x))
loss = np.multiply(hypothesis, y > 0) - y
theta = theta - alpha * (np.dot(loss, x) + lamb * theta)
return theta
def predict(summ_region, summ_name):
# Set up libraries, load saved x
x = np.load(open('tmp/trainedX_avg.npy', 'rb'))
api.set_api_key(keys.riotapikey)
api.set_region(summ_region)
# TODO: Handle unicode characters in summoner names
# Retrieve summoner
summ_name_key = summ_name.lower().replace(' ', '')
summ = api.get_summoners_by_name(summ_name)[summ_name_key]
# Create template summoner w/ empty ratings
db = Session()
champ_dict = {}
for champ in db.query(Champion):
champ_dict[champ.champion_id] = 0
# Fill in template summoner w/ specified summoner champion points
masteries = api.get_champion_masteries(summ.id)
for m in masteries:
champ_dict[m.championId] = m.championPoints
y_raw = [champ_dict[champ_id] for champ_id in sorted(champ_dict.keys())]
# Normalize summoner champion points
y_raw = np.asarray(y_raw)
y_std = np.std(y_raw)
y = y_raw / y_std
# Train theta for user
lamb = .1
alpha = .0001
iterations = 2000
feature_count = len(x[0])
init_theta = np.random.random_sample((1, feature_count))
theta = train_theta(x, y, init_theta, lamb, alpha, iterations)
# Create champ_id: champ_name dictionary
champ_dict = []
for champ in db.query(Champion):
champ_dict.append((champ.champion_id, champ.champion_name))
champ_dict = sorted(champ_dict, key=lambda x: x[0])
# Make predictions and print formatted results
h = np.dot(theta, x.T)
champ_ids, champ_names = zip(*champ_dict)
predictions = zip(champ_names, h[0])
predictions = sorted(predictions, key=lambda x: x[1])
return predictions
if(len(sys.argv) > 1):
summ_region = sys.argv[1]
summ_name = sys.argv[2]
predictions = predict(summ_region, summ_name)
print(predictions)