Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- my_app_assignment.py +64 -0
- requirement.txt +5 -0
my_app_assignment.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# my_app.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Load dataset
|
| 9 |
+
df = sns.load_dataset("tips")
|
| 10 |
+
df["tip_pct"] = (df["tip"] / df["total_bill"]) * 100
|
| 11 |
+
df["tip_pct"] = df["tip_pct"].round(2)
|
| 12 |
+
|
| 13 |
+
# App title
|
| 14 |
+
st.title("Restaurant Tip Insights")
|
| 15 |
+
st.markdown("Explore tip percentages by day, gender, and smoker status.")
|
| 16 |
+
|
| 17 |
+
# Sidebar filters
|
| 18 |
+
day = st.sidebar.selectbox("Select a day", df['day'].unique())
|
| 19 |
+
gender = st.sidebar.multiselect("Select gender(s)", df['sex'].unique(), default=df['sex'].unique())
|
| 20 |
+
smoker = st.sidebar.multiselect("Select smoker status", df['smoker'].unique(), default=df['smoker'].unique())
|
| 21 |
+
|
| 22 |
+
# Filter data
|
| 23 |
+
filtered = df[(df['day'] == day) & (df['sex'].isin(gender)) & (df['smoker'].isin(smoker))]
|
| 24 |
+
|
| 25 |
+
# KPI
|
| 26 |
+
avg_tip = filtered['tip_pct'].mean()
|
| 27 |
+
st.metric(label=f"Average Tip % on {day}", value=f"{avg_tip:.2f}%")
|
| 28 |
+
|
| 29 |
+
median_smokers = filtered[filtered["smoker"]=="Yes"]["tip_pct"].median()
|
| 30 |
+
median_non_smokers = filtered[filtered["smoker"]=="No"]["tip_pct"].median()
|
| 31 |
+
st.write(f"Median tip % for smokers: {median_smokers:.2f}%")
|
| 32 |
+
st.write(f"Median tip % for non-smokers: {median_non_smokers:.2f}%")
|
| 33 |
+
|
| 34 |
+
# Plot 1 — Average Tip Percentage by Day
|
| 35 |
+
avg_by_day = df.groupby("day")["tip_pct"].mean()
|
| 36 |
+
fig1, ax1 = plt.subplots(figsize=(6,4))
|
| 37 |
+
avg_by_day.plot(kind="bar", color="skyblue", ax=ax1)
|
| 38 |
+
ax1.set_title("Average Tip Percentage by Day")
|
| 39 |
+
ax1.set_xlabel("Day of Week")
|
| 40 |
+
ax1.set_ylabel("Average Tip Percentage (%)")
|
| 41 |
+
ax1.grid(axis="y", linestyle="--", alpha=0.7)
|
| 42 |
+
st.pyplot(fig1)
|
| 43 |
+
st.markdown("**Interpretation:** Friday has the highest average tip percentage, Saturday the lowest.")
|
| 44 |
+
|
| 45 |
+
# Plot 2 — Tip Percentage vs Total Bill by Sex
|
| 46 |
+
fig2, ax2 = plt.subplots(figsize=(8,6))
|
| 47 |
+
sns.scatterplot(x="total_bill", y="tip_pct", hue="sex", data=filtered, alpha=0.6, ax=ax2)
|
| 48 |
+
ax2.set_title("Tip Percentage vs Total Bill by Sex")
|
| 49 |
+
ax2.set_xlabel("Total Bill")
|
| 50 |
+
ax2.set_ylabel("Tip Percentage (%)")
|
| 51 |
+
ax2.grid(True, linestyle="--", alpha=0.5)
|
| 52 |
+
st.pyplot(fig2)
|
| 53 |
+
st.markdown("**Interpretation:** Tip % tends to decrease as total bill increases. Some small bills have very high tip percentages.")
|
| 54 |
+
|
| 55 |
+
# Plot 3 — Tip Percentage by Smoking Status
|
| 56 |
+
fig3, ax3 = plt.subplots()
|
| 57 |
+
sns.boxplot(x="smoker", y="tip_pct", data=filtered, palette="Set2", ax=ax3)
|
| 58 |
+
ax3.set_title("Tip Percentage by Smoking Status")
|
| 59 |
+
ax3.set_xlabel("Smoker Status")
|
| 60 |
+
ax3.set_ylabel("Tip Percentage (%)")
|
| 61 |
+
ax3.grid(axis="y", linestyle="--", alpha=0.7)
|
| 62 |
+
st.pyplot(fig3)
|
| 63 |
+
st.markdown("**Interpretation:** Smoking status does not largely impact tip %, but some smokers tip higher.")
|
| 64 |
+
|
requirement.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
seaborn
|
| 4 |
+
matplotlib
|
| 5 |
+
|