File size: 3,567 Bytes
e012bff
cda7913
e012bff
790b130
 
 
e012bff
cda7913
 
e012bff
 
 
cda7913
 
 
 
 
e012bff
 
cda7913
 
 
 
e012bff
cda7913
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e012bff
cda7913
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e012bff
cda7913
 
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
import os

import joblib
import pandas as pd
import streamlit as st


st.set_page_config(page_title="General Classification Lab", page_icon="🚀", layout="wide")
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


@st.cache_resource
def load_models():
    return (
        joblib.load(os.path.join(ROOT, "spaceship_bundle.joblib")),
        joblib.load(os.path.join(ROOT, "iris_bundle.joblib")),
    )


spaceship, iris = load_models()
st.title("🚀 General Classification AI Lab")
st.caption("Two classic classification problems rebuilt as interactive, tested applications.")
spaceship_tab, iris_tab = st.tabs(["Spaceship Titanic", "Iris Flower"])

with spaceship_tab:
    st.subheader("Will a passenger be transported to another dimension?")
    a, b, c = st.columns(3)
    home = a.selectbox("Home planet", ["Earth", "Europa", "Mars"])
    destination = a.selectbox("Destination", ["TRAPPIST-1e", "55 Cancri e", "PSO J318.5-22"])
    age = a.slider("Age", 0, 90, 30)
    cryo = a.checkbox("CryoSleep")
    vip = a.checkbox("VIP passenger")
    group_size = b.slider("Travel group size", 1, 8, 1)
    deck = b.selectbox("Cabin deck", list("ABCDEFGT"), index=5)
    side = b.selectbox("Cabin side", ["P", "S"], format_func=lambda x: "Port" if x == "P" else "Starboard")
    room = b.number_input("Room service spend", 0.0, 30000.0, 0.0, 50.0)
    food = b.number_input("Food court spend", 0.0, 30000.0, 500.0, 50.0)
    shopping = c.number_input("Shopping mall spend", 0.0, 30000.0, 0.0, 50.0)
    spa = c.number_input("Spa spend", 0.0, 30000.0, 0.0, 50.0)
    vr = c.number_input("VR deck spend", 0.0, 30000.0, 0.0, 50.0)
    if st.button("Predict transportation", type="primary"):
        values = {
            "HomePlanet": home, "CryoSleep": cryo, "Destination": destination, "Age": age, "VIP": vip,
            "RoomService": room, "FoodCourt": food, "ShoppingMall": shopping, "Spa": spa, "VRDeck": vr,
            "GroupSize": group_size, "Deck": deck, "CabinSide": side,
            "TotalSpend": room + food + shopping + spa + vr,
        }
        row = pd.DataFrame([values], columns=spaceship["features"])
        probability = float(spaceship["model"].predict_proba(row)[0, 1])
        st.metric("Transportation probability", f"{probability:.1%}")
        st.success("Likely transported") if probability >= .5 else st.warning("Likely not transported")
        st.caption("Validation accuracy: 80.16% on held-out Spaceship Titanic competition passengers.")

with iris_tab:
    st.subheader("Classify an Iris flower from four measurements")
    a, b = st.columns(2)
    sepal_length = a.slider("Sepal length (cm)", 4.0, 8.0, 5.8, .1)
    sepal_width = a.slider("Sepal width (cm)", 2.0, 5.0, 3.0, .1)
    petal_length = b.slider("Petal length (cm)", 1.0, 7.0, 4.3, .1)
    petal_width = b.slider("Petal width (cm)", .1, 2.5, 1.3, .1)
    if st.button("Classify flower", type="primary"):
        row = pd.DataFrame([[sepal_length, sepal_width, petal_length, petal_width]], columns=iris["features"])
        probabilities = iris["model"].predict_proba(row)[0]
        prediction = int(probabilities.argmax())
        st.success(f"Predicted species: **Iris {iris['target_names'][prediction].title()}**")
        chart = pd.DataFrame({"Species": [x.title() for x in iris["target_names"]], "Probability": probabilities}).set_index("Species")
        st.bar_chart(chart)
        st.caption("Validation accuracy: 96.67% on held-out Iris samples.")

st.divider()
st.caption("Portfolio of Jale Summak · Educational classification applications")