File size: 8,590 Bytes
8040f2d
3422720
9915298
 
 
 
 
e5a85fc
 
 
 
dc9c904
e5a85fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc9c904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5a85fc
 
dc9c904
e5a85fc
3422720
9915298
 
dc9c904
da717f5
 
e5a85fc
 
 
 
 
dc9c904
e5a85fc
3422720
e5a85fc
dc9c904
9915298
e5a85fc
 
 
 
 
dc9c904
e5a85fc
9915298
 
e5a85fc
 
9915298
e5a85fc
9915298
 
e5a85fc
9915298
dc9c904
 
9915298
e5a85fc
 
 
 
dc9c904
e5a85fc
 
 
 
 
 
9915298
e5a85fc
 
 
dc9c904
e5a85fc
 
 
 
dc9c904
e5a85fc
dc9c904
e5a85fc
 
dc9c904
3422720
9915298
 
 
 
 
 
e5a85fc
9915298
dc9c904
9915298
 
e5a85fc
 
 
9915298
 
 
 
 
 
 
 
 
 
dc9c904
 
9915298
3422720
9915298
e5a85fc
dc9c904
 
e5a85fc
 
 
dc9c904
 
 
 
 
 
 
9915298
e5a85fc
dc9c904
 
9915298
dc9c904
9915298
dc9c904
 
9915298
dc9c904
 
e5a85fc
9915298
e5a85fc
9915298
 
 
dc9c904
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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})")