Ejemplo n.º 1
0
    def get(self, league, home_team, away_team):
        sp_model_file_name = ''

        print('get : {}/{}/{}'.format(league, home_team, away_team))

        if league in ['England', 'Spain', 'Italy', 'Germany']:
            sp_model_file_name = 'modelForProd/' + league.lower(
            ) + '_sp_model.joblib'
        else:
            return "league : {} is not supported".format(league), 404

        print('load {}'.format(sp_model_file_name))
        model = load(sp_model_file_name)

        get_db_connector = mongo_API()
        get_data_manger = FileDataManger(get_db_connector)
        get_redwoodParser = RedWoodParser()
        get_data_predict_org = Data_Predict_Organizer()

        sp_model = SoccerPredictModel(model, get_redwoodParser,
                                      get_data_manger, league,
                                      get_data_predict_org)
        mapped_home_team = mapper.map(home_team)
        print(mapped_home_team)
        mapped_away_team = mapper.map(away_team)
        print(mapped_away_team)
        res = sp_model.predict(mapped_home_team, mapped_away_team, 6)
        print(res)

        return res, 200
Ejemplo n.º 2
0
    def test_get_collection_without_key_val(self):
        mongoApi = mongo_API()
        res_Cursor = mongoApi.get_collection(db_name=self.db_name, collection=self.db_collection)

        res = [json for json in res_Cursor]
        res_Cursor.close()

        for r in res:
            del r['_id']

        for index in range(len(self.res_list)):
            assert res[index] == self.res_list[index]
Ejemplo n.º 3
0
    def test_basic_model_english_random_forest(self):
        db_connector = mongo_API()
        data_manger = DataManger(db_connector)
        redwoodParser = RedWoodParser()
        model = RandomForestClassifier()
        data_predict_org = Data_Predict_Organizer()

        sp_model = SoccerPredictModel(model, redwoodParser, data_manger,
                                      'England', data_predict_org)
        sp_model.train(7, 0.1)

        test(sp_model, 10, self.english_csv_file)
Ejemplo n.º 4
0
    def test_basic_model_italy(self):
        db_connector = mongo_API()
        data_manger = DataManger(db_connector)
        redwoodParser = RedWoodParser()
        model = XGBClassifier(max_depth=3, booster='gblinear')
        data_predict_org = Data_Predict_Organizer()

        sp_model_italy = SoccerPredictModel(model, redwoodParser, data_manger,
                                            'Italy', data_predict_org)
        sp_model_italy.train(7, 0.1)

        test(sp_model_italy, 7, self.italy_csv_file)
Ejemplo n.º 5
0
    def test_basic_model_english_KNN(self):
        db_connector = mongo_API()
        data_manger = DataManger(db_connector)
        redwoodParser = RedWoodParser()
        model = KNeighborsClassifier(n_neighbors=3, )
        data_predict_org = Data_Predict_Organizer()

        sp_model = SoccerPredictModel(model, redwoodParser, data_manger,
                                      'England', data_predict_org)
        sp_model.train(7, 0.1)

        test(sp_model, 6, self.english_csv_file)
Ejemplo n.º 6
0
class MyTestCase(unittest.TestCase):
    mongoApi = mongo_API()
    print(mongoApi.get_ip_and_port())
    db_name='testDB'
    db_collection='testCollection'
    res_list = []
    special_key_val = {'HomeTeamName': ["home team test name"],
                       'AwayTeamName': ["away team test name"],
                       'homeTeamSecondHalfTestGoals': 3,
                       'AwayTeamFirstHalfSubTest': 7,
                       'RoundIdTest': 12}

    def test_insert_Json_File(self):
        self.res_list = self.mongoApi.insert_json_file('/Users/meornbru/PycharmProjects/SoccerProject/DataParser/testJson.json',
                                       db_name=self.db_name,
                                       collection=self.db_collection)
        assert len(self.res_list) == 5

    def test_get_collection_without_key_val(self):
        mongoApi = mongo_API()
        res_Cursor = mongoApi.get_collection(db_name=self.db_name, collection=self.db_collection)

        res = [json for json in res_Cursor]
        res_Cursor.close()

        for r in res:
            del r['_id']

        for index in range(len(self.res_list)):
            assert res[index] == self.res_list[index]

    def test_get_collection_with_key_val(self):
        mongoApi = mongo_API()
        res_Cursor = mongoApi.get_collection(db_name=self.db_name, collection=self.db_collection, filter=self.special_key_val)

        res = [json for json in res_Cursor]
        res_Cursor.close()

        for r in res:
            del r['_id']

        for index in range(len(self.res_list)):
            assert res[index] == self.res_list[index]

        for k, v in self.special_key_val.items():
            res_Cursor = mongoApi.get_collection(self.db_name, self.db_collection, {k: v})
            res = [json for json in res_Cursor]
            res_Cursor.close()
            assert res[0][k] == v
