Initial commit: добавление проекта predictV1
Включает модели ML для предсказаний, API маршруты, скрипты обучения и данные. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
53
routes/predict_stacking.py
Normal file
53
routes/predict_stacking.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import pickle
|
||||
from typing import Dict, Any
|
||||
from routes.predict import build_long_format_input, modelPro
|
||||
from routes.predict_bag_of_heroes import build_bag_of_heroes_features, modelBagOfHeroes
|
||||
from routes.predict_with_players import build_player_features, modelWithPlayers
|
||||
|
||||
# Загрузка мета-модели (Logistic Regression)
|
||||
with open("artifacts/model_stacking.pkl", 'rb') as f:
|
||||
modelStacking = pickle.load(f)
|
||||
|
||||
def predict_stacking(payload: Dict[str, Any]) -> Dict[str, float]:
|
||||
"""
|
||||
Делает предсказание с использованием стекинг-модели.
|
||||
|
||||
Сначала получает предсказания от всех базовых моделей,
|
||||
затем использует их как признаки для мета-модели.
|
||||
|
||||
Возвращает:
|
||||
{
|
||||
"radiant_win": вероятность победы Radiant (0-100),
|
||||
"dire_win": вероятность победы Dire (0-100)
|
||||
}
|
||||
"""
|
||||
# === Предсказание модели 1: Heroes + Positions ===
|
||||
X_with_pos = build_long_format_input(payload)
|
||||
pred1 = float(modelPro.predict_proba(X_with_pos)[0, 1])
|
||||
|
||||
# === Предсказание модели 2: Bag of Heroes ===
|
||||
X_bag = build_bag_of_heroes_features(payload)
|
||||
pred2 = float(modelBagOfHeroes.predict_proba(X_bag)[0, 1])
|
||||
|
||||
# === Предсказание модели 3: With Players ===
|
||||
X_players = build_player_features(payload)
|
||||
pred3 = float(modelWithPlayers.predict_proba(X_players)[0, 1])
|
||||
|
||||
# === Мета-модель ===
|
||||
X_meta = pd.DataFrame([{
|
||||
"pred_with_positions": pred1,
|
||||
"pred_bag_of_heroes": pred2,
|
||||
"pred_with_players": pred3
|
||||
}])
|
||||
|
||||
proba = modelStacking.predict_proba(X_meta)[0, 1]
|
||||
|
||||
radiant_win = round(float(np.clip(proba * 100.0, 0.0, 100.0)))
|
||||
dire_win = 100.0 - radiant_win
|
||||
|
||||
return {
|
||||
"radiant_win": radiant_win,
|
||||
"dire_win": dire_win
|
||||
}
|
||||
Reference in New Issue
Block a user