Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- README (1).md +51 -0
- app.py +103 -0
- model (1).pkl +3 -0
- model_columns (1).pkl +3 -0
- preprocessor (1).pkl +3 -0
- requirements.txt +6 -0
README (1).md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: UFC KO/TKO Predictor
|
| 3 |
+
emoji: 馃
|
| 4 |
+
colorFrom: 'red'
|
| 5 |
+
colorTo: 'blue'
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "4.3.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# 馃 Predictor de Finalizaci贸n UFC (KO/TKO)
|
| 14 |
+
|
| 15 |
+
Aplicaci贸n de Machine Learning que predice la **probabilidad de que un combate termine por Knockout/Technical Knockout (KO/TKO)**, bas谩ndose en el an谩lisis de estad铆sticas de los peleadores y el contexto de la pelea.
|
| 16 |
+
|
| 17 |
+
## 馃幆 Objetivo y Modelo
|
| 18 |
+
|
| 19 |
+
El modelo de clasificaci贸n fue entrenado para responder a la pregunta binaria: **驴El combate finaliza por KO/TKO (1) o Decisi贸n/Sumisi贸n (0)?**
|
| 20 |
+
|
| 21 |
+
* **Algoritmo Principal:** **Random Forest Classifier** (Seleccionado tras una evaluaci贸n comparativa).
|
| 22 |
+
* **M茅trica Clave:** **AUC Score** (脕rea bajo la curva ROC), priorizando la capacidad de distinci贸n de clases.
|
| 23 |
+
* **Pipeline de Preprocesamiento:** Incluye Imputaci贸n de nulos, `StandardScaler` y `OneHotEncoder` para la ubicaci贸n (`Location`).
|
| 24 |
+
|
| 25 |
+
[Image of Heatmap of a Correlation Matrix]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
## 馃搳 Caracter铆sticas Clave Analizadas
|
| 29 |
+
|
| 30 |
+
El modelo utiliza caracter铆sticas de diferencia y estad铆sticas brutas:
|
| 31 |
+
|
| 32 |
+
* **Diferencias:** Diferencia de Knockdowns (`KD_diff`), Golpes Significativos (`STR_diff`), Derribos (`TD_diff`) y Sumisiones.
|
| 33 |
+
* **Estad铆sticas Brutas:** `Round` actual, Precisi贸n de golpeo de ambos peleadores.
|
| 34 |
+
* **Contexto:** Ubicaci贸n del evento (`Location`) y Categor铆a de Peso (OHE).
|
| 35 |
+
|
| 36 |
+
## 鈿欙笍 Arquitectura T茅cnica
|
| 37 |
+
|
| 38 |
+
El servicio se despliega usando el ciclo de vida completo de ML:
|
| 39 |
+
|
| 40 |
+
1. **Entorno:** Google Colab / Scikit-learn.
|
| 41 |
+
2. **Serializaci贸n:** `model.pkl` y `preprocessor.pkl`.
|
| 42 |
+
3. **Despliegue:** **Gradio** en un **Hugging Face Space**.
|
| 43 |
+
|
| 44 |
+
## 馃捇 Uso de la Interfaz
|
| 45 |
+
|
| 46 |
+
1. **Ingresa las estad铆sticas** brutas y de diferencia de los peleadores.
|
| 47 |
+
2. **Aseg煤rate de que la Ubicaci贸n** sea correcta (usado para codificaci贸n).
|
| 48 |
+
3. El modelo devuelve una **Probabilidad de KO/TKO (%)** y el **Resultado M谩s Probable**.
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
*Este proyecto fue desarrollado con fines educativos y de demostraci贸n para el curso de An谩lisis de Datos.*
|
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 8 |
+
from sklearn.impute import SimpleImputer
|
| 9 |
+
from sklearn.compose import ColumnTransformer
|
| 10 |
+
from sklearn.pipeline import Pipeline
|
| 11 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 12 |
+
|
| 13 |
+
# --- CORRECCI脫N: Listas hardcodeadas para la interfaz ---
|
| 14 |
+
UFC_LOCATIONS = [
|
| 15 |
+
'Las Vegas, NV', 'Rio de Janeiro, Brazil', 'Abu Dhabi, UAE',
|
| 16 |
+
'London, England', 'New York, NY', 'Otros'
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
# --- 1. Cargar objetos serializados ---
|
| 20 |
+
model = None
|
| 21 |
+
preprocessor = None
|
| 22 |
+
model_columns = []
|
| 23 |
+
try:
|
| 24 |
+
model = joblib.load(os.path.join(os.path.dirname(__file__), 'model.pkl'))
|
| 25 |
+
preprocessor = joblib.load(os.path.join(os.path.dirname(__file__), 'preprocessor.pkl'))
|
| 26 |
+
model_columns = joblib.load(os.path.join(os.path.dirname(__file__), 'model_columns.pkl'))
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Error al cargar artefactos: {e}")
|
| 29 |
+
|
| 30 |
+
# --- 2. Funci贸n de Predicci贸n (Ganador) ---
|
| 31 |
+
# Los argumentos de entrada son las estad铆sticas brutas.
|
| 32 |
+
def predict_winner(F1_KD, F2_KD, F1_STR, F2_STR, F1_TD, F2_TD, F1_SUB, F2_SUB, Round,
|
| 33 |
+
F1_acc, F2_acc, KD_diff, STR_diff, TD_diff, SUB_diff, Location,
|
| 34 |
+
wc_B, wc_C, wc_F, wc_Fl, wc_H, wc_LH, wc_L, wc_M, wc_O, wc_SH, wc_W,
|
| 35 |
+
wc_WB, wc_WF, wc_WFl, wc_WS):
|
| 36 |
+
|
| 37 |
+
if model is None or preprocessor is None:
|
| 38 |
+
return "ERROR", "Fallo al cargar modelo. Revisa el log de versiones de Scikit-learn."
|
| 39 |
+
|
| 40 |
+
# 1. Crear el DataFrame de entrada
|
| 41 |
+
input_data = pd.DataFrame({
|
| 42 |
+
'Fighter_1_KD': [F1_KD], 'Fighter_2_KD': [F2_KD], 'Fighter_1_STR': [F1_STR], 'Fighter_2_STR': [F2_STR],
|
| 43 |
+
'Fighter_1_TD': [F1_TD], 'Fighter_2_TD': [F2_TD], 'Fighter_1_SUB': [F1_SUB], 'Fighter_2_SUB': [F2_SUB],
|
| 44 |
+
'Round': [Round], 'Fighter_1_accuracy': [F1_acc], 'Fighter_2_accuracy': [F2_acc],
|
| 45 |
+
'KD_diff': [KD_diff], 'STR_diff': [STR_diff], 'TD_diff': [TD_diff], 'SUB_diff': [SUB_diff],
|
| 46 |
+
'Location': [Location],
|
| 47 |
+
'weight_class_Bantamweight': [wc_B], 'weight_class_Catch Weight': [wc_C], 'weight_class_Featherweight': [wc_F],
|
| 48 |
+
'weight_class_Flyweight': [wc_Fl], 'weight_class_Heavyweight': [wc_H], 'weight_class_Light Heavyweight': [wc_LH],
|
| 49 |
+
'weight_class_Lightweight': [wc_L], 'weight_class_Middleweight': [wc_M], 'weight_class_Open Weight': [wc_O],
|
| 50 |
+
'weight_class_Super Heavyweight': [wc_SH], 'weight_class_Welterweight': [wc_W],
|
| 51 |
+
"weight_class_Women's Bantamweight": [wc_WB], "weight_class_Women's Featherweight": [wc_WF],
|
| 52 |
+
"weight_class_Women's Flyweight": [wc_WFl], "weight_class_Women's Strawweight": [wc_WS]
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
# 2. Preprocesamiento: Utilizar el ColumnTransformer ajustado.
|
| 56 |
+
X_processed = preprocessor.transform(input_data)
|
| 57 |
+
X_final = pd.DataFrame(X_processed, columns=model_columns)
|
| 58 |
+
|
| 59 |
+
# 3. Predicci贸n
|
| 60 |
+
# P(F1 Gana) = P(Clase 1)
|
| 61 |
+
proba_f1_wins = model.predict_proba(X_final)[0][1]
|
| 62 |
+
|
| 63 |
+
# 4. Formato de Salida
|
| 64 |
+
if proba_f1_wins >= 0.50:
|
| 65 |
+
winner = "PELEADOR 1 (Predicci贸n)"
|
| 66 |
+
confidence_percent = f"{proba_f1_wins*100:.2f}%"
|
| 67 |
+
else:
|
| 68 |
+
winner = "PELEADOR 2 (Predicci贸n)"
|
| 69 |
+
# La confianza es 1 - P(F1 Gana)
|
| 70 |
+
confidence_percent = f"{(1 - proba_f1_wins)*100:.2f}%"
|
| 71 |
+
|
| 72 |
+
return winner, confidence_percent
|
| 73 |
+
|
| 74 |
+
# --- 3. Creaci贸n de la Interfaz Gradio ---
|
| 75 |
+
inputs = [
|
| 76 |
+
gr.Slider(0, 5, value=1, step=1, label="KD P1"), gr.Slider(0, 5, value=0, step=1, label="KD P2"),
|
| 77 |
+
gr.Slider(0, 300, value=70, label="STR P1"), gr.Slider(0, 300, value=50, label="STR P2"),
|
| 78 |
+
gr.Slider(0, 20, value=5, label="TD P1"), gr.Slider(0, 20, value=2, label="TD P2"),
|
| 79 |
+
gr.Slider(0, 5, value=0, step=1, label="SUB P1"), gr.Slider(0, 5, value=0, step=1, label="SUB P2"),
|
| 80 |
+
gr.Slider(1, 5, value=3, step=1, label="Ronda actual (Round)"),
|
| 81 |
+
gr.Slider(0, 1, value=0.4, label="Precisi贸n STR P1 (F1_acc)"), gr.Slider(0, 1, value=0.3, label="Precisi贸n STR P2 (F2_acc)"),
|
| 82 |
+
gr.Slider(-5, 5, value=1, label="Diferencia de KD"), gr.Slider(-300, 300, value=20, label="Diferencia de STR"),
|
| 83 |
+
gr.Slider(-20, 20, value=3, label="Diferencia de TD"), gr.Slider(-5, 5, value=0, label="Diferencia de SUB"),
|
| 84 |
+
gr.Dropdown(UFC_LOCATIONS, value='Las Vegas, NV', label="Ubicaci贸n"),
|
| 85 |
+
gr.Checkbox(value=True, label="weight_class_Lightweight (wc_L)"), gr.Checkbox(value=False, label="weight_class_Bantamweight (wc_B)"),
|
| 86 |
+
gr.Checkbox(value=False, label="weight_class_Catch Weight (wc_C)"), gr.Checkbox(value=False, label="weight_class_Featherweight (wc_F)"),
|
| 87 |
+
gr.Checkbox(value=False, label="weight_class_Flyweight (wc_Fl)"), gr.Checkbox(value=False, label="weight_class_Heavyweight (wc_H)"),
|
| 88 |
+
gr.Checkbox(value=False, label="weight_class_Light Heavyweight (wc_LH)"), gr.Checkbox(value=False, label="weight_class_Middleweight (wc_M)"),
|
| 89 |
+
gr.Checkbox(value=False, label="weight_class_Open Weight (wc_O)"), gr.Checkbox(value=False, label="weight_class_Super Heavyweight (wc_SH)"),
|
| 90 |
+
gr.Checkbox(value=False, label="weight_class_Welterweight (wc_W)"), gr.Checkbox(value=False, label="weight_class_Women's Bantamweight (wc_WB)"),
|
| 91 |
+
gr.Checkbox(value=False, label="weight_class_Women's Featherweight (wc_WF)"), gr.Checkbox(value=False, label="weight_class_Women's Flyweight (wc_WFl)"),
|
| 92 |
+
gr.Checkbox(value=False, label="weight_class_Women's Strawweight (wc_WS)")
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
outputs = [gr.Textbox(label="Ganador Predicho"), gr.Textbox(label="Confianza (%)")]
|
| 96 |
+
|
| 97 |
+
gr.Interface(
|
| 98 |
+
fn=predict_winner,
|
| 99 |
+
inputs=inputs,
|
| 100 |
+
outputs=outputs,
|
| 101 |
+
title="馃 Predictor del Ganador de Combates UFC",
|
| 102 |
+
description="Modelo Random Forest para predecir si el Peleador 1 o el Peleador 2 ganar谩."
|
| 103 |
+
).launch(server_name="0.0.0.0", server_port=7860)
|
model (1).pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b7e9ac0a2615424608c379be33a5faccea2c24cb8d5c3447d1578396ff0ae50
|
| 3 |
+
size 2319337
|
model_columns (1).pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:448bedf417d5e97e35963c15fae216ef0f3dede72f902fd063ea9d272638b96c
|
| 3 |
+
size 8071
|
preprocessor (1).pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:03b1023f6b3d90cb7fd3758d1c412da46f61b451ae0243f63a5a2402ef5512bc
|
| 3 |
+
size 10879
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# requirements.txt
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn==1.3.2
|
| 5 |
+
gradio==4.3.0
|
| 6 |
+
joblib
|