Ejemplo n.º 7
0
    def test_basic_model_english_xg(self):
        db_connector = mongo_API()
        data_manger = DataManger(db_connector)
        redwoodParser = RedWoodParser()
        ignore_list = [
            'Date', 'TournamentName', 'SeasonName', 'RoundId', 'HomeTeamName',
            'AwayTeamName'
        ]
        redwoodParser.set_ignore_list(ignore_list)
        model = XGBClassifier(max_depth=5, booster='gblinear')
        data_predict_org = Data_Predict_Organizer()

        sp_model = SoccerPredictModel(model, redwoodParser, data_manger,
                                      'England', data_predict_org)
        sp_model.train(7, 0.1)

        test(sp_model, 6, self.english_csv_file)
Ejemplo n.º 8
0
    def test_get_collection_with_key_val(self):
        mongoApi = mongo_API()
        res_Cursor = mongoApi.get_collection(db_name=self.db_name, collection=self.db_collection, filter=self.special_key_val)

        res = [json for json in res_Cursor]
        res_Cursor.close()

        for r in res:
            del r['_id']

        for index in range(len(self.res_list)):
            assert res[index] == self.res_list[index]

        for k, v in self.special_key_val.items():
            res_Cursor = mongoApi.get_collection(self.db_name, self.db_collection, {k: v})
            res = [json for json in res_Cursor]
            res_Cursor.close()
            assert res[0][k] == v
Ejemplo n.º 9
0
from model.dataManger import FileDataManger
from model.soccer_predict_model import SoccerPredictModel
from model.Data_Predict_Organizer import Data_Predict_Organizer

from model.teams_mapper import TeamMapper as mapper

# from xgboost import XGBClassifier

from joblib import load

app = Flask(__name__)
api = Api(app)
API = CORS(app, supports_credentials=True)

get_db_connector = mongo_API()
data_manger = FileDataManger(get_db_connector)
redwoodParser = RedWoodParser()
data_predict_org = Data_Predict_Organizer()


class ModelServer(Resource):
    def get(self, league, home_team, away_team):
        sp_model_file_name = ''

        print('get : {}/{}/{}'.format(league, home_team, away_team))

        if league in ['England', 'Spain', 'Italy', 'Germany']:
            sp_model_file_name = 'modelForProd/' + league.lower(
            ) + '_sp_model.joblib'
        else:
Ejemplo n.º 10
0
    def test_grid_search_english_model_xgboost(self):
        # general things:
        db_connector = mongo_API()
        data_manger = DataManger(db_connector)
        redwoodParser = RedWoodParser()
        data_predict_org = Data_Predict_Organizer()

        # model params
        max_depths = range(1, 22, 1)
        learning_rates = [x / 20 for x in range(1, 21)]
        n_estimators = range(50, 400, 50)
        objectives = [
            "binary:logistic", "reg:linear", "reg:logistic", "binary:logistic",
            "binary:logitraw", "count:poisson", "multi:softmax",
            "multi:softprob", "rank:pairwise"
        ]
        boosters = ['gbtree', 'gblinear']
        gammas = [x / 20 for x in range(0, 105, 5)]
        min_child_weights = [x / 2 for x in range(0, 21)]
        max_delta_steps = range(0, 10, 1)
        subsamples = [x / 20 for x in range(0, 21)]
        colsample_bytrees = [x / 20 for x in range(0, 21)]
        #reg_alpha = 0
        #reg_lambda = 1
        base_score = [x / 20 for x in range(1, 21)]
        num_of_games = range(0, 15)

        test_results = []

        for md in max_depths:
            for lr in learning_rates:
                for ne in n_estimators:
                    for obj in objectives:
                        for booster in boosters:
                            for g in gammas:
                                for min_child_w in min_child_weights:
                                    for max_delta_step in max_delta_steps:
                                        for sub_sample in subsamples:
                                            for cb in colsample_bytrees:
                                                for bs in base_score:
                                                    model = XGBClassifier(
                                                        max_depth=md,
                                                        learning_rate=lr,
                                                        n_estimators=ne,
                                                        objective=obj,
                                                        booster=booster,
                                                        gamma=g,
                                                        min_child_weight=
                                                        min_child_w,
                                                        max_delta_step=
                                                        max_delta_step,
                                                        subsample=sub_sample,
                                                        colsample_bytree=cb,
                                                        base_score=bs)
                                                    sp_model = SoccerPredictModel(
                                                        model, redwoodParser,
                                                        data_manger, 'England',
                                                        data_predict_org)
                                                    sp_model.train(7, 0.1)
                                                    for num in num_of_games:
                                                        res = test(
                                                            sp_model, num,
                                                            self.
                                                            english_csv_file)
                                                        test_results.append({
                                                            'max_depth':
                                                            md,
                                                            'learning_rate':
                                                            lr,
                                                            'n_estimators':
                                                            ne,
                                                            'objective':
                                                            obj,
                                                            'booster':
                                                            booster,
                                                            'gamma':
                                                            g,
                                                            'min_child_weight':
                                                            min_child_w,
                                                            'max_delta_step':
                                                            max_delta_step,
                                                            'subsample':
                                                            sub_sample,
                                                            'colsample_bytree':
                                                            cb,
                                                            'base_score':
                                                            bs,
                                                            'res':
                                                            res
                                                        })

        print(test_results)
        with open(self.english_xg_result) as res_file:
            # save fields names
            for k, v in test_results[0].items():
                res_file.write(k + ',')

            for result in test_results:
                for k, v in result.items():
                    res_file.write(v + ',')