Upload streamlit_app.py
Browse files- src/streamlit_app.py +60 -33
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,67 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# To run this app, use: streamlit run test.py
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.metrics import accuracy_score
|
| 9 |
|
| 10 |
+
# Application title and description
|
| 11 |
+
st.title("Machine Learning Model Visualization")
|
| 12 |
+
st.write("This application demonstrates random forest classification on the iris dataset")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Data acquisition and preparation
|
| 15 |
+
@st.cache_data
|
| 16 |
+
def load_data():
|
| 17 |
+
from sklearn.datasets import load_iris
|
| 18 |
+
iris = load_iris()
|
| 19 |
+
df = pd.DataFrame(iris.data, columns=iris.feature_names)
|
| 20 |
+
df['target'] = iris.target
|
| 21 |
+
return df, iris.target_names
|
| 22 |
|
| 23 |
+
data, target_names = load_data()
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Interactive data exploration
|
| 26 |
+
st.subheader("Dataset Exploration")
|
| 27 |
+
if st.checkbox("Display dataset"):
|
| 28 |
+
st.dataframe(data)
|
| 29 |
|
| 30 |
+
# Feature selection interface
|
| 31 |
+
st.subheader("Feature Selection")
|
| 32 |
+
features = st.multiselect(
|
| 33 |
+
"Select features for model training",
|
| 34 |
+
options=data.columns[:-1],
|
| 35 |
+
default=data.columns[0]
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
if len(features) > 0:
|
| 39 |
+
# Model parameters adjustment
|
| 40 |
+
st.subheader("Model Parameters")
|
| 41 |
+
n_estimators = st.slider("Number of trees", 1, 100, 10)
|
| 42 |
+
max_depth = st.slider("Maximum tree depth", 1, 20, 5)
|
| 43 |
+
|
| 44 |
+
# Model training
|
| 45 |
+
if st.button("Train Model"):
|
| 46 |
+
X = data[features]
|
| 47 |
+
y = data['target']
|
| 48 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 49 |
+
|
| 50 |
+
model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
|
| 51 |
+
model.fit(X_train, y_train)
|
| 52 |
+
|
| 53 |
+
# Performance evaluation
|
| 54 |
+
y_pred = model.predict(X_test)
|
| 55 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 56 |
+
|
| 57 |
+
st.success(f"Model accuracy: {accuracy:.4f}")
|
| 58 |
+
|
| 59 |
+
# Visualization of feature importance
|
| 60 |
+
if len(features) > 1:
|
| 61 |
+
st.subheader("Feature Importance")
|
| 62 |
+
fig, ax = plt.subplots()
|
| 63 |
+
ax.bar(features, model.feature_importances_)
|
| 64 |
+
plt.xticks(rotation=45)
|
| 65 |
+
st.pyplot(fig)
|
| 66 |
+
else:
|
| 67 |
+
st.warning("Please select at least one feature for model training")
|