-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (109 loc) · 3.44 KB
/
Copy pathapp.py
File metadata and controls
137 lines (109 loc) · 3.44 KB
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import streamlit as st
import numpy as np
import pandas as pd
import joblib
import plotly.express as px
import plotly.graph_objects as go
from sklearn.datasets import load_iris
st.set_page_config(
page_title="Iris ML App",
page_icon="🌸",
layout="wide"
)
iris = load_iris()
X = iris.data
y = iris.target
feature_names = iris.feature_names
target_names = iris.target_names
model = joblib.load("random_forest_iris_model.pkl")
df = pd.DataFrame(X, columns=feature_names)
df["species"] = y
df["species_name"] = df["species"].apply(lambda x: target_names[x])
st.sidebar.title("🌿 Input Features")
sepal_length = st.sidebar.slider(
"Sepal Length (cm)", float(df.iloc[:,0].min()), float(df.iloc[:,0].max()), 5.1
)
sepal_width = st.sidebar.slider(
"Sepal Width (cm)", float(df.iloc[:,1].min()), float(df.iloc[:,1].max()), 3.5
)
petal_length = st.sidebar.slider(
"Petal Length (cm)", float(df.iloc[:,2].min()), float(df.iloc[:,2].max()), 1.4
)
petal_width = st.sidebar.slider(
"Petal Width (cm)", float(df.iloc[:,3].min()), float(df.iloc[:,3].max()), 0.2
)
input_data = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
prediction = model.predict(input_data)[0]
prediction_proba = model.predict_proba(input_data)[0]
st.title("🌸 Iris Flower Classification App")
st.caption("Random Forest • Streamlit • Plotly • ML")
col1, col2, col3 = st.columns(3)
col1.metric("🌼 Prediction", target_names[prediction])
col2.metric("🔥 Confidence", f"{np.max(prediction_proba)*100:.2f}%")
col3.metric("📦 Model", "Random Forest")
tab1, tab2, tab3 = st.tabs(["📊 Visualizations", "🧠 Model Confidence", "📈 Dataset Overview"])
with tab1:
st.subheader("2D Feature Relationship")
fig_2d = px.scatter(
df,
x="petal length (cm)",
y="petal width (cm)",
color="species_name",
opacity=0.7
)
fig_2d.add_scatter(
x=[petal_length],
y=[petal_width],
mode="markers",
marker=dict(size=15, color="black", symbol="star"),
name="Your Input"
)
st.plotly_chart(fig_2d, use_container_width=True)
st.subheader("3D Iris Visualization")
fig_3d = px.scatter_3d(
df,
x="sepal length (cm)",
y="sepal width (cm)",
z="petal length (cm)",
color="species_name",
opacity=0.6
)
fig_3d.add_trace(
go.Scatter3d(
x=[sepal_length],
y=[sepal_width],
z=[petal_length],
mode="markers",
marker=dict(size=8, color="black", symbol="diamond"),
name="Your Input"
)
)
st.plotly_chart(fig_3d, use_container_width=True)
with tab2:
st.subheader("Prediction Probability")
proba_df = pd.DataFrame({
"Species": target_names,
"Probability": prediction_proba
})
fig_bar = px.bar(
proba_df,
x="Species",
y="Probability",
color="Species",
text_auto=".2f"
)
st.plotly_chart(fig_bar, use_container_width=True)
with tab3:
st.subheader("Iris Dataset Preview")
st.dataframe(df.head(20), use_container_width=True)
st.subheader("Feature Distribution")
feature = st.selectbox("Choose Feature", feature_names)
fig_hist = px.histogram(
df,
x=feature,
color="species_name",
marginal="box"
)
st.plotly_chart(fig_hist, use_container_width=True)
st.markdown("---")
st.caption("Created By Muhammad Uzair | © 2026")