diff --git a/src/backend/classifier.py b/src/backend/classifier.py index 87adff9..08c84d8 100644 --- a/src/backend/classifier.py +++ b/src/backend/classifier.py @@ -5,12 +5,21 @@ 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. @@ -18,17 +27,18 @@ def __init__(self): 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('_____________________') diff --git a/src/backend/knn.py b/src/backend/knn.py index 24c375e..398ee4e 100644 --- a/src/backend/knn.py +++ b/src/backend/knn.py @@ -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('=====================') diff --git a/src/backend/main.py b/src/backend/main.py index d18b6b6..9fb319c 100644 --- a/src/backend/main.py +++ b/src/backend/main.py @@ -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) \ No newline at end of file +#tables_manager = Tables() +#print(tables_manager.get_all_matches()) +#knn = Knn(7) +random_forest = RandomForest() \ No newline at end of file diff --git a/src/backend/tables_manager.py b/src/backend/tables_manager.py index bfb44fe..7223619 100644 --- a/src/backend/tables_manager.py +++ b/src/backend/tables_manager.py @@ -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 diff --git a/src/configs.json b/src/configs.json index 0b09de5..0574e78 100644 --- a/src/configs.json +++ b/src/configs.json @@ -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", @@ -17,4 +17,4 @@ "databasePort": 27017 } } -} +} \ No newline at end of file