Skip to content

Commit

Permalink
Tagging the alfa version
Browse files Browse the repository at this point in the history
  • Loading branch information
NaelsonDouglas committed Jul 23, 2020
1 parent c6bfe48 commit 29be49b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
28 changes: 19 additions & 9 deletions src/backend/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@
import pandas as pd

class Classifier:
def __init__(self):
self.tables_manager = Tables()
def __init__(self,is_dummy):
self.tables_manager = Tables(is_dummy)
self.data = self.tables_manager.get_all_matches().dropna()
logging.info(self.data.head())
self.x = self.data[['R1','R2','R3','R4','R5','D1','D2','D3','D4','D5']].values
self.y = self.data[['radiant_win']].values
if is_dummy:
self.x = self.data[range(1,130)].values
self.y = self.data[['win']].values
else:
self.x = self.data[['R1','R2','R3','R4','R5','D1','D2','D3','D4','D5']].values
self.y = self.data[['radiant_win']].values
#self.x = self.data[['R1','R2','R3','R4','R5']].values


logging.info(self.x)
logging.info(self.y)
self.test_size=0.3
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(self.x, self.y, test_size=self.test_size)
self.classifier = None #To be filled by child class.

def train(self):
self.classifier.fit(self.x_train, self.y_train.ravel())

def results(self):
self.result = self.classifier.predict(self.x_test)
def results(self):
self.result = self.classifier.predict(self.x_test)
self.confusion_matrix = pd.crosstab(self.y_test.ravel(),self.result, rownames=['Real'], colnames=['Predito'], margins=True)
print(self.confusion_matrix)
self.fscore = f1_score(self.y_test.ravel(),self.result,average='macro')
self.overview = {'F-score': self.fscore, 'Confusion-matrix': self.confusion_matrix}
self.overview = {'F-score': self.fscore, 'Confusion-matrix': self.confusion_matrix}
self.print_results()
return self.overview

def print_results(self):
print('Training dataset size: '+str(len(self.data.index)))
print('Training dataset percentage: '+str(100*self.test_size)+"%")
print('Dataset size: '+str(len(self.data.index)))
print('Training percentage: '+str(100*self.test_size)+"%")
print('_____________________')
print('Confusion-matrix: \n'+str(self.overview['Confusion-matrix']))
print('_____________________')
Expand Down
5 changes: 3 additions & 2 deletions src/backend/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from classifier import Classifier
from sklearn.neighbors import KNeighborsClassifier

class Knn(Classifier):]
class Knn(Classifier):
def __init__(self,k):
super().__init__()
super().__init__(False)
self.k = k
self.classifier = KNeighborsClassifier(n_neighbors=self.k)
self.train()
self.results()
self.dummy = True

def print_results(self):
print('=====================')
Expand Down
7 changes: 5 additions & 2 deletions src/backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from tables_manager import Tables
import logging
from knn import Knn
from random_forest import RandomForest
import pandas as pd

tables_manager = Tables()
knn = Knn(7)
#tables_manager = Tables()
#print(tables_manager.get_all_matches())
#knn = Knn(7)
random_forest = RandomForest()
31 changes: 26 additions & 5 deletions src/backend/tables_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,35 @@
import json

class Tables:
def __init__(self):
self.context = Context()
def __init__(self,is_dummy = False):
self.context = Context()
self.dummy = is_dummy

def get_all_matches(self):
matches_collection = self.context.db['Matches']
matches_cursor = matches_collection.find({})
p = list(matches_cursor)
result = pd.DataFrame(p)
matches_cursor = matches_collection.find({})
cursor_list = list(matches_cursor)
result = None
if self.dummy:
dummy_cursor = []
heroes_range = ['_id', 'win']
heroes_range = heroes_range+list(range(1,130))
for match in cursor_list:
radiant_row = dict.fromkeys(heroes_range,0)
dire_row = dict.fromkeys(heroes_range,0)
radiant_row['_id'] = match['_id']
dire_row['_id'] = match['_id']
for r in ['R1','R2','R3','R4','R5']:
radiant_row[match[r]] = 1
radiant_row['win'] = match['radiant_win']
for d in ['D1','D2','D3','D4','D5']:
dire_row[match[d]] = 1
dire_row['win'] = not match['radiant_win']
dummy_cursor.append(dire_row)
dummy_cursor.append(radiant_row)
result = pd.DataFrame(dummy_cursor)
else:
result = pd.DataFrame(cursor_list)
#result['teams'] = result['teams'].map(lambda x :np.concatenate((x[0],x[1])))
#result['radiant_win'] = result['radiant_win'] * 1
return result
Expand Down
4 changes: 2 additions & 2 deletions src/configs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"local":{
"openDotaApiBaseUrl": "https://api.opendota.com/api",
"steamBaseUrl": "https://api.steampowered.com/IDOTA2Match_570",
"steamApiKey": "NULL",
"steamApiKey": "null",
"imagesBaseUrl": "http://cdn.dota2.com",
"databasePort": 27017,
"databaseIP": "localhost",
Expand All @@ -17,4 +17,4 @@
"databasePort": 27017
}
}
}
}

0 comments on commit 29be49b

Please sign in to comment.