-
Notifications
You must be signed in to change notification settings - Fork 0
/
lol_draft_simulator.py
110 lines (86 loc) · 3.76 KB
/
lol_draft_simulator.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Use Neural Networks to simulate a League of Legends draft."""
__file__ = 'lol_draft_simulator.py'
__author__ = 'Jesse Estes'
__copyright__ = 'Copyright 2022, LolDraftSimulator'
__credits__ = ['Jesse Estes']
__license__ = 'MIT'
__version__ = '1.0.2'
__maintainer__ = 'Jesse Estes'
__email__ = '[email protected]'
__status__ = 'Prototype'
# --------------------------------------------------------------------------- #
# Imports #
# --------------------------------------------------------------------------- #
# Standard libraries
# Third-party libraries
import pandas as pd
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
from sklearn.model_selection import train_test_split
# Owned libraries
# --------------------------------------------------------------------------- #
# Code #
# --------------------------------------------------------------------------- #
def main():
"""Read the data to be used and call necessary functions."""
# Read in the data and fill missing values
selection_data = pd.read_csv('picks_bans_2021.csv').fillna(method='ffill')
# Create a list to add selected champions to
selected_champions = []
# Create a counter to show what selection is being looked at
selection_counter = 0
# Simulate the draft
print('Beginning draft simulation.')
for draft_phase in selection_data.columns:
select_champions(selection_data, draft_phase, selected_champions)
# Print a blank line in certain spots for formatting
if draft_phase in ['Blue Ban 1', 'Blue Pick 1', 'Red Ban 4', 'Red Pick 4']:
print()
# Print the selection and look at the next selection
print('\t'+ draft_phase + ':\t', selected_champions[selection_counter])
selection_counter += 1
# End of program
print('\nDraft simulation finished!\n')
def select_champions(
data: pd.DataFrame,
phase: str,
selections: list
) -> None:
"""Use Neural Networks to classify champions selected in the given phase.
Args:
data: Champion selections and their selection phases
phase: Draft phase that the champion was selected in
selections: List of champions that have been selected
"""
# Determine what phase is being classified
target = data[phase]
# Create a modified copy of the DataFrame excluding the current phase
features = data.drop(phase, axis=1)
# Use Encoders to make the data readable by the Neural Network
features_encoder = OrdinalEncoder().fit(features)
encoded_features = features_encoder.transform(features)
target_encoder = LabelEncoder().fit(target)
encoded_target = target_encoder.transform(target)
# Split 80% of the features into training and 20% into testing
features_train, features_test, labels_train, labels_test = train_test_split(
encoded_features, encoded_target, test_size=0.2
)
# Remove the unused 'labels_test' variable
del labels_test
# Create and train a Neural Network classifier
classifier = MLPClassifier(max_iter=1500)
classifier.fit(features_train, labels_train)
# Determine the champion selected (picked or banned) in the given phase
predictions = classifier.predict(features_test)
# Un-encode the potential selections (i.e., revert to champion name)
predictions = target_encoder.inverse_transform(predictions)
# Remove duplicates and previously selected champions
predictions = list(set(predictions) - set(selections))
# Choose a random champion and add them to the list of selected champions
selection = np.random.choice(predictions, 1)
selections.append(str(selection[0]))
if __name__ == '__main__':
main()