ml-pr / src /streamlit_app.py
sellacio's picture
add files
dc9c904
Raw History Blame Contribute Delete
8.59 kB
import os
import streamlit as st
import pandas as pd
import numpy as np
import joblib
import shap
import matplotlib.pyplot as plt
from sklearn.base import BaseEstimator, TransformerMixin
import xgboost as xgb
# ============================================================================
# 1. КАСТОМНЫЕ КЛАССЫ C ПОДДЕРЖКОЙ АТРИБУТОВ
# ============================================================================
class AirlineFeatureEngineer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.copy()
X["ArrivalDelay_missing"] = X["Arrival Delay in Minutes"].isna().astype(int)
dep = X["Departure Delay in Minutes"].fillna(X["Departure Delay in Minutes"].median())
arr = X["Arrival Delay in Minutes"].fillna(X["Arrival Delay in Minutes"].median())
X["Total Delay Minutes"] = dep + arr
X = X.drop(columns=["Departure Delay in Minutes", "Arrival Delay in Minutes"])
return X
class EarlyStoppingXGBClassifier(xgb.XGBClassifier):
def __init__(self, **kwargs):
# Явно задаём атрибуты, которые могут потребоваться при predict
self.verbosity = kwargs.pop('verbosity', 1)
self.enable_categorical = kwargs.pop('enable_categorical', False)
super().__init__(verbosity=self.verbosity, enable_categorical=self.enable_categorical, **kwargs)
# Необходимо для scikit-learn > 1.6
def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.input_tags.categorical = self.enable_categorical
return tags
def _more_tags(self):
tags = super()._more_tags()
tags["categorical"] = self.enable_categorical
return tags
# ============================================================================
# 2. ЗАГРУЗКА МОДЕЛЕЙ
# ============================================================================
@st.cache_resource
def load_models():
base_dir = os.path.dirname(__file__) # папка src/
xgb_path = os.path.join(base_dir, 'xgb_pipeline.pkl')
knn_path = os.path.join(base_dir, 'knn_pipeline.pkl')
xgb_model = joblib.load(xgb_path)
knn_model = joblib.load(knn_path)
return xgb_model, knn_model
# ============================================================================
# 3. ФУНКЦИЯ ДЛЯ SHAP
# ============================================================================
def get_shap_for_instance(model, input_df_raw):
pre_pipeline = model[:-1] # всё, кроме классификатора
clf = model.named_steps['clf']
X_trans = pre_pipeline.transform(input_df_raw)
pred = clf.predict(X_trans)[0]
proba = clf.predict_proba(X_trans)[0][1]
# SHAP: TreeExplainer для XGBoost, KernelExplainer для KNN
if hasattr(clf, 'get_booster') or isinstance(clf, xgb.XGBClassifier):
explainer = shap.TreeExplainer(clf)
shap_values = explainer.shap_values(X_trans)
if isinstance(shap_values, list):
shap_vals = shap_values[1][0] if len(shap_values) > 1 else shap_values[0][0]
else:
shap_vals = shap_values[0] if len(shap_values.shape) == 3 else shap_values[0]
base_val = explainer.expected_value
if isinstance(base_val, (list, np.ndarray)):
base_val = base_val[1] if len(base_val) > 1 else base_val[0]
else:
# KNN — KernelExplainer (background = первые 100 объектов)
background = X_trans[:min(100, len(X_trans))]
explainer = shap.KernelExplainer(clf.predict_proba, background)
shap_values = explainer.shap_values(X_trans)
shap_vals = shap_values[1][0] if len(shap_values) == 2 else shap_values[0][0]
base_val = explainer.expected_value[1] if isinstance(explainer.expected_value, (list, np.ndarray)) else explainer.expected_value
# Имена признаков
try:
all_feature_names = model.named_steps['preprocessor'].get_feature_names_out()
selector_mask = model.named_steps['selector'].get_support()
feature_names = all_feature_names[selector_mask]
except:
feature_names = [f"feature_{i}" for i in range(X_trans.shape[1])]
return pred, proba, shap_vals, base_val, feature_names, X_trans[0]
# ============================================================================
# 4. ИНТЕРФЕЙС STREAMLIT
# ============================================================================
st.set_page_config(page_title="✈️ Passenger Satisfaction", layout="wide")
st.title("✈️ Airline Passenger Satisfaction Prediction")
st.markdown("Compare **XGBoost** and **K-Nearest Neighbors**.")
with st.spinner("Loading models..."):
xgb_model, knn_model = load_models()
model_choice = st.radio("Select model", ["XGBoost", "KNN"], horizontal=True)
with st.form("input_form"):
col1, col2 = st.columns(2)
with col1:
gender = st.selectbox("Gender", ["Male", "Female"])
customer_type = st.selectbox("Customer Type", ["Loyal Customer", "disloyal Customer"])
age = st.slider("Age", 0, 100, 30)
travel_type = st.selectbox("Type of Travel", ["Personal Travel", "Business travel"])
flight_class = st.selectbox("Class", ["Eco", "Eco Plus", "Business"])
flight_distance = st.number_input("Flight Distance", 0, 5000, 500)
with col2:
inflight_wifi = st.slider("Inflight wifi service", 0, 5, 3)
dep_arr_convenient = st.slider("Departure/Arrival time convenient", 0, 5, 3)
ease_booking = st.slider("Ease of Online booking", 0, 5, 3)
gate_loc = st.slider("Gate location", 0, 5, 3)
food_drink = st.slider("Food and drink", 0, 5, 3)
online_boarding = st.slider("Online boarding", 0, 5, 3)
seat_comfort = st.slider("Seat comfort", 0, 5, 3)
inflight_entertainment = st.slider("Inflight entertainment", 0, 5, 3)
onboard_service = st.slider("On-board service", 0, 5, 3)
leg_room = st.slider("Leg room service", 0, 5, 3)
baggage_handling = st.slider("Baggage handling", 0, 5, 3)
checkin_service = st.slider("Checkin service", 0, 5, 3)
inflight_service = st.slider("Inflight service", 0, 5, 3)
cleanliness = st.slider("Cleanliness", 0, 5, 3)
dep_delay = st.number_input("Departure Delay (min)", 0.0, 500.0, 0.0)
arr_delay = st.number_input("Arrival Delay (min)", 0.0, 500.0, 0.0)
submitted = st.form_submit_button("Predict")
if submitted:
input_data = {
"Gender": gender, "Customer Type": customer_type, "Age": age,
"Type of Travel": travel_type, "Class": flight_class,
"Flight Distance": flight_distance,
"Inflight wifi service": inflight_wifi,
"Departure/Arrival time convenient": dep_arr_convenient,
"Ease of Online booking": ease_booking, "Gate location": gate_loc,
"Food and drink": food_drink, "Online boarding": online_boarding,
"Seat comfort": seat_comfort, "Inflight entertainment": inflight_entertainment,
"On-board service": onboard_service, "Leg room service": leg_room,
"Baggage handling": baggage_handling, "Checkin service": checkin_service,
"Inflight service": inflight_service, "Cleanliness": cleanliness,
"Departure Delay in Minutes": dep_delay, "Arrival Delay in Minutes": arr_delay
}
input_df = pd.DataFrame([input_data])
model = xgb_model if model_choice == "XGBoost" else knn_model
model_name = model_choice
pred, proba, shap_vals, base_val, feat_names, _ = get_shap_for_instance(model, input_df)
st.subheader(f"🔮 Prediction ({model_name})")
st.success(f"✅ Satisfied (prob: {proba:.2f})" if pred == 1 else f"❌ Not satisfied (prob: {1-proba:.2f})")
st.subheader("📊 SHAP Explanation")
exp = shap.Explanation(values=shap_vals, base_values=base_val, data=np.zeros(len(feat_names)), feature_names=feat_names)
fig, ax = plt.subplots(figsize=(10, 6))
shap.plots.waterfall(exp, max_display=12, show=False)
plt.tight_layout()
st.pyplot(fig)
plt.close()
with st.expander("Compare with other model"):
other = knn_model if model_name == "XGBoost" else xgb_model
other_pred = other.predict(input_df)[0]
other_proba = other.predict_proba(input_df)[0][1]
st.write(f"**{other_name}** predicts: {'Satisfied' if other_pred==1 else 'Not satisfied'} (prob: {other_proba:.2f})")