Skip to content

Commit

Permalink
Added missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
NaelsonDouglas committed Jul 18, 2020
1 parent dc0759b commit c6bfe48
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/backend/classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from tables_manager import Tables
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
import logging
import pandas as pd

class Classifier:
def __init__(self):
self.tables_manager = Tables()
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
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)
self.confusion_matrix = pd.crosstab(self.y_test.ravel(),self.result, rownames=['Real'], colnames=['Predito'], margins=True)
self.fscore = f1_score(self.y_test.ravel(),self.result,average='macro')
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('_____________________')
print('Confusion-matrix: \n'+str(self.overview['Confusion-matrix']))
print('_____________________')
print('F-score: '+str(self.overview['F-score']))
print('_____________________')
print('=====================')
24 changes: 24 additions & 0 deletions src/backend/data_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from api import OpenDota, Steam
import logging
from context import Context
from configure_logging import configure_logging
configure_logging('main.py', logging.INFO)

class DataExtractor:
def __init__(self):
self.open_dota = OpenDota()
self.steam = Steam()
self.context = Context()

def import_matches(self,amount):
match_ids = self.open_dota.get_matches_ids(amount)
match_collection = self.context.db.get_collection('Matches')
for match_id in match_ids:
if match_collection.count_documents({ '_id': match_id }, limit = 1) == 0:
logging.debug('Accessing Steam to get info about match '+str(match_id))
splited_match = self.steam.get_match_detail(match_id)
if splited_match != None:
logging.info('Inserting match '+str(splited_match['_id'])+' on database.')
self.context.insert_one(splited_match,'Matches')
else:
logging.warn('Match '+str(match_id)+' already on database.')
18 changes: 18 additions & 0 deletions src/backend/knn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tables_manager import Tables
from classifier import Classifier
from sklearn.neighbors import KNeighborsClassifier

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

def print_results(self):
print('=====================')
print('Algorithm: knn')
print('K= '+str(self.k))
super().print_results()

20 changes: 20 additions & 0 deletions src/backend/tables_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import pandas as pd
import logging
import pymongo
from context import Context
import json

class Tables:
def __init__(self):
self.context = Context()

def get_all_matches(self):
matches_collection = self.context.db['Matches']
matches_cursor = matches_collection.find({})
p = list(matches_cursor)
result = pd.DataFrame(p)
#result['teams'] = result['teams'].map(lambda x :np.concatenate((x[0],x[1])))
#result['radiant_win'] = result['radiant_win'] * 1
return result

0 comments on commit c6bfe48

Please sign in to comment.