I am implementing a Random Forest Classifier model in Streamlit and am displaying model information upon user input (checkbox). Although every time a checkbox is selected, the model is rerun and the accuracy, confusion matrix and summary is changed. I tried caching the model and predictions, but that doesn't seem to solve the issue.
@st.cache
def Classify_RF(df):
RF_Model = RandomForestClassifier()
RF_Model.fit(
X_train,
y_train,
)
return RF_Model
@st.cache
def RF_Predictions(RF_Model):
y_pred = RF_Model.predict(X_test)
return y_pred
Whenever any of these checkboxes are selected, the model gets rerun.
if st.checkbox("See Feature Importance"):
features = ["Open", "High", "Low", "Close", "Adj Close", "Volume"]
for indicator in tech_indicators:
features.append(indicator)
fig = go.Figure([go.Bar(x = features, y = RF_Model.feature_importances_)])
fig.update_layout(title= 'Feature Importances', xaxis_title = 'Features')
st.write(fig)
if st.checkbox("Test Random Forest Classifier Accuracy"):
st.write("Model accuracy on test dataset: ", accuracy_score(y_test, y_pred))
if st.checkbox("View Confusion Matrix"):
st.write(confusion_matrix(y_test, y_pred))