Spaces:
Sleeping
Sleeping
Shafeek Saleem
commited on
Commit
·
b636878
1
Parent(s):
9708914
ss
Browse files
pages/3_Training the Model.py
CHANGED
@@ -30,15 +30,23 @@ def process_file(csv_file):
|
|
30 |
return data
|
31 |
|
32 |
|
33 |
-
def
|
34 |
if model_choice == 'LightGBM':
|
35 |
model = lgb.LGBMRegressor() if not tune_model else lgb.LGBMRegressor(**tuned_parameters('lgbm'))
|
36 |
elif model_choice == 'Random Forest':
|
37 |
model = RandomForestRegressor(n_estimators=100, random_state=42) if not tune_model else RandomForestRegressor(**tuned_parameters('rf'))
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
X_train, X_test, y_train, y_test = train_test_split(
|
42 |
|
43 |
model.fit(X_train, y_train)
|
44 |
y_pred = model.predict(X_test)
|
@@ -178,13 +186,28 @@ def step3_page():
|
|
178 |
pass
|
179 |
|
180 |
if state == "model selection":
|
181 |
-
st.subheader("Step
|
182 |
st.write("Different machine learning algorithms can be used for weather forecasting and the choice of model depends on the characteristics of the data and the forecasting task. But for this tutorial, you can select between a 'LightGBM' model and a 'Random Forest' model.")
|
183 |
models = ['LightGBM', 'Random Forest']
|
184 |
model_choice = st.selectbox('Choose Model', models)
|
|
|
185 |
else:
|
186 |
pass
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
# st.subheader("Step 3: Data Splitting")
|
190 |
# st.write(
|
|
|
30 |
return data
|
31 |
|
32 |
|
33 |
+
def model_train(train_X, train_y, model_choice, train_size, tune_model):
|
34 |
if model_choice == 'LightGBM':
|
35 |
model = lgb.LGBMRegressor() if not tune_model else lgb.LGBMRegressor(**tuned_parameters('lgbm'))
|
36 |
elif model_choice == 'Random Forest':
|
37 |
model = RandomForestRegressor(n_estimators=100, random_state=42) if not tune_model else RandomForestRegressor(**tuned_parameters('rf'))
|
38 |
|
39 |
+
X_train, X_test, y_train, y_test = train_test_split(train_X, train_y, train_size=train_size/100, random_state=42, shuffle=False)
|
40 |
+
model.fit(X_train, y_train)
|
41 |
+
return model, X_test, y_test
|
42 |
+
|
43 |
+
def model_predict(model, X_test, y_test):
|
44 |
+
if model_choice == 'LightGBM':
|
45 |
+
model = lgb.LGBMRegressor() if not tune_model else lgb.LGBMRegressor(**tuned_parameters('lgbm'))
|
46 |
+
elif model_choice == 'Random Forest':
|
47 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42) if not tune_model else RandomForestRegressor(**tuned_parameters('rf'))
|
48 |
|
49 |
+
X_train, X_test, y_train, y_test = train_test_split(train_X, train_y, train_size=train_size/100, random_state=42, shuffle=False)
|
50 |
|
51 |
model.fit(X_train, y_train)
|
52 |
y_pred = model.predict(X_test)
|
|
|
186 |
pass
|
187 |
|
188 |
if state == "model selection":
|
189 |
+
st.subheader("Step 4: Model Selection")
|
190 |
st.write("Different machine learning algorithms can be used for weather forecasting and the choice of model depends on the characteristics of the data and the forecasting task. But for this tutorial, you can select between a 'LightGBM' model and a 'Random Forest' model.")
|
191 |
models = ['LightGBM', 'Random Forest']
|
192 |
model_choice = st.selectbox('Choose Model', models)
|
193 |
+
state = "model training"
|
194 |
else:
|
195 |
pass
|
196 |
|
197 |
+
if state == "model training":
|
198 |
+
st.subheader("Step 5: Model Training")
|
199 |
+
st.write("Finally, let;s train our weather forecasting model based on the parameters that you have selected!")
|
200 |
+
tune_model = st.checkbox('Tune Hyperparameters')
|
201 |
+
|
202 |
+
if st.button("Train", key="train"):
|
203 |
+
model, X_test, y_test = model_train(X, y, model_choice, train_size, tune_model)
|
204 |
+
my_bar = st.progress(0, text="Training model...")
|
205 |
+
for i in range(100):
|
206 |
+
my_bar.progress(i, text="Training model...")
|
207 |
+
my_bar.progress(100, text="Training completed")
|
208 |
+
state = "model predict"
|
209 |
+
else:
|
210 |
+
pass
|
211 |
|
212 |
# st.subheader("Step 3: Data Splitting")
|
213 |
# st.write(
|