Update src/streamlit_app.py
Browse files- src/streamlit_app.py +77 -29
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,88 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
.encode(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import altair as alt
|
| 4 |
+
|
| 5 |
+
# 1. Page title
|
| 6 |
+
st.title("Phase 02: Interactive Crime Data Pie Charts")
|
| 7 |
|
| 8 |
+
# 2. Data info & load
|
| 9 |
+
st.header("Dataset Information")
|
| 10 |
+
st.markdown(
|
| 11 |
+
"""
|
| 12 |
+
- **Source:** Chicago crime incidents dataset
|
| 13 |
+
- **Rows:** incidents (one per row)
|
| 14 |
+
- **Columns:** e.g. `crm_cd_desc` (crime type), `arrest` (boolean), `date`, `location_description`, etc.
|
| 15 |
+
- **Purpose:** Explore crime-type distribution and arrest outcomes via pie charts.
|
| 16 |
"""
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
@st.cache_data
|
| 20 |
+
def load_data():
|
| 21 |
+
return pd.read_csv("crime_data.csv")
|
| 22 |
|
| 23 |
+
df = load_data()
|
| 24 |
+
|
| 25 |
+
# 3. Data preview
|
| 26 |
+
st.header("Data Preview")
|
| 27 |
+
st.write(f"Total records: {df.shape[0]} | Total columns: {df.shape[1]}")
|
| 28 |
+
st.dataframe(df.head())
|
| 29 |
|
| 30 |
+
# 4. Pie Chart 1: Top 10 Crime Types
|
| 31 |
+
st.header("Pie Chart 1: Top 10 Crime Types Distribution")
|
| 32 |
|
| 33 |
+
# Prepare Top 10 crime types
|
| 34 |
+
top_crimes = (
|
| 35 |
+
df["crm_cd_desc"]
|
| 36 |
+
.value_counts()
|
| 37 |
+
.nlargest(10)
|
| 38 |
+
.reset_index()
|
| 39 |
+
.rename(columns={"index": "Crime Type", "crm_cd_desc": "Count"})
|
| 40 |
+
)
|
| 41 |
+
top_crimes["Percentage"] = top_crimes["Count"] / top_crimes["Count"].sum()
|
| 42 |
+
|
| 43 |
+
# Build Altair pie chart
|
| 44 |
+
chart1 = (
|
| 45 |
+
alt.Chart(top_crimes)
|
| 46 |
+
.mark_arc(innerRadius=50)
|
| 47 |
+
.encode(
|
| 48 |
+
theta=alt.Theta(field="Count", type="quantitative"),
|
| 49 |
+
color=alt.Color(field="Crime Type", type="nominal"),
|
| 50 |
+
tooltip=[
|
| 51 |
+
alt.Tooltip("Crime Type:N", title="Crime Type"),
|
| 52 |
+
alt.Tooltip("Count:Q", title="Count"),
|
| 53 |
+
alt.Tooltip("Percentage:Q", title="Percentage", format=".1%")
|
| 54 |
+
],
|
| 55 |
+
)
|
| 56 |
+
.properties(width=400, height=400, title="Top 10 Crime Types")
|
| 57 |
+
)
|
| 58 |
+
st.altair_chart(chart1, use_container_width=True)
|
| 59 |
|
| 60 |
+
# 5. Pie Chart 2: Arrest vs. Non-Arrest Distribution
|
| 61 |
+
st.header("Pie Chart 2: Arrest vs. Non-Arrest Distribution")
|
| 62 |
|
| 63 |
+
# Prepare arrest data
|
| 64 |
+
arrest_counts = (
|
| 65 |
+
df["arrest"]
|
| 66 |
+
.map({True: "Arrest Made", False: "No Arrest"})
|
| 67 |
+
.value_counts()
|
| 68 |
+
.reset_index()
|
| 69 |
+
.rename(columns={"index": "Arrest Status", "arrest": "Count"})
|
| 70 |
+
)
|
| 71 |
+
arrest_counts["Percentage"] = arrest_counts["Count"] / arrest_counts["Count"].sum()
|
| 72 |
|
| 73 |
+
# Build second Altair pie chart
|
| 74 |
+
chart2 = (
|
| 75 |
+
alt.Chart(arrest_counts)
|
| 76 |
+
.mark_arc(innerRadius=30)
|
| 77 |
.encode(
|
| 78 |
+
theta=alt.Theta(field="Count", type="quantitative"),
|
| 79 |
+
color=alt.Color(field="Arrest Status", type="nominal"),
|
| 80 |
+
tooltip=[
|
| 81 |
+
alt.Tooltip("Arrest Status:N", title="Status"),
|
| 82 |
+
alt.Tooltip("Count:Q", title="Count"),
|
| 83 |
+
alt.Tooltip("Percentage:Q", title="Percentage", format=".1%")
|
| 84 |
+
],
|
| 85 |
+
)
|
| 86 |
+
.properties(width=400, height=400, title="Arrest Outcomes")
|
| 87 |
+
)
|
| 88 |
+
st.altair_chart(chart2, use_container_width=True)
|