Last active
August 10, 2016 01:53
-
-
Save manuel-delverme/d66bc877511b40430b701bc07fb06a7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import scipy.cluster.hierarchy as hcluster | |
| from sklearn.manifold import TSNE | |
| import collections | |
| import random | |
| import fireplace.utils | |
| from fireplace import cards | |
| from hearthstone.enums import CardClass | |
| try: | |
| del decks | |
| del data | |
| del clusters | |
| except NameError: | |
| pass | |
| NUM_ARCHETYPES = 30 | |
| NUM_TECH_CARDS = 5 | |
| NUM_COMMON_CARDS = 30 - NUM_TECH_CARDS | |
| try: | |
| decks | |
| except NameError: | |
| cards.db.initialize() | |
| decks = [] | |
| archetypes = [] | |
| for _ in range(NUM_ARCHETYPES): | |
| deck = fireplace.utils.random_draft(CardClass.WARRIOR) | |
| archetypes.append(deck) | |
| for _ in range(1000 - NUM_ARCHETYPES): | |
| tech_cards = fireplace.utils.random_draft(CardClass.WARRIOR) | |
| base_deck = random.choice(archetypes) | |
| deck = base_deck[:NUM_COMMON_CARDS] + tech_cards[NUM_COMMON_CARDS:] | |
| decks.append(collections.Counter(deck)) | |
| # dims = { int(k.split("_")[-1]) for d in decks for k in d.keys() } | |
| try: | |
| data | |
| except NameError: | |
| lookup = list({card for deck in decks for card in deck}) | |
| data = [] | |
| for deck in decks: | |
| datapoint = [0] * len(lookup) | |
| for card in deck: | |
| card_dimension = lookup.index(card) | |
| datapoint[card_dimension] = deck[card] | |
| data.append(datapoint) | |
| data = np.array(data) | |
| try: | |
| clusters | |
| except NameError: | |
| clusters = range(1000) | |
| thresh = 0.5 | |
| # clustering | |
| while len(set(clusters)) > len(data)/5: # minimum 5 clusters <--- only parameter we have to tune [threshold i mean] | |
| thresh += 0.5 | |
| clusters = hcluster.fclusterdata(data, thresh, criterion="distance") | |
| print(thresh, len(set(clusters))) | |
| # plotting | |
| model = TSNE() | |
| embed = model.fit_transform(data) | |
| plt.axis("equal") | |
| plt.scatter(*np.transpose(embed), c=clusters) | |
| plt.title("thres %f; clusters: %d" % (thresh, len(set(clusters)))) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment