In this file I'm gonna try to find appropriate description and distance functions for heroes. This is needed to apply Collaborative filtering in the future.
In [1]:
import json
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import (euclidean_distances,
manhattan_distances,
cosine_similarity,
cosine_distances)
from itertools import combinations, product
from atod import Heroes, Hero
In [2]:
heroes = Heroes.all()
data = pd.DataFrame([h.get_description(['name', 'laning', 'role', 'type'])
for h in heroes])
data.head()
Out[2]:
In [3]:
def hero_vector(name: str):
return data[data['name'] == name].drop(['name'], axis=1)
In [4]:
# find all the closest heroes to the
puck = hero_vector('Io')
distances = list()
for hero in heroes:
distances.append((hero.name,
cosine_similarity(puck, hero_vector(hero.name))[0][0]))
distances = list(reversed(sorted(distances, key=lambda x: x[1])))
print(distances[:10])
In [5]:
with open('data/players_in_matches.json') as fp:
players_in_matches = json.load(fp)
In [6]:
matches = dict()
for record in players_in_matches:
# create match in matches dictionary with arrays for
# winners and losers ids
matches.setdefault(str(record['match_id']),
{
'winners': [],
'loosers': [],
}
)
if record['win']:
# add hero to winners of this match
matches[str(record['match_id'])]['winners'].append(record['hero_id'])
else:
# add hero to losers
matches[str(record['match_id'])]['loosers'].append(record['hero_id'])
# length of matches should be 10 times smaller than length of players...
# since there are 10 players in each match
assert len(matches), len(players_in_matches) / 10
In [7]:
n_heroes = 115
hero2vec = np.zeros((n_heroes, n_heroes))
for match in matches.values():
# for winners
# sorting is needed to have upper traingular matrix
# combinations produces all heroes pairs with smaller id first
for hero1, hero2 in product(match['winners'], repeat=2):
hero2vec[hero1][hero2] += 1
for hero1, hero2 in product(match['loosers'], repeat=2):
hero2vec[hero1][hero2] += .75
In [10]:
id1 = Hero.from_name('Naga Siren').id
id2 = Hero.from_name('Anti-Mage').id
id3 = Hero.from_name('Beastmaster').id
id4 = Hero.from_name('Dazzle').id
id5 = Hero.from_name('Oracle').id
# print(hero2vec[id1])
print(cosine_distances(hero2vec[id1], hero2vec[id2]))
print(cosine_distances(hero2vec[id2], hero2vec[id3]))
print(cosine_distances(hero2vec[id1], hero2vec[id3]))
print(cosine_distances(hero2vec[id4], hero2vec[id5]))
In [ ]: