-
-
Notifications
You must be signed in to change notification settings - Fork 43.9k
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
Create xgboost_classifier_custom.py #11244
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
from sklearn.tree import DecisionTreeClassifier | ||
|
||
class CustomXGBoostClassifier: | ||
def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3): | ||
self.n_estimators = n_estimators | ||
self.learning_rate = learning_rate | ||
self.max_depth = max_depth | ||
self.trees = [] | ||
|
||
def fit(self, x, y): | ||
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
n_samples, n_features = x.shape | ||
y = np.where(y == 0, -1, 1) # Convert 0/1 labels to -1/1 | ||
|
||
predictions = np.zeros(n_samples) | ||
|
||
for _ in range(self.n_estimators): | ||
residual = y - predictions | ||
tree = DecisionTreeClassifier(max_depth=self.max_depth) | ||
tree.fit(x, residual) | ||
tree_predictions = tree.predict(x) | ||
predictions += self.learning_rate * tree_predictions | ||
self.trees.append(tree) | ||
|
||
def predict(self, x): | ||
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
result = np.zeros(x.shape[0]) | ||
|
||
for tree in self.trees: | ||
result += self.learning_rate * tree.predict(x) | ||
|
||
return np.where(result >= 0, 1, 0) | ||
|
||
# Example Usage: | ||
# clf = CustomXGBoostClassifier() | ||
# clf.fit(X_train, y_train) | ||
# predictions = clf.predict(X_test) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide type hint for the parameter:
n_estimators
Please provide type hint for the parameter:
learning_rate
Please provide type hint for the parameter:
max_depth