Naprawa kategorycznych, Zmiana zapisywania/wczytywania xgboost

This commit is contained in:
Maciej Chmielarz 2024-01-19 15:25:32 +01:00
parent 3749cf22b0
commit db214d217e
9 changed files with 830 additions and 73 deletions

View File

@ -25,12 +25,12 @@ const Hero = () => {
const handleOneOnOneChange = (event) => {
SetoneOnOne(event.target.checked);
sentQuestion()
sentQuestion();
};
const handleAfterAirDuelChange = (event) => {
SetafterAirDuele(event.target.checked);
sentQuestion()
sentQuestion();
};
const handleOpenGoalChange = (event) => {
@ -301,7 +301,7 @@ const handleRedirectChange = (event) => {
///Dziwny Blad
loadPlayers()
if (number_of_shooters_rev.current == 1) {
console.log('Wysyłanie wartości: ', bodyPart, technique, actionType, shooterPossition, gameMinute, firstShot);
//console.log('Wysyłanie wartości: ', bodyPart, technique, actionType, shooterPossition, gameMinute, firstShot);
// Użyj backticksów zamiast zwykłych cudzysłowów
fetch(`http://127.0.0.1:5000/get_model?shooter=${shooter}&goalkeeper=${goalkeeper}&defenders=${defenders}&strickers=${stricers}&bodyPart=${bodyPart}&technique=${technique}&actionType=${actionType}&shooterPossition=${shooterPossition}&gameMinute=${gameMinute}&shot_first_time=${firstShot}&shot_one_on_one=${oneOnOne}&shot_aerial_won=${afterAirDuel}&shot_open_goal=${openGoal}&shot_follows_dribble=${afterDribbling}&shot_redirect=${redirect}`).then(
res => res.json()

Binary file not shown.

View File

@ -3,6 +3,8 @@ import pandas as pd
from math import sqrt
import math
import numpy as np
import xgboost
from sklearn.preprocessing import OrdinalEncoder
# Funkcja zwraca prawdopodobieństwo zdobycia gola
def LogisticRegression_predict_proba(position_x, position_y, distance_to_goalM, angle, match_minute, Number_Intervening_Opponents, Number_Intervening_Teammates, isFoot, isHead):
@ -36,7 +38,11 @@ def xgboost_predict_proba(minute=0, position_name='Center Forward', shot_body_pa
y_player_teammate_10=np.nan, x_player_opponent_7=np.nan, y_player_opponent_7=np.nan,
x_player_teammate_Goalkeeper=np.nan, y_player_teammate_Goalkeeper=np.nan):
model = load('xgboost.joblib')
model = xgboost.XGBClassifier()
model.load_model('xgboost.json')
enc = OrdinalEncoder()
enc = load('labelEncoder.joblib')
X_new = pd.DataFrame(columns=['minute', 'position_name', 'shot_body_part_name', 'shot_technique_name',
'shot_type_name', 'shot_first_time', 'shot_one_on_one',
@ -85,18 +91,37 @@ def xgboost_predict_proba(minute=0, position_name='Center Forward', shot_body_pa
x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper]
X_new[['position_name',
'shot_technique_name',
'shot_type_name',
'shot_body_part_name']] = enc.transform(X_new[['position_name', 'shot_technique_name', 'shot_type_name', 'shot_body_part_name']], )
X_new[['minute',
'position_name',
'shot_technique_name',
'shot_type_name',
'number_of_players_opponents',
'number_of_players_teammates',
'shot_body_part_name']] = X_new[['position_name',
'shot_body_part_name']] = X_new[['minute',
'position_name',
'shot_technique_name',
'shot_type_name',
'number_of_players_opponents',
'number_of_players_teammates',
'shot_body_part_name']].astype('category')
'shot_body_part_name']].astype(int)
X_new['minute'] = X_new['minute'].astype(int)
# X_new[['minute',
# 'position_name',
# 'shot_technique_name',
# 'shot_type_name',
# 'number_of_players_opponents',
# 'number_of_players_teammates',
# 'shot_body_part_name']] = X_new[['minute',
# 'position_name',
# 'shot_technique_name',
# 'shot_type_name',
# 'number_of_players_opponents',
# 'number_of_players_teammates',
# 'shot_body_part_name']].astype('category')
X_new[['shot_first_time',
'shot_one_on_one',
@ -108,7 +133,10 @@ def xgboost_predict_proba(minute=0, position_name='Center Forward', shot_body_pa
'shot_aerial_won',
'shot_open_goal',
'shot_follows_dribble',
'shot_redirect']].astype(bool)
'shot_redirect']].astype(int)
print(X_new)
print(X_new.dtypes)
return model.predict_proba(X_new)[0][1].round(3)

View File

@ -91,7 +91,7 @@ def get_model():
atakujacy_macierz = zmienWMaciez(atakujacy_lista)
if gameMinute == "":
gameMinute = 1 # to change
gameMinute = 1.0 # to change
print("wspolrzedne obroncow: ", obroncy)
print("wspolrzedne atakujacych: ", atakujacy)
@ -124,11 +124,15 @@ def get_model():
posortowani_atakujacy = sort_coordinates_by_distance(atakujacy_macierz, [shooter_x, shooter_y])
print("Posortowani obroncy: ", posortowani_obroncy)
print("Posortowani atakujacy: ", posortowani_atakujacy)
number_of_players_opponents = np.sum(~np.isnan(posortowani_obroncy).all(axis=1))
number_of_players_teammates = np.sum(~np.isnan(posortowani_atakujacy).all(axis=1))
number_of_players_opponents = float(np.sum(~np.isnan(posortowani_obroncy).all(axis=1)))
number_of_players_teammates = float(np.sum(~np.isnan(posortowani_atakujacy).all(axis=1)))
print("Liczba obroncow:", number_of_players_opponents)
print("Liczba atakujacych:", number_of_players_teammates)
## add angle, match minutes and number of players
def loc2angle(x, y):
@ -155,8 +159,42 @@ def get_model():
print("bramkarz:", goalkepper_x, goalkepper_y)
if shot_first_time == 'true':
shot_first_time = True
else:
shot_first_time = False
if shot_one_on_one == 'true':
shot_one_on_one = True
else:
shot_one_on_one = False
if shot_aerial_won == 'true':
shot_aerial_won = True
else:
shot_aerial_won = False
if shot_open_goal == 'true':
shot_open_goal = True
else:
shot_open_goal = False
if shot_follows_dribble == 'true':
shot_follows_dribble = True
else:
shot_follows_dribble = False
if shot_redirect == 'true':
shot_redirect = True
else:
shot_redirect = False
#### CHECKLIST
print('CHECKLISTA')
print('shot_first_time', shot_first_time)
print('shot_one_on_one', shot_one_on_one)
print('shot_aerial_won', shot_aerial_won)
print('shot_open_goal', shot_open_goal)
print('shot_follows_dribble', shot_follows_dribble)
print('shot_redirect', shot_redirect)
# MODEL XGBOOST
response = xgboost_predict_proba(minute=gameMinute,
response = xgboost_predict_proba(minute=int(gameMinute), #minute=0,
position_name=position_name,
shot_body_part_name=body_part,
shot_technique_name=technique,

File diff suppressed because one or more lines are too long

Binary file not shown.

1
notebooks/xgboost.json Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long