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

[ENH] Support customized base estimator and predictor #48

Merged
merged 21 commits into from
Mar 11, 2021
Merged

Conversation

xuyxu
Copy link
Member

@xuyxu xuyxu commented Mar 3, 2021

resolves #29 #26

Steps

  • Implement K-Fold wrapper for base estimators
  • Implement customized cascade layer
  • Implement set_estimator and set_predictor for the model
  • Add unit tests
  • Add backward compatibility
  • Add documentation and working examples

Code Snippet

from deepforest import CascadeForestClassifier

model = CascadeForestClassifier()

# New Steps
estimator_1, estimator_2 = your_estimator(), your_estimator()
model.set_estimator(estimator=[estimator_1, estimator_2],  # a list of your base estimators
                    n_splits=5,  # the number of folds
                    oob_approx=False,  # whether to use out-of-bag approximation
                    random_state=None)  # random state used for base estimators

model.set_predictor(predictor=your_predictor)  # an instantiated object of your predictor

model.fit(X_train, y_train)
y_pred = model.predict(X_test)

@xuyxu xuyxu added the feature request New feature or request label Mar 3, 2021
@xuyxu
Copy link
Member Author

xuyxu commented Mar 6, 2021

Hi @Maryom, this PR implements the feature request on custom base estimators. If you want to use XGBClassifier as the base estimators, below is the example code:

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from deepforest import CascadeForestClassifier
from xgboost import XGBClassifier

X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
model = CascadeForestClassifier(random_state=1)

# New Steps
estimators = [XGBClassifier() for _ in range(4)]  # 4 base estimators per cascade layer
model.set_estimator(estimators)

model.fit(X_train, y_train)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred) * 100
print("\nTesting Accuracy: {:.3f} %".format(acc))

You can find the wheels for installation here .Feel free to comment below if you have any problem or suggestion when using this feature.

Copy link
Member Author

@xuyxu xuyxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@xuyxu xuyxu merged commit 7777f99 into master Mar 11, 2021
@xuyxu xuyxu deleted the custom_estimator branch March 20, 2021 04:45
@Maryom
Copy link

Maryom commented Mar 24, 2021

Hi @xuyxu I'm really sorry for not being responsive I was sick. Thank you so much for you awesome work 🙏🏼 it is really handy 👌🏼

@kangwenhao
Copy link

徐老师,您好!我仿照您的代码段,自定义了xgboost的回归模型,但是一直报错,我有点不知道是为什么?我使用原版深度森林时,没有这样的错误。
image
image

@kangwenhao
Copy link

下面是正常时的代码
image

@xuyxu
Copy link
Member Author

xuyxu commented Apr 12, 2021

Thanks for reporting @kangwenhao, please refer to #67 for details.

@kangwenhao
Copy link

Thank you for your help. Good luck!

@kangwenhao
Copy link

kangwenhao commented Apr 13, 2021

Teacher Xu, hello.Now I can customize one estimator, but if I want to customize two, there will be a problem,
image

@xuyxu
Copy link
Member Author

xuyxu commented Apr 13, 2021

Hi @kangwenhao, please refer to the API Reference for an introduction on set_estimator. For example, the name of the first input parameter should be estimators instead of estimator.

@yunwezhang yunwezhang mentioned this pull request Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic cascade structure
3 participants