-
-
Notifications
You must be signed in to change notification settings - Fork 50.7k
Added Random Forest Regressor as an additional prediction model. #12767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
priyanshu-8789
wants to merge
14
commits into
TheAlgorithms:master
Choose a base branch
from
priyanshu-8789:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0f1a8b4
Added Random Forest Regressor as an additional prediction model.
priyanshu-8789 f2793d3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 67b1d80
Added Random Forest Regressor to main voting
priyanshu-8789 7ac28e1
Merge branches 'master' and 'master' of https://github.com/priyanshu-…
priyanshu-8789 4ad2464
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 691d98d
Update run.py
priyanshu-8789 062e046
Update run.py
priyanshu-8789 6c2f7b4
Update run.py
priyanshu-8789 c8df2cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 39bc446
Update run.py
priyanshu-8789 f882dfc
Update run.py
priyanshu-8789 c7279bd
Merge branch 'master' into master
poyea 401ee4f
Merge branch 'master' into master
poyea 4effc1d
updating DIRECTORY.md
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,10 @@ | |
|
|
||
| from warnings import simplefilter | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| import pandas as pd | ||
| from sklearn.ensemble import RandomForestRegressor | ||
| from sklearn.preprocessing import Normalizer | ||
| from sklearn.svm import SVR | ||
| from statsmodels.tsa.statespace.sarimax import SARIMAX | ||
|
|
@@ -78,6 +80,29 @@ def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> f | |
| return float(y_pred[0]) | ||
|
|
||
|
|
||
| def random_forest_regressor(x_train: list, x_test: list, train_user: list) -> float: | ||
| """ | ||
| Fourth method: Random Forest Regressor | ||
| Random Forest is an ensemble learning method for regression that operates | ||
| by constructing a multitude of decision trees at training time and outputting | ||
| the mean prediction of the individual trees. | ||
|
|
||
| It is more robust than a single decision tree and less prone to overfitting. | ||
| Good for capturing nonlinear relationships in data. | ||
|
|
||
| input : training data (date, total_event) in list of float | ||
| where x = list of set (date and total event) | ||
| output : list of total user prediction in float | ||
|
|
||
| >>> random_forest_regressor([[5,2],[1,5],[6,2]], [[3,2]], [2,1,4]) | ||
| 1.95 | ||
| """ | ||
| model = RandomForestRegressor(n_estimators=100, random_state=42) | ||
| model.fit(x_train, train_user) | ||
| prediction = model.predict(x_test) | ||
| return float(prediction[0]) | ||
|
poyea marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def interquartile_range_checker(train_user: list) -> float: | ||
| """ | ||
| Optional method: interquatile range | ||
|
|
@@ -120,6 +145,22 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool: | |
| return safe > not_safe | ||
|
|
||
|
|
||
| def plot_forecast(actual, predictions): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type hints required |
||
| plt.figure(figsize=(10, 5)) | ||
| plt.plot(range(len(actual)), actual, label="Actual") | ||
| plt.plot(len(actual), predictions[0], "ro", label="Linear Reg") | ||
| plt.plot(len(actual), predictions[1], "go", label="SARIMAX") | ||
|
poyea marked this conversation as resolved.
|
||
| plt.plot(len(actual), predictions[2], "bo", label="SVR") | ||
| plt.plot(len(actual), predictions[3], "yo", label="RF") | ||
| plt.legend() | ||
| plt.title("Data Safety Forecast") | ||
| plt.xlabel("Days") | ||
| plt.ylabel("Normalized User Count") | ||
| plt.grid(True) | ||
| plt.tight_layout() | ||
| plt.show() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| """ | ||
| data column = total user in a day, how much online event held in one day, | ||
|
|
@@ -155,8 +196,11 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool: | |
| ), | ||
| sarimax_predictor(train_user, train_match, test_match), | ||
| support_vector_regressor(x_train, x_test, train_user), | ||
| random_forest_regressor(x_train, x_test, train_user), | ||
| ] | ||
|
|
||
| # check the safety of today's data | ||
| not_str = "" if data_safety_checker(res_vote, test_user[0]) else "not " | ||
| print(f"Today's data is {not_str}safe.") | ||
|
|
||
| plot_forecast(train_user, res_vote) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.