Spaces:
Sleeping
Sleeping
Shafeek Saleem
commited on
Commit
·
b576f65
1
Parent(s):
b636878
ss
Browse files- pages/3_Training the Model.py +20 -53
pages/3_Training the Model.py
CHANGED
@@ -30,29 +30,16 @@ def process_file(csv_file):
|
|
30 |
return data
|
31 |
|
32 |
|
33 |
-
def model_train(train_X, train_y, model_choice, train_size
|
34 |
if model_choice == 'LightGBM':
|
35 |
-
model = lgb.LGBMRegressor()
|
36 |
elif model_choice == 'Random Forest':
|
37 |
-
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
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)
|
53 |
-
|
54 |
-
return y_test, y_pred, model
|
55 |
-
|
56 |
|
57 |
def create_model_inputs(data, lag, mean_period):
|
58 |
df_processed = data.copy()
|
@@ -174,16 +161,12 @@ def step3_page():
|
|
174 |
with cols[1]:
|
175 |
st.info("Let's display our Target variable")
|
176 |
st.dataframe(y)
|
177 |
-
else:
|
178 |
-
pass
|
179 |
|
180 |
if state == "splitting":
|
181 |
st.subheader("Step 3: Data Splitting")
|
182 |
st.write("Now let's split the dataset into training and testing sets. The training set is used to train the machine learning model, and the testing set is used to evaluate its performance. For that, you have to select the train-test split %.")
|
183 |
train_size = st.slider("Select Train Dataset Size (%)", min_value=10, max_value=90, value=70)
|
184 |
state = "model selection"
|
185 |
-
else:
|
186 |
-
pass
|
187 |
|
188 |
if state == "model selection":
|
189 |
st.subheader("Step 4: Model Selection")
|
@@ -191,52 +174,36 @@ def step3_page():
|
|
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
|
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
|
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 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
#
|
225 |
-
# # Display feature importance
|
226 |
-
# if st.sidebar.checkbox('Show feature importance'):
|
227 |
-
# feature_names = ['Solar_Irradiance', 'Temperature', 'Rain_Fall', 'Wind_speed', 'PV_Output_lag',
|
228 |
-
# 'PV_Output_mean']
|
229 |
-
# fig = feature_importance_plot(model, feature_names)
|
230 |
-
# with _lock:
|
231 |
-
# st.pyplot(fig)
|
232 |
-
#
|
233 |
-
# fig = show_output(y_test, y_pred)
|
234 |
-
#
|
235 |
# download_link(y_test, y_pred)
|
236 |
#
|
237 |
# download_plot(fig)
|
238 |
|
239 |
-
|
240 |
if st.button("Complete"):
|
241 |
complete_level(LEVEL)
|
242 |
|
|
|
30 |
return data
|
31 |
|
32 |
|
33 |
+
def model_train(train_X, train_y, model_choice, train_size):
|
34 |
if model_choice == 'LightGBM':
|
35 |
+
model = lgb.LGBMRegressor()
|
36 |
elif model_choice == 'Random Forest':
|
37 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
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 |
|
44 |
def create_model_inputs(data, lag, mean_period):
|
45 |
df_processed = data.copy()
|
|
|
161 |
with cols[1]:
|
162 |
st.info("Let's display our Target variable")
|
163 |
st.dataframe(y)
|
|
|
|
|
164 |
|
165 |
if state == "splitting":
|
166 |
st.subheader("Step 3: Data Splitting")
|
167 |
st.write("Now let's split the dataset into training and testing sets. The training set is used to train the machine learning model, and the testing set is used to evaluate its performance. For that, you have to select the train-test split %.")
|
168 |
train_size = st.slider("Select Train Dataset Size (%)", min_value=10, max_value=90, value=70)
|
169 |
state = "model selection"
|
|
|
|
|
170 |
|
171 |
if state == "model selection":
|
172 |
st.subheader("Step 4: Model Selection")
|
|
|
174 |
models = ['LightGBM', 'Random Forest']
|
175 |
model_choice = st.selectbox('Choose Model', models)
|
176 |
state = "model training"
|
|
|
|
|
177 |
|
178 |
if state == "model training":
|
179 |
st.subheader("Step 5: Model Training")
|
180 |
+
st.write("Finally, let's train our weather forecasting model based on the parameters that you have selected!")
|
|
|
|
|
181 |
if st.button("Train", key="train"):
|
182 |
+
model, X_test, y_test = model_train(X, y, model_choice, train_size)
|
183 |
my_bar = st.progress(0, text="Training model...")
|
184 |
for i in range(100):
|
185 |
my_bar.progress(i, text="Training model...")
|
186 |
my_bar.progress(100, text="Training completed")
|
187 |
state = "model predict"
|
188 |
+
st.success("Model training successfully completed!")
|
189 |
+
|
190 |
+
# Display feature importance
|
191 |
+
if st.checkbox('Show feature importance'):
|
192 |
+
feature_names = ['Solar_Irradiance', 'Temperature', 'Rain_Fall', 'Wind_speed', 'PV_Output_lag',
|
193 |
+
'PV_Output_mean']
|
194 |
+
fig = feature_importance_plot(model, feature_names)
|
195 |
+
with _lock:
|
196 |
+
st.pyplot(fig)
|
197 |
+
|
198 |
+
if state == "model predict":
|
199 |
+
st.subheader("Step 5: Model Evaluation")
|
200 |
+
st.write("Now, let's evaluate our weather forecasting model's performance against the test data set.")
|
201 |
+
y_pred = model.predict(X_test)
|
202 |
+
fig = show_output(y_test, y_pred)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
# download_link(y_test, y_pred)
|
204 |
#
|
205 |
# download_plot(fig)
|
206 |
|
|
|
207 |
if st.button("Complete"):
|
208 |
complete_level(LEVEL)
|
209 |
|