Riya1217 commited on
Commit
0893455
·
verified ·
1 Parent(s): 690bf95

Upload 2 files

Browse files
Files changed (2) hide show
  1. my_app_assignment.py +70 -0
  2. requirements.txt +5 -0
my_app_assignment.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:** This bar chart shows the average tip percentage for each day of the week present in the dataset (Thursday, Friday, Saturday, and Sunday)." \
44
+ " Friday has the highest average tip percentage, while Saturday has the lowest." \
45
+ " Thursday and Sunday have similar average tip percentages, falling between Friday and Saturday.")
46
+
47
+ # Plot 2 — Tip Percentage vs Total Bill by Sex
48
+ fig2, ax2 = plt.subplots(figsize=(8,6))
49
+ sns.scatterplot(x="total_bill", y="tip_pct", hue="sex", data=filtered, alpha=0.6, ax=ax2)
50
+ ax2.set_title("Tip Percentage vs Total Bill by Sex")
51
+ ax2.set_xlabel("Total Bill")
52
+ ax2.set_ylabel("Tip Percentage (%)")
53
+ ax2.grid(True, linestyle="--", alpha=0.5)
54
+ st.pyplot(fig2)
55
+ st.markdown("**Interpretation:** The scatter plot shows that as the total bill goes up, the tip percentage generally goes down." \
56
+ " This means people(both male and female) tend to tip a smaller percentage on larger bills. " \
57
+ "There are a few cases with very high tip percentages, especially on smaller bills.")
58
+
59
+ # Plot 3 — Tip Percentage by Smoking Status
60
+ fig3, ax3 = plt.subplots()
61
+ sns.boxplot(x="smoker", y="tip_pct", data=filtered, palette="Set2", ax=ax3)
62
+ ax3.set_title("Tip Percentage by Smoking Status")
63
+ ax3.set_xlabel("Smoker Status")
64
+ ax3.set_ylabel("Tip Percentage (%)")
65
+ ax3.grid(axis="y", linestyle="--", alpha=0.7)
66
+ st.pyplot(fig3)
67
+ st.markdown("**Interpretation:** The box plot and median values suggest that smoking status does not have a large impact on the average tip percentage. " \
68
+ "The median tip percentage for smokers is 15,41%, and for non-smokers it is 15.56%." \
69
+ " However, there are some instances where smokers gave significantly higher tips.")
70
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ seaborn
4
+ matplotlib
5
+