Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py β Titanic Data Explorer β
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
@@ -7,9 +7,8 @@ import plotly.express as px
|
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
from sklearn.model_selection import train_test_split
|
| 9 |
from sklearn.preprocessing import LabelEncoder
|
| 10 |
-
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
|
| 11 |
from sklearn.ensemble import RandomForestClassifier
|
| 12 |
-
from sklearn.linear_model import LogisticRegression
|
| 13 |
|
| 14 |
# =======================
|
| 15 |
# DATA
|
|
@@ -20,13 +19,11 @@ def load_data(path="Titanic-Dataset.csv"):
|
|
| 20 |
df = pd.read_csv(path)
|
| 21 |
df.columns = [c.lower().strip() for c in df.columns]
|
| 22 |
|
| 23 |
-
# kolommen check
|
| 24 |
req = {"survived", "pclass", "sex", "age", "sibsp", "parch", "fare", "embarked"}
|
| 25 |
miss = req - set(df.columns)
|
| 26 |
if miss:
|
| 27 |
raise ValueError(f"Ontbrekende kolommen: {miss}")
|
| 28 |
|
| 29 |
-
# missende waarden vullen
|
| 30 |
for col in df.columns:
|
| 31 |
if df[col].isna().any():
|
| 32 |
if df[col].dtype == "object":
|
|
@@ -73,13 +70,15 @@ def plot_survival_heatmap(x):
|
|
| 73 |
return make_plot(f, "Overlevingspercentage per Geslacht en Klasse")
|
| 74 |
|
| 75 |
def plot_density_age_fare(x):
|
| 76 |
-
f = px.density_contour(x, x="age", y="fare", color="status",
|
|
|
|
| 77 |
return make_plot(f, "Leeftijd vs Ticketprijs (dichtheidsverdeling)")
|
| 78 |
|
| 79 |
def plot_bubble_family_fare(x):
|
| 80 |
f = px.scatter(
|
| 81 |
x, x="fare", y="family_size", size="age", color="status",
|
| 82 |
-
hover_data=["sex", "pclass"], size_max=40,
|
|
|
|
| 83 |
)
|
| 84 |
return make_plot(f, "Bubble Chart β Fare vs Family Size vs Age")
|
| 85 |
|
|
@@ -123,7 +122,7 @@ def train_and_evaluate(x):
|
|
| 123 |
return f"π― **Nauwkeurigheid:** {acc:.2%} | **ROC AUC:** {auc:.3f}", fig_cm
|
| 124 |
|
| 125 |
# =======================
|
| 126 |
-
#
|
| 127 |
# =======================
|
| 128 |
def dashboard():
|
| 129 |
acc_text, cm_fig = train_and_evaluate(df)
|
|
@@ -152,7 +151,7 @@ body {
|
|
| 152 |
color: #EAF2FF;
|
| 153 |
}
|
| 154 |
.gradio-container {
|
| 155 |
-
background: rgba(10, 16, 26, 0.
|
| 156 |
}
|
| 157 |
.gradio-container::before {
|
| 158 |
content: "";
|
|
@@ -166,8 +165,18 @@ body {
|
|
| 166 |
.kpi .value {font-size:1.6rem; font-weight:800; color:#FFD26A;}
|
| 167 |
.kpi .label {font-size:0.9rem; color:#C4D7F0;}
|
| 168 |
.section-title {font-size:1.3rem; font-weight:800; color:#FFD26A; margin-top:12px;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
"""
|
| 170 |
|
|
|
|
|
|
|
|
|
|
| 171 |
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="blue")) as demo:
|
| 172 |
gr.HTML("<h1 style='text-align:center;margin-top:10px;'>π³οΈ Titanic Data Explorer β Night Sky Edition</h1>")
|
| 173 |
gr.HTML("<p style='text-align:center;color:#C4D7F0;'>Interactieve visualisatie & machine learning analyse</p>")
|
|
@@ -196,7 +205,8 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", secondar
|
|
| 196 |
fig_cm = gr.Plot(label="Confusion Matrix")
|
| 197 |
|
| 198 |
gr.HTML("<div class='section-title'>ποΈ Data voorbeeld</div>")
|
| 199 |
-
|
|
|
|
| 200 |
|
| 201 |
def update_dashboard():
|
| 202 |
return dashboard()
|
|
|
|
| 1 |
+
# app.py β Titanic Data Explorer β Night Sky Edition (Gradio 4.44 compatibel)
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
|
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
from sklearn.model_selection import train_test_split
|
| 9 |
from sklearn.preprocessing import LabelEncoder
|
| 10 |
+
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
|
| 11 |
from sklearn.ensemble import RandomForestClassifier
|
|
|
|
| 12 |
|
| 13 |
# =======================
|
| 14 |
# DATA
|
|
|
|
| 19 |
df = pd.read_csv(path)
|
| 20 |
df.columns = [c.lower().strip() for c in df.columns]
|
| 21 |
|
|
|
|
| 22 |
req = {"survived", "pclass", "sex", "age", "sibsp", "parch", "fare", "embarked"}
|
| 23 |
miss = req - set(df.columns)
|
| 24 |
if miss:
|
| 25 |
raise ValueError(f"Ontbrekende kolommen: {miss}")
|
| 26 |
|
|
|
|
| 27 |
for col in df.columns:
|
| 28 |
if df[col].isna().any():
|
| 29 |
if df[col].dtype == "object":
|
|
|
|
| 70 |
return make_plot(f, "Overlevingspercentage per Geslacht en Klasse")
|
| 71 |
|
| 72 |
def plot_density_age_fare(x):
|
| 73 |
+
f = px.density_contour(x, x="age", y="fare", color="status",
|
| 74 |
+
marginal_x="histogram", marginal_y="histogram")
|
| 75 |
return make_plot(f, "Leeftijd vs Ticketprijs (dichtheidsverdeling)")
|
| 76 |
|
| 77 |
def plot_bubble_family_fare(x):
|
| 78 |
f = px.scatter(
|
| 79 |
x, x="fare", y="family_size", size="age", color="status",
|
| 80 |
+
hover_data=["sex", "pclass"], size_max=40,
|
| 81 |
+
color_discrete_sequence=px.colors.qualitative.Set3
|
| 82 |
)
|
| 83 |
return make_plot(f, "Bubble Chart β Fare vs Family Size vs Age")
|
| 84 |
|
|
|
|
| 122 |
return f"π― **Nauwkeurigheid:** {acc:.2%} | **ROC AUC:** {auc:.3f}", fig_cm
|
| 123 |
|
| 124 |
# =======================
|
| 125 |
+
# DASHBOARD
|
| 126 |
# =======================
|
| 127 |
def dashboard():
|
| 128 |
acc_text, cm_fig = train_and_evaluate(df)
|
|
|
|
| 151 |
color: #EAF2FF;
|
| 152 |
}
|
| 153 |
.gradio-container {
|
| 154 |
+
background: rgba(10, 16, 26, 0.75);
|
| 155 |
}
|
| 156 |
.gradio-container::before {
|
| 157 |
content: "";
|
|
|
|
| 165 |
.kpi .value {font-size:1.6rem; font-weight:800; color:#FFD26A;}
|
| 166 |
.kpi .label {font-size:0.9rem; color:#C4D7F0;}
|
| 167 |
.section-title {font-size:1.3rem; font-weight:800; color:#FFD26A; margin-top:12px;}
|
| 168 |
+
.dataframe-container {
|
| 169 |
+
height: 320px;
|
| 170 |
+
overflow-y: auto;
|
| 171 |
+
background: rgba(20,28,42,0.7);
|
| 172 |
+
border-radius: 10px;
|
| 173 |
+
padding: 5px;
|
| 174 |
+
}
|
| 175 |
"""
|
| 176 |
|
| 177 |
+
# =======================
|
| 178 |
+
# UI
|
| 179 |
+
# =======================
|
| 180 |
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="blue")) as demo:
|
| 181 |
gr.HTML("<h1 style='text-align:center;margin-top:10px;'>π³οΈ Titanic Data Explorer β Night Sky Edition</h1>")
|
| 182 |
gr.HTML("<p style='text-align:center;color:#C4D7F0;'>Interactieve visualisatie & machine learning analyse</p>")
|
|
|
|
| 205 |
fig_cm = gr.Plot(label="Confusion Matrix")
|
| 206 |
|
| 207 |
gr.HTML("<div class='section-title'>ποΈ Data voorbeeld</div>")
|
| 208 |
+
with gr.Column(elem_classes=["dataframe-container"]):
|
| 209 |
+
table = gr.Dataframe(interactive=False)
|
| 210 |
|
| 211 |
def update_dashboard():
|
| 212 |
return dashboard()
|