def model3(record): """ Chooses the choice that would beat the player's least frequent recent choice""" if len(record['p1']) > 4: least_freq = record['p1'].value_counts().iloc[-5:].index[-1] choice = beats(least_freq) elif len(record['p1']) > 0: least_freq = record['p1'].value_counts().index[-1] choice = beats(least_freq) else: choice = 0 return int(choice)
def model2(record): """ Chooses the choice that would beat the player's most frequent recent choice. Based on repeated choices """ if len(record) >= 5: most_freq = record['p1'].value_counts().iloc[-5:].index[0] choice = beats(most_freq) elif len(record) > 0: most_freq = record['p1'].value_counts().index[0] choice = beats(most_freq) else: choice = 0 return int(choice)
def vectorize(choice1, choice2): """ returns a vector describing the difference between one choice and another """ if choice1 == beats(choice2): return 1 elif choice1 == loses_to(choice2): return -1 else: return 0
def model1(record): """ Vector-based choice based on past three rounds """ if len(record) > 1: if len(record) > 5: vectors = [] for i in range(1,5): vectors.append(vectorize(record['p1'].iloc[-i], record['p1'].iloc[-i-1])) vector = pd.Series(vectors).value_counts().index[0] else: vector = vectorize(record['p1'].iloc[-1], record['p1'].iloc[-2]) if vector == 1: choice = beats(beats(record['p1'].iloc[-1])) elif vector == -1: choice = beats(loses_to(record['p1'].iloc[-1])) elif vector == 0: choice = beats(record['p1'].iloc[-1]) else: choice = 0 return int(choice)
def model5(record): """ uses an sklearn decision tree model trained with historical data to predict next choice""" if len(record) < 5: return int(0) model = load(filename='decision_tree_clf.joblib') X,y,this_round = get_Xy(record) guess = int(model.predict(this_round.to_numpy().reshape(1, -1))) return beats(guess)
def model4(record): """ Uses a pickled tensorflow neural network model trained with historical data to predict next choice""" if len(record) < 8: return int(0) X,y,this_round = get_nn_Xy(record) nn_clf = load('nn_clf.joblib') curr = sk_flatten(this_round) guess = int(nn_clf.predict(curr)) return beats(guess)
def model0(record): """ Chooses the choice that would lose to or beat the player's last choice. Based on whether a player is changing their answers or not in the past three rounds. """ if len(record) > 3: repeats = 0 for i in range(1,3): repeats = repeats + (int(record['p1'].iloc[-i] == record['p1'].iloc[-i-1])) if repeats > 1: choice = beats(record['p1'].iloc[-1]) else: choice = loses_to(record['p1'].iloc[-1]) elif len(record) > 0: choice = loses_to(record['p1'].iloc[-1]) else: choice = int(randint(0,2)) return int(choice)
def score_model(model, record, drop_first=1): """ Returns a score for a model's performance based on its record """ n = len(record) - drop_first if n < 1: return -1.0 # creates a weighted model score that prioritizes recency model_record = [] w_max = 0 for j in range(drop_first,len(record)): if n == 1 and j == 0: return float(-1.0) if record.iloc[j][f'model{model}'] == beats(record['p1'].iloc[j]): model_record.append(j**2) elif record.iloc[j][f'model{model}'] == loses_to(record['p1'].iloc[j]): model_record.append(-j**2) w_max = w_max + j**2 weighted_model_score = np.sum(model_record) / w_max return weighted_model_score