create an app that allows me to train a model using scikit-learn (and some sample binary classification data), and dump the model as a pickle file, then on a different section, allow me to select one a pickle file and make predictions on new data (generate new synthetic data when clicking on the button to run the predictions)
To upload files, please first save the app
import streamlit as st
import pandas as pd
import numpy as np
import pickle
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# Define a function to create synthetic data
def create_data(samples=100):
X, y = make_classification(n_samples=samples, n_features=20, n_informative=2, n_redundant=10)
return pd.DataFrame(X), pd.Series(y)
# Training section
st.title('Train a Model')
if st.button('Generate Sample Data'):
X, y = create_data(samples=100)
st.session_state.X_train = X
st.session_state.y_train = y
st.write('Sample data generated!')
st.dataframe(X.head())
if 'X_train' in st.session_state and 'y_train' in st.session_state:
if st.button('Train Model'):
X_train, X_test, y_train, y_test = train_test_split(st.session_state.X_train, st.session_state.y_train, test_size=0.2, random_state=42)
model = RandomForestClassifier()
model.fit(X_train, y_train)
st.session_state.model = model
st.write('Model trained!')
if st.button('Save Model'):
if 'model' in st.session_state:
model_filename = 'trained_model.pkl'
with open(model_filename, 'wb') as file:
pickle.dump(st.session_state.model, file)
st.write(f'Model saved as {model_filename}!')
# Prediction section
st.title('Make Predictions')
if st.button('Load Model'):
model_filename = 'trained_model.pkl'
with open(model_filename, 'rb') as file:
loaded_model = pickle.load(file)
st.session_state.loaded_model = loaded_model
st.write('Model loaded!')
if 'loaded_model' in st.session_state:
if st.button('Generate New Data and Predict'):
X_new, _ = create_data(samples=10)
print("X_new", X_new)
predictions = st.session_state.loaded_model.predict(X_new)
st.write('Predictions:', predictions)
st.dataframe(X_new)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?