Mpavan45 commited on
Commit
65eb3d0
·
verified ·
1 Parent(s): 90478e8

Create Model Creation with Optuna.py

Browse files
Files changed (1) hide show
  1. pages/Model Creation with Optuna.py +58 -0
pages/Model Creation with Optuna.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import optuna
4
+ from sklearn.ensemble import RandomForestClassifier
5
+ from sklearn.model_selection import train_test_split, cross_val_score
6
+ from sklearn.metrics import classification_report
7
+
8
+ # Title
9
+ st.title("Model Creation and Hyperparameter Tuning with Optuna")
10
+ st.markdown("""
11
+ Upload your dataset, select features and target, and let Optuna optimize hyperparameters
12
+ to train the best Random Forest model.
13
+ """)
14
+
15
+ # File uploader
16
+ uploaded_file = st.file_uploader("Upload your prepared dataset (CSV format):", type=["csv"])
17
+
18
+ if uploaded_file is not None:
19
+ data = pd.read_csv(uploaded_file)
20
+ st.write("### Dataset:")
21
+ st.dataframe(data)
22
+
23
+ # Feature and target selection
24
+ features = st.multiselect("Select Features:", options=data.columns)
25
+ target = st.selectbox("Select Target:", options=data.columns)
26
+
27
+ if features and target:
28
+ X = data[features]
29
+ y = data[target]
30
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
31
+
32
+ def objective(trial):
33
+ model = RandomForestClassifier(
34
+ n_estimators=trial.suggest_int("n_estimators", 10, 200),
35
+ max_depth=trial.suggest_int("max_depth", 2, 32, log=True),
36
+ min_samples_split=trial.suggest_int("min_samples_split", 2, 20),
37
+ min_samples_leaf=trial.suggest_int("min_samples_leaf", 1, 20),
38
+ random_state=42,
39
+ )
40
+ return cross_val_score(model, X_train, y_train, cv=3, scoring="accuracy").mean()
41
+
42
+ st.write("### Hyperparameter Tuning")
43
+ n_trials = st.slider("Number of Trials:", 10, 100, 20)
44
+ if st.button("Start Tuning"):
45
+ study = optuna.create_study(direction="maximize")
46
+ study.optimize(objective, n_trials=n_trials)
47
+
48
+ st.write("#### Best Parameters:")
49
+ st.json(study.best_params)
50
+
51
+ model = RandomForestClassifier(**study.best_params, random_state=42)
52
+ model.fit(X_train, y_train)
53
+
54
+ y_pred = model.predict(X_test)
55
+ st.write("### Model Performance:")
56
+ st.text(classification_report(y_test, y_pred))
57
+ else:
58
+ st.warning("Upload a dataset to start.")