Upload 2 files
Browse files- app.py +67 -0
- requirements.txt +4 -2
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
-
|
| 2 |
pandas
|
| 3 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
pandas
|
| 3 |
+
numpy
|
| 4 |
+
matplotlib
|
| 5 |
+
scikit-learn
|