Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- my_app_assignment.py +79 -0
- requirements.txt +5 -0
my_app_assignment.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Load dataset from CSV
|
| 10 |
+
df = pd.read_csv("tips.csv")
|
| 11 |
+
|
| 12 |
+
# App title
|
| 13 |
+
st.title("Restaurant Tip Insights")
|
| 14 |
+
st.markdown("Explore tip percentages by day, gender, and smoker status.")
|
| 15 |
+
|
| 16 |
+
# Sidebar filters
|
| 17 |
+
day = st.sidebar.selectbox("Select a day", df['day'].unique())
|
| 18 |
+
gender = st.sidebar.multiselect("Select gender(s)", df['sex'].unique(), default=df['sex'].unique())
|
| 19 |
+
smoker = st.sidebar.multiselect("Select smoker status", df['smoker'].unique(), default=df['smoker'].unique())
|
| 20 |
+
|
| 21 |
+
# Filter data
|
| 22 |
+
filtered = df[(df['day'] == day) & (df['sex'].isin(gender)) & (df['smoker'].isin(smoker))]
|
| 23 |
+
|
| 24 |
+
# KPI
|
| 25 |
+
avg_tip = filtered['tip_pct'].mean()
|
| 26 |
+
st.metric(label=f"Average Tip % on {day}", value=f"{avg_tip:.2f}%")
|
| 27 |
+
|
| 28 |
+
# Plot 1 — Average Tip Percentage by Day
|
| 29 |
+
avg_by_day = df.groupby("day")["tip_pct"].mean()
|
| 30 |
+
fig1, ax1 = plt.subplots(figsize=(6,4))
|
| 31 |
+
avg_by_day.plot(kind="bar", color="skyblue", ax=ax1)
|
| 32 |
+
ax1.set_title("Average Tip Percentage by Day")
|
| 33 |
+
ax1.set_xlabel("Day of Week")
|
| 34 |
+
ax1.set_ylabel("Average Tip Percentage (%)")
|
| 35 |
+
ax1.grid(axis="y", linestyle="--", alpha=0.7)
|
| 36 |
+
st.pyplot(fig1)
|
| 37 |
+
st.markdown("**Interpretation:** This bar chart shows the average tip percentage for each day of the week present in the dataset (Thursday, Friday, Saturday, and Sunday)." \
|
| 38 |
+
" Friday has the highest average tip percentage, while Saturday has the lowest." \
|
| 39 |
+
" Thursday and Sunday have similar average tip percentages, falling between Friday and Saturday.")
|
| 40 |
+
|
| 41 |
+
# Plot 2 — Tip Percentage vs Total Bill by Sex
|
| 42 |
+
fig2, ax2 = plt.subplots(figsize=(8,6))
|
| 43 |
+
sns.scatterplot(x="total_bill", y="tip_pct", hue="sex", data=filtered, alpha=0.6, ax=ax2)
|
| 44 |
+
ax2.set_title("Tip Percentage vs Total Bill by Sex")
|
| 45 |
+
ax2.set_xlabel("Total Bill")
|
| 46 |
+
ax2.set_ylabel("Tip Percentage (%)")
|
| 47 |
+
ax2.grid(True, linestyle="--", alpha=0.5)
|
| 48 |
+
st.pyplot(fig2)
|
| 49 |
+
st.markdown("**Interpretation:** The scatter plot shows that as the total bill goes up, the tip percentage generally goes down." \
|
| 50 |
+
" This means people(both male and female) tend to tip a smaller percentage on larger bills. " \
|
| 51 |
+
"There are a few cases with very high tip percentages, especially on smaller bills.")
|
| 52 |
+
|
| 53 |
+
# Plot 3 — Tip Percentage by Smoking Status
|
| 54 |
+
fig3, ax3 = plt.subplots(figsize=(6,4)) # match Colab size
|
| 55 |
+
sns.set_style("whitegrid") # match Colab style
|
| 56 |
+
|
| 57 |
+
# Use filtered data from sidebar
|
| 58 |
+
sns.boxplot(x="smoker", y="tip_pct", data=filtered, palette="Set2", ax=ax3)
|
| 59 |
+
|
| 60 |
+
# Titles and labels
|
| 61 |
+
ax3.set_title("Tip Percentage by Smoking Status")
|
| 62 |
+
ax3.set_xlabel("Smoker Status")
|
| 63 |
+
ax3.set_ylabel("Tip Percentage (%)")
|
| 64 |
+
ax3.grid(axis="y", linestyle="--", alpha=0.7)
|
| 65 |
+
|
| 66 |
+
# Display plot in Streamlit
|
| 67 |
+
st.pyplot(fig3)
|
| 68 |
+
|
| 69 |
+
# --- Calculate medians dynamically ---
|
| 70 |
+
median_smokers = filtered[filtered["smoker"]=="Yes"]["tip_pct"].median()
|
| 71 |
+
median_non_smokers = filtered[filtered["smoker"]=="No"]["tip_pct"].median()
|
| 72 |
+
|
| 73 |
+
# --- Display interpretation dynamically ---
|
| 74 |
+
st.markdown(
|
| 75 |
+
f"**Interpretation:** The median tip percentage for smokers is {median_smokers:.2f}%, "
|
| 76 |
+
f"and for non-smokers it is {median_non_smokers:.2f}%. "
|
| 77 |
+
"The box plot suggests that smoking status does not have a large impact on the average tip percentage. "
|
| 78 |
+
"However, there are some instances where smokers gave significantly higher tips."
|
| 79 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
seaborn
|
| 4 |
+
matplotlib
|
| 5 |
+
|