-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.py
95 lines (69 loc) · 2.4 KB
/
naive.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
from sklearn import tree
from sklearn.model_selection import cross_val_score
from sklearn import preprocessing
"""
Create the histogramme of possible actions, then make a decision tree
"""
list_actions = set()
features = []
players = []
le = preprocessing.LabelEncoder()
le.fit(['Terran', 'Protoss', 'Zerg'])
def extractFeatures(actions, list_actions):
actions_histo = {}
real_actions = actions[1::2]
for action in list_actions:
actions_histo[action] = 0
# we create the histogramme
for action in real_actions:
actions_histo[action] += 1
race = le.transform([actions[0]])
#now we transform action to a vector
features_player = [race]
for key,value in actions_histo.items():
features_player.append(value/len(actions))
#we add the apm
apm = len(real_actions)/int(actions[len(actions)-1])*30*60
features_player.append(apm)
return features_player
def write_output(list_actions):
predict_features = []
row_num = []
with open('data/test.csv', 'r') as csvfile:
for row in csvfile:
row = row.split(';')
actions = row[1].split(',')
features_player = extractFeatures(actions, list_actions)
predict_features.append(features_player)
row_num.append(row[0])
predictions = clf.predict(predict_features)
#OUTPUT
with open('output.csv', 'w') as csvfile:
text_to_write = 'row ID,battleneturl\n'
for i in range(len(row_num)):
text_to_write += '{},{}\n'.format(row_num[i], predictions[i])
csvfile.write(text_to_write)
def list_all_actions(list_actions):
with open('data/train.csv', 'r') as csvfile:
# we add all possible actions to list_actions
for row in csvfile:
row = row.split(';')
actions = row[1].split(',')
for i in range(1,len(actions),2):
list_actions.add(actions[i])
#TRAINING
list_all_actions(list_actions)
with open('data/train.csv', 'r') as csvfile:
for row in csvfile:
row = row.split(';')
player = row[0]
actions = row[1].split(',')
features_player = extractFeatures(actions, list_actions)
features.append(features_player)
players.append(player)
clf = tree.DecisionTreeClassifier()
# clf = clf.fit(features, players)
scores = cross_val_score(clf, features, players, cv=10)
print(scores)
#TEST
#write_output()