Skip to content

Commit c3984d3

Browse files
priyanshu-8789pre-commit-ci[bot]poyeacclausspriya-sundaram-dev
authored
Added Random Forest Regressor as an additional prediction model. (#12767)
* Added Random Forest Regressor as an additional prediction model. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added Random Forest Regressor to main voting * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update run.py * Update run.py * Update run.py Used matplotlib to plot actual vs predicted user count, forecast confidence intervals, outlier thresholds from IQR. Added logging instead of print because in production, print() is not scalable. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update run.py * Update run.py * updating DIRECTORY.md * Apply suggestion from @cclauss * updating DIRECTORY.md * updating DIRECTORY.md * Apply batched suggestions from code review Co-authored-by: priya-sundaram-dev <oc-409d01@agentmail.to> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: John Law <johnlaw.po@gmail.com> Co-authored-by: poyea <poyea@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com> Co-authored-by: priya-sundaram-dev <oc-409d01@agentmail.to>
1 parent c27e951 commit c3984d3

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

  • machine_learning/forecasting

‎machine_learning/forecasting/run.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import numpy as np
1717
import pandas as pd
18+
from sklearn.ensemble import RandomForestRegressor
1819
from sklearn.preprocessing import Normalizer
1920
from sklearn.svm import SVR
2021
from statsmodels.tsa.statespace.sarimax import SARIMAX
@@ -78,6 +79,29 @@ def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> f
7879
return float(y_pred[0])
7980

8081

82+
def random_forest_regressor(x_train: list, x_test: list, train_user: list) -> float:
83+
"""
84+
Fourth method: Random Forest Regressor
85+
Random Forest is an ensemble learning method for regression that operates
86+
by constructing a multitude of decision trees at training time and outputting
87+
the mean prediction of the individual trees.
88+
89+
It is more robust than a single decision tree and less prone to overfitting.
90+
Good for capturing nonlinear relationships in data.
91+
92+
input : training data (date, total_event) in list of float
93+
where x = list of set (date and total event)
94+
output : list of total user prediction in float
95+
96+
>>> random_forest_regressor([[5,2],[1,5],[6,2]], [[3,2]], [2,1,4])
97+
1.95
98+
"""
99+
model = RandomForestRegressor(n_estimators=100, random_state=42)
100+
model.fit(x_train, train_user)
101+
prediction = model.predict(x_test)
102+
return float(prediction[0])
103+
104+
81105
def interquartile_range_checker(train_user: list) -> float:
82106
"""
83107
Optional method: interquatile range
@@ -155,6 +179,7 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool:
155179
),
156180
sarimax_predictor(train_user, train_match, test_match),
157181
support_vector_regressor(x_train, x_test, train_user),
182+
random_forest_regressor(x_train, x_test, train_user),
158183
]
159184

160185
# check the safety of today's data

0 commit comments

Comments
 (0)