Dlasza praca nad logiką obliczeniową

This commit is contained in:
LukaszChrostowski 2024-01-06 14:52:25 +01:00
parent 6cb9ffc781
commit 0b5e268118
5 changed files with 142 additions and 7 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -3,9 +3,9 @@ import pandas as pd
from math import sqrt from math import sqrt
# Funkcja zwraca prawdopodobieństwo zdobycia gola # Funkcja zwraca prawdopodobieństwo zdobycia gola
def LogisticRegression_predict_proba(position_x, position_y, angle, match_minute, Number_Intervening_Opponents, Number_Intervening_Teammates, isFoot, isHead): def LogisticRegression_predict_proba(position_x, position_y, distance_to_goalM, angle, match_minute, Number_Intervening_Opponents, Number_Intervening_Teammates, isFoot, isHead):
distance_to_goalM = sqrt(( (position_x**2) + (position_y**2))) # distance_to_goalM = sqrt(( (position_x**2) + (position_y**2)))
model = load('regresja_logistyczna.joblib') model = load('regresja_logistyczna.joblib')
X_new = pd.DataFrame(columns=['position_x', 'position_y', 'distance_to_goalM', 'angle','match_minute', 'Number_Intervening_Opponents','Number_Intervening_Teammates', 'isFoot', 'isHead']) X_new = pd.DataFrame(columns=['position_x', 'position_y', 'distance_to_goalM', 'angle','match_minute', 'Number_Intervening_Opponents','Number_Intervening_Teammates', 'isFoot', 'isHead'])
@ -14,7 +14,79 @@ def LogisticRegression_predict_proba(position_x, position_y, angle, match_minute
#kolejne modele #kolejne modele
def xgboost_predict_proba(): def xgboost_predict_proba(minute, position_name, shot_body_part_name, shot_technique_name,
shot_type_name, shot_first_time, shot_one_on_one,
shot_aerial_won, shot_deflected, shot_open_goal,
shot_follows_dribble, shot_redirect, x1, y1,
number_of_players_opponents, number_of_players_teammates,
angle, distance, x_player_opponent_Goalkeeper,
x_player_opponent_8, x_player_opponent_1, x_player_opponent_2,
x_player_opponent_3, x_player_teammate_1, x_player_opponent_4,
x_player_opponent_5, x_player_opponent_6, x_player_teammate_2,
x_player_opponent_9, x_player_opponent_10, x_player_opponent_11,
x_player_teammate_3, x_player_teammate_4, x_player_teammate_5,
x_player_teammate_6, x_player_teammate_7, x_player_teammate_8,
x_player_teammate_9, x_player_teammate_10,
y_player_opponent_Goalkeeper, y_player_opponent_8,
y_player_opponent_1, y_player_opponent_2, y_player_opponent_3,
y_player_teammate_1, y_player_opponent_4, y_player_opponent_5,
y_player_opponent_6, y_player_teammate_2, y_player_opponent_9,
y_player_opponent_10, y_player_opponent_11, y_player_teammate_3,
y_player_teammate_4, y_player_teammate_5, y_player_teammate_6,
y_player_teammate_7, y_player_teammate_8, y_player_teammate_9,
y_player_teammate_10, x_player_opponent_7, y_player_opponent_7,
x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper,
shot_kick_off):
model = load('xgboost.joblib') model = load('xgboost.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',
'shot_aerial_won', 'shot_deflected', 'shot_open_goal',
'shot_follows_dribble', 'shot_redirect', 'x1', 'y1',
'number_of_players_opponents', 'number_of_players_teammates',
'angle', 'distance', 'x_player_opponent_Goalkeeper',
'x_player_opponent_8', 'x_player_opponent_1', 'x_player_opponent_2',
'x_player_opponent_3', 'x_player_teammate_1', 'x_player_opponent_4',
'x_player_opponent_5', 'x_player_opponent_6', 'x_player_teammate_2',
'x_player_opponent_9', 'x_player_opponent_10', 'x_player_opponent_11',
'x_player_teammate_3', 'x_player_teammate_4', 'x_player_teammate_5',
'x_player_teammate_6', 'x_player_teammate_7', 'x_player_teammate_8',
'x_player_teammate_9', 'x_player_teammate_10',
'y_player_opponent_Goalkeeper', 'y_player_opponent_8',
'y_player_opponent_1', 'y_player_opponent_2', 'y_player_opponent_3',
'y_player_teammate_1', 'y_player_opponent_4', 'y_player_opponent_5',
'y_player_opponent_6', 'y_player_teammate_2', 'y_player_opponent_9',
'y_player_opponent_10', 'y_player_opponent_11', 'y_player_teammate_3',
'y_player_teammate_4', 'y_player_teammate_5', 'y_player_teammate_6',
'y_player_teammate_7', 'y_player_teammate_8', 'y_player_teammate_9',
'y_player_teammate_10', 'x_player_opponent_7', 'y_player_opponent_7',
'x_player_teammate_Goalkeeper', 'y_player_teammate_Goalkeeper',
'shot_kick_off'])
X_new.loc[len(X_new.index)] = [minute, position_name, shot_body_part_name, shot_technique_name,
shot_type_name, shot_first_time, shot_one_on_one,
shot_aerial_won, shot_deflected, shot_open_goal,
shot_follows_dribble, shot_redirect, x1, y1,
number_of_players_opponents, number_of_players_teammates,
angle, distance, x_player_opponent_Goalkeeper,
x_player_opponent_8, x_player_opponent_1, x_player_opponent_2,
x_player_opponent_3, x_player_teammate_1, x_player_opponent_4,
x_player_opponent_5, x_player_opponent_6, x_player_teammate_2,
x_player_opponent_9, x_player_opponent_10, x_player_opponent_11,
x_player_teammate_3, x_player_teammate_4, x_player_teammate_5,
x_player_teammate_6, x_player_teammate_7, x_player_teammate_8,
x_player_teammate_9, x_player_teammate_10,
y_player_opponent_Goalkeeper, y_player_opponent_8,
y_player_opponent_1, y_player_opponent_2, y_player_opponent_3,
y_player_teammate_1, y_player_opponent_4, y_player_opponent_5,
y_player_opponent_6, y_player_teammate_2, y_player_opponent_9,
y_player_opponent_10, y_player_opponent_11, y_player_teammate_3,
y_player_teammate_4, y_player_teammate_5, y_player_teammate_6,
y_player_teammate_7, y_player_teammate_8, y_player_teammate_9,
y_player_teammate_10, x_player_opponent_7, y_player_opponent_7,
x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper,
shot_kick_off]
return model.predict_proba(X_new)[0][1].round(2)

View File

@ -1,12 +1,26 @@
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from flask_cors import CORS from flask_cors import CORS
from modele.modele import LogisticRegression_predict_proba from modele.modele import LogisticRegression_predict_proba, xgboost_predict_proba
import math
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type' app.config['CORS_HEADERS'] = 'Content-Type'
def loc2angle(x, y):
rads = math.atan(7.32 * x / (x**2 + (y - 34)**2 - (7.32/2)**2))
rads = math.pi + rads if rads < 0 else rads
deg = math.degrees(rads)
return deg
def loc2distance(x, y):
return math.sqrt(x**2 + (y - 34)**2)
def loc2locdistance(x1, y1, x2, y2):
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
# model Api # model Api
# @app.route("/members") # @app.route("/members")
# def members(): # def members():
@ -16,6 +30,51 @@ app.config['CORS_HEADERS'] = 'Content-Type'
# @app.route("/LRegresion<x>&<y>") # @app.route("/LRegresion<x>&<y>")
@app.route("/get_model", methods = ['GET']) @app.route("/get_model", methods = ['GET'])
# def get_model():
# # x = int(x[0:2])
# # y = int(y[0:2])
# x = request.args.get('x', type=float)
# y = request.args.get('y', type=float)
# ## change model on xgboost
# ## add angle, match minutes and number of players
# angle = loc2angle(x = x, y = y)
# dist = loc2distance(x = x, y = y)
# if y is None and x is None:
# return jsonify({"error": "Brak wymaganych parametrów"}), 400
# response = xgboost_predict_proba(minute = 20, position_name, shot_body_part_name, shot_technique_name,
# shot_type_name, shot_first_time, shot_one_on_one,
# shot_aerial_won, shot_deflected, shot_open_goal,
# shot_follows_dribble, shot_redirect, x1 = x, y1 = y,
# number_of_players_opponents, number_of_players_teammates,
# angle = angle, distance = dist, x_player_opponent_Goalkeeper,
# x_player_opponent_8, x_player_opponent_1, x_player_opponent_2,
# x_player_opponent_3, x_player_teammate_1, x_player_opponent_4,
# x_player_opponent_5, x_player_opponent_6, x_player_teammate_2,
# x_player_opponent_9, x_player_opponent_10, x_player_opponent_11,
# x_player_teammate_3, x_player_teammate_4, x_player_teammate_5,
# x_player_teammate_6, x_player_teammate_7, x_player_teammate_8,
# x_player_teammate_9, x_player_teammate_10,
# y_player_opponent_Goalkeeper, y_player_opponent_8,
# y_player_opponent_1, y_player_opponent_2, y_player_opponent_3,
# y_player_teammate_1, y_player_opponent_4, y_player_opponent_5,
# y_player_opponent_6, y_player_teammate_2, y_player_opponent_9,
# y_player_opponent_10, y_player_opponent_11, y_player_teammate_3,
# y_player_teammate_4, y_player_teammate_5, y_player_teammate_6,
# y_player_teammate_7, y_player_teammate_8, y_player_teammate_9,
# y_player_teammate_10, x_player_opponent_7, y_player_opponent_7,
# x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper,
# shot_kick_off)
# #print(x)
# #print(y)
# #print(response)
# res = str(response)
# #return {"response":res}
# return jsonify({"response":res})
def get_model(): def get_model():
# x = int(x[0:2]) # x = int(x[0:2])
@ -25,12 +84,16 @@ def get_model():
## change model on xgboost ## change model on xgboost
## add angle, match minutes and number of players ## add angle, match minutes and number of players
angle = loc2angle(x = x, y = y)
dist = loc2distance(x = x, y = y)
if y is None and x is None: if y is None and x is None:
return jsonify({"error": "Brak wymaganych parametrów"}), 400 return jsonify({"error": "Brak wymaganych parametrów"}), 400
response = LogisticRegression_predict_proba(position_x=x, response = LogisticRegression_predict_proba(position_x=x,
position_y=y, position_y=y,
angle = 13.67, distance_to_goalM = dist,
angle = angle,
match_minute=13, match_minute=13,
Number_Intervening_Opponents=3, Number_Intervening_Opponents=3,
Number_Intervening_Teammates=0, Number_Intervening_Teammates=0,

View File

@ -114,7 +114,7 @@ loc2angle <- function(x, y) {
# distance to goal # distance to goal
loc2distance <- function(x, y) { loc2distance <- function(x, y) {
sqrt(x^2 + y^2) sqrt(x^2 + (y - 34)^2)
} }
# distance between two points on the pitch # distance between two points on the pitch
@ -345,5 +345,5 @@ cols <- names(data3_final)[grepl(pattern, names(data3_final))]
data_final <- data3_final %>% unnest(all_of(cols)) data_final <- data3_final %>% unnest(all_of(cols))
skimr::skim(data_final) skimr::skim(data_final)
write_csv(data_final, file = "data/final_data.csv") write_csv(data_final, file = "data/final_data.csv")
# df_test <- read.csv("data/final_data.csv", nrows = 100) df_test <- read.csv("data/final_data.csv", nrows = 1000)
##################### The fourth dataset ############################## ##################### The fourth dataset ##############################