Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions machine_learning/xgboost_classifier_custom.py
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:

Check failure on line 4 in machine_learning/xgboost_classifier_custom.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/xgboost_classifier_custom.py:1:1: I001 Import block is un-sorted or un-formatted
def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):

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

self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.max_depth = max_depth
self.trees = []

def fit(self, x, y):

Choose a reason for hiding this comment

The 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 machine_learning/xgboost_classifier_custom.py, please provide doctest for the function fit

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

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):

Choose a reason for hiding this comment

The 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 machine_learning/xgboost_classifier_custom.py, please provide doctest for the function predict

Please provide return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

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)