-
Notifications
You must be signed in to change notification settings - Fork 174
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
Add test for xgboost modelbuiler #1359
Merged
avolkov-intel
merged 9 commits into
intel:master
from
avolkov-intel:dev/xgb-mb-earlystop-test
Jul 27, 2023
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bbd3839
Add test
avolkov-intel 87918cd
Merge master
avolkov-intel deb862e
Add test and remove example for xgboost early stop
avolkov-intel 4ccd0ba
Change xgboost modelbuilder interface
avolkov-intel 0a792a7
Change objective in test
avolkov-intel a02ef8a
Move xgboost import inside the test to avoid failures
avolkov-intel 2dc3694
Fix typo
avolkov-intel 3f0a3e3
Merge branch 'master' into dev/xgb-mb-earlystop-test
avolkov-intel eed2ca3
Formatting
avolkov-intel 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 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,87 @@ | ||
#=============================================================================== | ||
# Copyright 2023 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#=============================================================================== | ||
|
||
import unittest | ||
import importlib.util | ||
import daal4py as d4p | ||
import xgboost as xgb | ||
import numpy as np | ||
from sklearn.datasets import make_classification | ||
from sklearn.model_selection import train_test_split | ||
from daal4py.sklearn._utils import daal_check_version | ||
|
||
from daal4py import _get__daal_link_version__ as dv | ||
# First item is major version - 2021, | ||
# second is minor+patch - 0110, | ||
# third item is status - B | ||
daal_version = (int(dv()[0:4]), dv()[10:11], int(dv()[4:8])) | ||
reason = str(((2021, 'P', 1))) + " not supported in this library version " | ||
reason += str(daal_version) | ||
|
||
|
||
class XgboostModelBuilder(unittest.TestCase): | ||
@unittest.skipUnless(all([ | ||
hasattr(d4p, 'get_gbt_model_from_xgboost'), | ||
hasattr(d4p, 'gbt_classification_prediction'), | ||
daal_check_version(((2021, 'P', 1)))]), reason) | ||
@unittest.skipUnless(importlib.util.find_spec('xgboost') | ||
is not None, 'xgoost library is not installed') | ||
def test_earlystop(self): | ||
num_classes = 3 | ||
X, y = make_classification(n_samples=1000, | ||
n_features=10, | ||
n_informative=3, | ||
n_classes=num_classes, | ||
random_state=42) | ||
X_train, X_test, y_train, y_test = train_test_split( | ||
X, y, test_size=0.3, random_state=42) | ||
|
||
# training parameters setting | ||
params = { | ||
'n_estimators': 100, | ||
'max_bin': 256, | ||
'scale_pos_weight': 2, | ||
'lambda_l2': 1, | ||
'alpha': 0.9, | ||
'max_depth': 8, | ||
'num_leaves': 2**8, | ||
'verbosity': 0, | ||
'objective': 'multi:softproba', | ||
'learning_rate': 0.3, | ||
'num_class': num_classes, | ||
'early_stopping_rounds': 5 | ||
} | ||
|
||
xgb_clf = xgb.XGBClassifier(**params) | ||
xgb_clf.fit(X_train, y_train, eval_set=[(X_test, y_test)]) | ||
booster = xgb_clf.get_booster() | ||
|
||
xgb_prediction = xgb_clf.predict(X_test) | ||
xgb_proba = xgb_clf.predict_proba(X_test) | ||
xgb_errors_count = np.count_nonzero(xgb_prediction - np.ravel(y_test)) | ||
|
||
daal_model = d4p.mb.convert_model(booster) | ||
|
||
daal_prediction = daal_model.predict(X_test) | ||
daal_proba = daal_model.predict_proba(X_test) | ||
daal_errors_count = np.count_nonzero(daal_prediction - np.ravel(y_test)) | ||
|
||
self.assertTrue(np.absolute(xgb_errors_count - daal_errors_count) == 0) | ||
self.assertTrue(np.allclose(xgb_proba, daal_proba)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
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.