Files
predictV1/routes/predict_stacking.py
mamonov.ep 8a134239d7 Initial commit: добавление проекта predictV1
Включает модели ML для предсказаний, API маршруты, скрипты обучения и данные.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-21 17:22:58 +03:00

54 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}