Delete Random Forest Model

parent 2408f853
# Feeding the data into the algorithm
rf_model = RandomForestRegressor(n_estimators=100, max_depth=20)
rf_model.fit(x_train, y_train)
rf_pred = rf_model.predict(x_test)
# Transform the prediction back to it's original state
rf_pred = rf_pred.reshape(-1,1)
rf_pred_test_set = np.concatenate([rf_pred, x_test], axis=1)
rf_pred_test_set = scaler.inverse_transform(rf_pred_test_set)
# calculating the predicted exports values from the difference values and append that to the predicted data frame
result_list = []
for index in range(0, len(rf_pred_test_set)):
result_list.append(rf_pred_test_set[index][0] + act_exports[index])
rf_pred_series = pd.Series(result_list, name='rf_pred')
predict_df = predict_df.merge(rf_pred_series, left_index=True, right_index=True)
# Now that we have calculated the predicted data and now we can evaluate it with varius matrix to test it's accuracy
rf_rmse = np.sqrt(mean_squared_error(predict_df['rf_pred'], monthly_exports['Quantity'][-12:]))
rf_mae = mean_absolute_error(predict_df['rf_pred'], monthly_exports['Quantity'][-12:])
rf_r2 = r2_score(predict_df['rf_pred'], monthly_exports['Quantity'][-12:])
print('Random Forest RMSE: ', rf_rmse)
print('Random Forest MAE: ', rf_mae)
print('Random Forest R2 Score: ', rf_r2)
# Visualising the data with the original values
plt.figure(figsize=(15,7))
plt.plot(monthly_exports['Date'], monthly_exports['Quantity'])
plt.plot(predict_df['Date'], predict_df['rf_pred'])
plt.title("Export Forecast using Random Forest")
plt.xlabel("Date")
plt.ylabel("Exports")
plt.legend(["Original exports", "Predicted exports"])
plt.show()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment