You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
miner/frontend/pages/prediction_classification.py

42 lines
1.7 KiB

import streamlit as st
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
import pandas as pd
st.header("Prediction: Classification")
if "data" in st.session_state:
data = st.session_state.data
with st.form("classification_form"):
st.subheader("Random Forest Parameters")
data_name = st.multiselect("Features", data.select_dtypes(include="object").columns, help="Sélectionnez les caractéristiques pour l'entraînement.")
target_name = st.selectbox("Target", data.columns, help="Sélectionnez la variable cible pour l'entraînement.")
n_estimators = st.number_input("Number of estimators", step=1, min_value=1, value=100, help="Nombre d'arbres dans la forêt.")
max_depth = st.number_input("Max depth", step=1, min_value=1, value=10, help="Profondeur maximale des arbres.")
submit_button = st.form_submit_button('Train and Predict')
if submit_button and data_name and target_name:
le = LabelEncoder()
X = data[data_name].apply(le.fit_transform)
y = le.fit_transform(data[target_name])
model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=111)
model.fit(X, y)
st.subheader("Enter values for prediction")
pred_values = [st.selectbox(f"Value for {feature}", options=data[feature].unique(), key=f"value_{feature}") for feature in data_name]
pred_values_encoded = [le.transform([val])[0] for val in pred_values]
prediction = model.predict([pred_values_encoded])
prediction_decoded = le.inverse_transform(prediction)
st.write("Prediction:", prediction_decoded[0])
else:
st.error("File not loaded")