-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
made random forrest classifier from the ground up. does not use sklearn #9770
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4f80ed6
made random forrest classifier from the ground up. does not use sklearn
Rajkanwars15 78fae70
added doctests to my random forest classifier
Rajkanwars15 2297d98
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c035855
fixed errors
Rajkanwars15 37ae599
fixed errors
Rajkanwars15 de5eedb
fixed errors
Rajkanwars15 cb42dd6
fixed errors
Rajkanwars15 750fbdb
fixed errors
Rajkanwars15 1e12e75
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] be27873
fixed errors
Rajkanwars15 dcb71b0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2ab8e3b
fixed errors after 37
Rajkanwars15 29385d3
fixed errors after 37
Rajkanwars15 9b35c92
fixed errors after 49
Rajkanwars15 68c378f
fixed errors after 55
Rajkanwars15 beb0e1e
fixed errors after 55-2
Rajkanwars15 5d257d5
fixed errors after 55-3
Rajkanwars15 ce678ae
fixed errors after 55-4
Rajkanwars15 978b44a
fixed errors after 55-5
Rajkanwars15 c8b2076
fixed errors in line 9
Rajkanwars15 5a958d0
fixed errors in line 9-2
Rajkanwars15 ca2b8e0
fixed errors in line 17
Rajkanwars15 d966c1f
fixed errors in line 1 and 2
Rajkanwars15 87533f6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import numpy as np | ||
from typing import Optional | ||
|
||
class DecisionTree: | ||
""" | ||
Decision Tree classifier. | ||
|
||
Parameters: | ||
max_depth (Optional[int]): Maximum depth of the tree. If None, the tree grows until pure nodes or min_samples_split is reached. | ||
|
||
Attributes: | ||
tree (tuple): The decision tree structure. | ||
""" | ||
|
||
def __init__(self, max_depth: Optional[int] = None) -> None: | ||
self.max_depth = max_depth | ||
|
||
def fit(self, features, labels) -> None: | ||
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Fit the decision tree to the training data. | ||
|
||
Parameters: | ||
features: The input features. | ||
labels: The target labels. | ||
|
||
Returns: | ||
None | ||
""" | ||
self.tree = self._build_tree(features, labels, depth=0) | ||
|
||
def _build_tree(self, features, labels, depth) -> tuple: | ||
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Recursively build the decision tree. | ||
|
||
Parameters: | ||
features: The input features. | ||
labels: The target labels. | ||
depth: The current depth of the tree. | ||
|
||
Returns: | ||
tuple: The decision tree structure. | ||
""" | ||
# Your existing _build_tree implementation | ||
|
||
def _calculate_gini(self, labels) -> float: | ||
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Calculate the Gini impurity for a given set of labels. | ||
|
||
Parameters: | ||
labels: A list of labels. | ||
|
||
Returns: | ||
float: The Gini impurity. | ||
""" | ||
# Your existing _calculate_gini implementation | ||
|
||
def predict(self, features) -> list: | ||
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Make predictions for input features. | ||
|
||
Parameters: | ||
features: The input features. | ||
|
||
Returns: | ||
list: Predicted labels. | ||
""" | ||
return [self._predict_tree(data_point, self.tree) for data_point in features] | ||
|
||
def _predict_tree(self, data_point, tree) -> int: | ||
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
Rajkanwars15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Recursively traverse the decision tree to make predictions. | ||
|
||
Parameters: | ||
data_point: Input features for a single data point. | ||
tree: The decision tree structure. | ||
|
||
Returns: | ||
int: Predicted label. | ||
""" | ||
# Your existing _predict_tree implementation | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.