Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import altair as alt
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import seaborn as sns
|
7 |
+
from duckduckgo_search import DDGS
|
8 |
+
|
9 |
+
# Function to load the dataset
|
10 |
+
@st.cache_data # Cache the function to enhance performance
|
11 |
+
def load_data():
|
12 |
+
# Define the file path
|
13 |
+
file_path = 'https://raw.githubusercontent.com/aaubs/ds-master/main/apps/M1-attrition-streamlit/HR-Employee-Attrition-synth.csv'
|
14 |
+
|
15 |
+
# Load the CSV file into a pandas dataframe
|
16 |
+
df = pd.read_csv(file_path)
|
17 |
+
|
18 |
+
# Create age groups and add as a new column
|
19 |
+
bin_edges = [18, 25, 35, 45, 60]
|
20 |
+
bin_labels = ['18-24', '25-34', '35-44', '45-60']
|
21 |
+
df['AgeGroup'] = pd.cut(df['Age'], bins=bin_edges, labels=bin_labels, right=False)
|
22 |
+
|
23 |
+
return df
|
24 |
+
|
25 |
+
# Load the data using the defined function
|
26 |
+
df = load_data()
|
27 |
+
|
28 |
+
# Set the app title and sidebar header
|
29 |
+
st.title("Employee Attrition Dashboard ππ")
|
30 |
+
st.sidebar.header("Filters π")
|
31 |
+
|
32 |
+
# Introduction
|
33 |
+
|
34 |
+
# HR Attrition Dashboard
|
35 |
+
|
36 |
+
st.markdown("""
|
37 |
+
Welcome to the HR Attrition Dashboard. In the backdrop of rising employee turnovers, HR departments are stressing the significance of predicting and understanding employee departures. Through the lens of data analytics, this dashboard unveils the deeper causes of employee churn and proposes strategies to boost employee retention.
|
38 |
+
""")
|
39 |
+
with st.expander("π **Objective**"):
|
40 |
+
st.markdown("""
|
41 |
+
At the heart of this dashboard is the mission to visually decode data, equipping HR experts with insights to tackle these queries:
|
42 |
+
- Which company factions face a greater likelihood of employee exits?
|
43 |
+
- What might be pushing these individuals to part ways?
|
44 |
+
- Observing the discerned trends, what incentives might hold the key to decreasing the attrition rate?
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
|
48 |
+
# Tutorial Expander
|
49 |
+
with st.expander("How to Use the Dashboard π"):
|
50 |
+
st.markdown("""
|
51 |
+
1. **Filter Data** - Use the sidebar filters to narrow down specific data sets.
|
52 |
+
2. **Visualize Data** - From the dropdown, select a visualization type to view patterns.
|
53 |
+
3. **Insights & Recommendations** - Scroll down to see insights derived from the visualizations and actionable recommendations.
|
54 |
+
""")
|
55 |
+
|
56 |
+
|
57 |
+
# Sidebar filter: Age Group
|
58 |
+
selected_age_group = st.sidebar.multiselect("Select Age Groups π°οΈ", df['AgeGroup'].unique().tolist(), default=df['AgeGroup'].unique().tolist())
|
59 |
+
if not selected_age_group:
|
60 |
+
st.warning("Please select an age group from the sidebar β οΈ")
|
61 |
+
st.stop()
|
62 |
+
filtered_df = df[df['AgeGroup'].isin(selected_age_group)]
|
63 |
+
|
64 |
+
# Sidebar filter: Department
|
65 |
+
departments = df['Department'].unique().tolist()
|
66 |
+
selected_department = st.sidebar.multiselect("Select Departments π’", departments, default=departments)
|
67 |
+
if not selected_department:
|
68 |
+
st.warning("Please select a department from the sidebar β οΈ")
|
69 |
+
st.stop()
|
70 |
+
filtered_df = filtered_df[filtered_df['Department'].isin(selected_department)]
|
71 |
+
|
72 |
+
# Sidebar filter: Monthly Income Range
|
73 |
+
min_income = int(df['MonthlyIncome'].min())
|
74 |
+
max_income = int(df['MonthlyIncome'].max())
|
75 |
+
income_range = st.sidebar.slider("Select Monthly Income Range π°", min_income, max_income, (min_income, max_income))
|
76 |
+
filtered_df = filtered_df[(filtered_df['MonthlyIncome'] >= income_range[0]) & (filtered_df['MonthlyIncome'] <= income_range[1])]
|
77 |
+
|
78 |
+
# Sidebar filter: Job Satisfaction Level
|
79 |
+
satisfaction_levels = sorted(df['JobSatisfaction'].unique().tolist())
|
80 |
+
selected_satisfaction = st.sidebar.multiselect("Select Job Satisfaction Levels π", satisfaction_levels, default=satisfaction_levels)
|
81 |
+
if not selected_satisfaction:
|
82 |
+
st.warning("Please select a job satisfaction level from the sidebar β οΈ")
|
83 |
+
st.stop()
|
84 |
+
filtered_df = filtered_df[filtered_df['JobSatisfaction'].isin(selected_satisfaction)]
|
85 |
+
|
86 |
+
# Displaying the Attrition Analysis header
|
87 |
+
st.header("Attrition Analysis π")
|
88 |
+
|
89 |
+
# Dropdown to select the type of visualization
|
90 |
+
visualization_option = st.selectbox(
|
91 |
+
"Select Visualization π¨",
|
92 |
+
["Attrition by Age Group",
|
93 |
+
"KDE Plot: Distance from Home by Attrition",
|
94 |
+
"Attrition by Job Role",
|
95 |
+
"Attrition Distribution by Gender",
|
96 |
+
"MonthlyRate and DailyRate by JobLevel"]
|
97 |
+
)
|
98 |
+
|
99 |
+
# Visualizations based on user selection
|
100 |
+
if visualization_option == "Attrition by Age Group":
|
101 |
+
# Bar chart for attrition by age group
|
102 |
+
chart = alt.Chart(filtered_df).mark_bar().encode(
|
103 |
+
x='AgeGroup',
|
104 |
+
y='count()',
|
105 |
+
color='Attrition'
|
106 |
+
).properties(
|
107 |
+
title='Attrition Rate by Age Group'
|
108 |
+
)
|
109 |
+
st.altair_chart(chart, use_container_width=True)
|
110 |
+
|
111 |
+
elif visualization_option == "KDE Plot: Distance from Home by Attrition":
|
112 |
+
# KDE plot for Distance from Home based on Attrition
|
113 |
+
plt.figure(figsize=(10, 6))
|
114 |
+
sns.kdeplot(data=filtered_df, x='DistanceFromHome', hue='Attrition', fill=True, palette='Set2')
|
115 |
+
plt.xlabel('Distance From Home')
|
116 |
+
plt.ylabel('Density')
|
117 |
+
plt.title('KDE Plot of Distance From Home by Attrition')
|
118 |
+
st.pyplot(plt)
|
119 |
+
|
120 |
+
elif visualization_option == "Attrition by Job Role":
|
121 |
+
# Bar chart for attrition by job role
|
122 |
+
chart = alt.Chart(filtered_df).mark_bar().encode(
|
123 |
+
y='JobRole',
|
124 |
+
x='count()',
|
125 |
+
color='Attrition'
|
126 |
+
).properties(
|
127 |
+
title='Attrition by Job Role'
|
128 |
+
)
|
129 |
+
st.altair_chart(chart, use_container_width=True)
|
130 |
+
|
131 |
+
elif visualization_option == "Attrition Distribution by Gender":
|
132 |
+
# Pie chart for attrition distribution by gender
|
133 |
+
pie_chart_data = filtered_df[filtered_df['Attrition'] == 'Yes']['Gender'].value_counts().reset_index()
|
134 |
+
pie_chart_data.columns = ['Gender', 'count']
|
135 |
+
|
136 |
+
chart = alt.Chart(pie_chart_data).mark_arc().encode(
|
137 |
+
theta='count:Q',
|
138 |
+
color='Gender:N',
|
139 |
+
tooltip=['Gender', 'count']
|
140 |
+
).properties(
|
141 |
+
title='Attrition Distribution by Gender',
|
142 |
+
width=300,
|
143 |
+
height=300
|
144 |
+
).project('identity')
|
145 |
+
st.altair_chart(chart, use_container_width=True)
|
146 |
+
|
147 |
+
elif visualization_option == "MonthlyRate and DailyRate by JobLevel":
|
148 |
+
# Boxplots for MonthlyRate and DailyRate by JobLevel
|
149 |
+
fig, ax = plt.subplots(1, 2, figsize=(15, 7))
|
150 |
+
|
151 |
+
# MonthlyRate by JobLevel
|
152 |
+
sns.boxplot(x="JobLevel", y="MonthlyRate", data=filtered_df, ax=ax[0], hue="JobLevel", palette='Set2', legend=False)
|
153 |
+
ax[0].set_title('MonthlyRate by JobLevel')
|
154 |
+
ax[0].set_xlabel('Job Level')
|
155 |
+
ax[0].set_ylabel('Monthly Rate')
|
156 |
+
|
157 |
+
# DailyRate by JobLevel
|
158 |
+
sns.boxplot(x="JobLevel", y="DailyRate", data=filtered_df, ax=ax[1], hue="JobLevel", palette='Set2', legend=False)
|
159 |
+
ax[1].set_title('DailyRate by JobLevel')
|
160 |
+
ax[1].set_xlabel('Job Level')
|
161 |
+
ax[1].set_ylabel('Daily Rate')
|
162 |
+
|
163 |
+
plt.tight_layout()
|
164 |
+
st.pyplot(fig)
|
165 |
+
|
166 |
+
# Display dataset overview
|
167 |
+
st.header("Dataset Overview")
|
168 |
+
st.dataframe(df.describe())
|
169 |
+
|
170 |
+
|
171 |
+
# Insights from Visualization Section Expander
|
172 |
+
with st.expander("Insights from Visualization π§ "):
|
173 |
+
st.markdown("""
|
174 |
+
1. **Age Groups & Attrition** - The 'Attrition by Age Group' plot showcases which age brackets face higher attrition.
|
175 |
+
2. **Home Distance's Impact** - The 'KDE Plot: Distance from Home by Attrition' visualizes if being farther away influences leaving tendencies.
|
176 |
+
3. **Roles & Attrition** - 'Attrition by Job Role' reveals which roles might be more attrition-prone.
|
177 |
+
4. **Gender & Attrition** - The pie chart for 'Attrition Distribution by Gender' provides insights into any gender-based patterns.
|
178 |
+
5. **Earnings Patterns** - 'MonthlyRate and DailyRate by JobLevel' boxplots display the compensation distribution across job levels.
|
179 |
+
""")
|
180 |
+
|
181 |
+
# Recommendations Expander
|
182 |
+
with st.expander("Recommendations for Action π"):
|
183 |
+
st.markdown("""
|
184 |
+
- π **Incentive Programs:** Introduce incentives tailored for groups showing higher attrition tendencies.
|
185 |
+
- π‘ **Remote Work Options:** Providing flexibility, especially for those living farther from the workplace, could reduce attrition.
|
186 |
+
- π **Training & Growth:** Invest in employee development, especially in roles with higher attrition rates.
|
187 |
+
- π« **Gender Equality:** Foster an environment that supports equal opportunities regardless of gender.
|
188 |
+
- πΈ **Compensation Review:** Regularly review and adjust compensation structures to stay competitive and retain talent.
|
189 |
+
""")
|
190 |
+
|
191 |
+
if st.button("AI Little Inayah"):
|
192 |
+
with st.expander("AI Evalution"):
|
193 |
+
st.markdown(DDGS().chat("You are a smart HR person: Provide a concise 3 sentences evaluation of HR situation given some certain datapoints here: "+str(df.describe()), model='claude-3-haiku'))
|