|
3 | 3 | """ |
4 | 4 | import sys |
5 | 5 | import unittest |
6 | | -from sklearn.datasets import load_iris |
| 6 | +import numpy as np |
| 7 | +from sklearn.datasets import load_diabetes, load_iris, make_classification |
| 8 | +from sklearn.model_selection import train_test_split |
7 | 9 | from xgboost import XGBRegressor, XGBClassifier |
8 | 10 | from onnxmltools.convert import convert_xgboost |
9 | 11 | from onnxmltools.convert.common.data_types import FloatTensorType |
10 | | -from onnxmltools.utils import dump_multiple_classification, dump_single_regression, dump_binary_classification |
| 12 | +from onnxmltools.utils import dump_data_and_model |
| 13 | + |
| 14 | + |
| 15 | +def _fit_classification_model(model, n_classes, is_str=False): |
| 16 | + x, y = make_classification(n_classes=n_classes, n_features=100, |
| 17 | + n_samples=1000, |
| 18 | + random_state=42, n_informative=7) |
| 19 | + y = y.astype(np.str) if is_str else y.astype(np.int64) |
| 20 | + x_train, x_test, y_train, _ = train_test_split(x, y, test_size=0.5, |
| 21 | + random_state=42) |
| 22 | + model.fit(x_train, y_train) |
| 23 | + return model, x_test.astype(np.float32) |
11 | 24 |
|
12 | 25 |
|
13 | 26 | class TestXGBoostModels(unittest.TestCase): |
14 | 27 |
|
15 | | - @unittest.skipIf(sys.version_info[0] == 2, reason="xgboost converter not tested on python 2") |
| 28 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 29 | + reason="xgboost converter not tested on python 2") |
16 | 30 | def test_xgb_regressor(self): |
17 | | - iris = load_iris() |
18 | | - X = iris.data[:, :2] |
| 31 | + iris = load_diabetes() |
| 32 | + x = iris.data |
19 | 33 | y = iris.target |
20 | | - |
| 34 | + x_train, x_test, y_train, _ = train_test_split(x, y, test_size=0.5, |
| 35 | + random_state=42) |
21 | 36 | xgb = XGBRegressor() |
22 | | - xgb.fit(X, y) |
23 | | - conv_model = convert_xgboost(xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
| 37 | + xgb.fit(x_train, y_train) |
| 38 | + conv_model = convert_xgboost( |
| 39 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
24 | 40 | self.assertTrue(conv_model is not None) |
25 | | - dump_single_regression(xgb, suffix="-Dec4") |
| 41 | + dump_data_and_model( |
| 42 | + x_test.astype("float32"), |
| 43 | + xgb, |
| 44 | + conv_model, |
| 45 | + basename="SklearnXGBRegressor-Dec4", |
| 46 | + allow_failure="StrictVersion(" |
| 47 | + "onnx.__version__)" |
| 48 | + "< StrictVersion('1.3.0')", |
| 49 | + ) |
26 | 50 |
|
27 | | - @unittest.skipIf(sys.version_info[0] == 2, reason="xgboost converter not tested on python 2") |
| 51 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 52 | + reason="xgboost converter not tested on python 2") |
28 | 53 | def test_xgb_classifier(self): |
29 | | - iris = load_iris() |
30 | | - X = iris.data[:, :2] |
31 | | - y = iris.target |
32 | | - y[y == 2] = 0 |
33 | | - |
34 | | - xgb = XGBClassifier() |
35 | | - xgb.fit(X, y) |
36 | | - conv_model = convert_xgboost(xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
| 54 | + xgb, x_test = _fit_classification_model(XGBClassifier(), 2) |
| 55 | + conv_model = convert_xgboost( |
| 56 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
37 | 57 | self.assertTrue(conv_model is not None) |
38 | | - dump_binary_classification(xgb) |
| 58 | + dump_data_and_model( |
| 59 | + x_test, |
| 60 | + xgb, |
| 61 | + conv_model, |
| 62 | + basename="SklearnXGBClassifier", |
| 63 | + allow_failure="StrictVersion(" |
| 64 | + "onnx.__version__)" |
| 65 | + "< StrictVersion('1.3.0')", |
| 66 | + ) |
39 | 67 |
|
40 | | - @unittest.skipIf(sys.version_info[0] == 2, reason="xgboost converter not tested on python 2") |
| 68 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 69 | + reason="xgboost converter not tested on python 2") |
41 | 70 | def test_xgb_classifier_multi(self): |
42 | | - iris = load_iris() |
43 | | - X = iris.data[:, :2] |
44 | | - y = iris.target |
45 | | - |
46 | | - xgb = XGBClassifier() |
47 | | - xgb.fit(X, y) |
48 | | - conv_model = convert_xgboost(xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
| 71 | + xgb, x_test = _fit_classification_model(XGBClassifier(), 3) |
| 72 | + conv_model = convert_xgboost( |
| 73 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
49 | 74 | self.assertTrue(conv_model is not None) |
50 | | - dump_multiple_classification(xgb, allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')") |
| 75 | + dump_data_and_model( |
| 76 | + x_test, |
| 77 | + xgb, |
| 78 | + conv_model, |
| 79 | + basename="SklearnXGBClassifierMulti", |
| 80 | + allow_failure="StrictVersion(" |
| 81 | + "onnx.__version__)" |
| 82 | + "< StrictVersion('1.3.0')", |
| 83 | + ) |
51 | 84 |
|
52 | | - @unittest.skipIf(sys.version_info[0] == 2, reason="xgboost converter not tested on python 2") |
| 85 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 86 | + reason="xgboost converter not tested on python 2") |
53 | 87 | def test_xgb_classifier_multi_reglog(self): |
54 | | - iris = load_iris() |
55 | | - X = iris.data[:, :2] |
56 | | - y = iris.target |
57 | | - |
58 | | - xgb = XGBClassifier(objective='reg:logistic') |
59 | | - xgb.fit(X, y) |
60 | | - conv_model = convert_xgboost(xgb, initial_types=[('input', FloatTensorType(shape=[1, 2]))]) |
| 88 | + xgb, x_test = _fit_classification_model( |
| 89 | + XGBClassifier(objective='reg:logistic'), 4) |
| 90 | + conv_model = convert_xgboost( |
| 91 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 2]))]) |
61 | 92 | self.assertTrue(conv_model is not None) |
62 | | - dump_multiple_classification(xgb, suffix="RegLog", |
63 | | - allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')") |
| 93 | + dump_data_and_model( |
| 94 | + x_test, |
| 95 | + xgb, |
| 96 | + conv_model, |
| 97 | + basename="SklearnXGBClassifierMultiRegLog", |
| 98 | + allow_failure="StrictVersion(" |
| 99 | + "onnx.__version__)" |
| 100 | + "< StrictVersion('1.3.0')", |
| 101 | + ) |
64 | 102 |
|
65 | | - @unittest.skipIf(sys.version_info[0] == 2, reason="xgboost converter not tested on python 2") |
| 103 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 104 | + reason="xgboost converter not tested on python 2") |
66 | 105 | def test_xgb_classifier_reglog(self): |
| 106 | + xgb, x_test = _fit_classification_model( |
| 107 | + XGBClassifier(objective='reg:logistic'), 2) |
| 108 | + conv_model = convert_xgboost( |
| 109 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 2]))]) |
| 110 | + self.assertTrue(conv_model is not None) |
| 111 | + dump_data_and_model( |
| 112 | + x_test, |
| 113 | + xgb, |
| 114 | + conv_model, |
| 115 | + basename="SklearnXGBClassifierRegLog", |
| 116 | + allow_failure="StrictVersion(" |
| 117 | + "onnx.__version__)" |
| 118 | + "< StrictVersion('1.3.0')", |
| 119 | + ) |
| 120 | + |
| 121 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 122 | + reason="xgboost converter not tested on python 2") |
| 123 | + def test_xgb_classifier_multi_str_labels(self): |
| 124 | + xgb, x_test = _fit_classification_model( |
| 125 | + XGBClassifier(n_estimators=4), 5, is_str=True) |
| 126 | + conv_model = convert_xgboost( |
| 127 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
| 128 | + self.assertTrue(conv_model is not None) |
| 129 | + dump_data_and_model( |
| 130 | + x_test, |
| 131 | + xgb, |
| 132 | + conv_model, |
| 133 | + basename="SklearnXGBClassifierMultiStrLabels", |
| 134 | + allow_failure="StrictVersion(" |
| 135 | + "onnx.__version__)" |
| 136 | + "< StrictVersion('1.3.0')", |
| 137 | + ) |
| 138 | + |
| 139 | + @unittest.skipIf(sys.version_info[0] == 2, |
| 140 | + reason="xgboost converter not tested on python 2") |
| 141 | + def test_xgb_classifier_multi_discrete_int_labels(self): |
67 | 142 | iris = load_iris() |
68 | | - X = iris.data[:, :2] |
| 143 | + x = iris.data[:, :2] |
69 | 144 | y = iris.target |
70 | | - y[y == 2] = 0 |
71 | | - |
72 | | - xgb = XGBClassifier(objective='reg:logistic') |
73 | | - xgb.fit(X, y) |
74 | | - conv_model = convert_xgboost(xgb, initial_types=[('input', FloatTensorType(shape=[1, 2]))]) |
| 145 | + y[y == 0] = 10 |
| 146 | + y[y == 1] = 20 |
| 147 | + y[y == 2] = -30 |
| 148 | + x_train, x_test, y_train, _ = train_test_split(x, |
| 149 | + y, |
| 150 | + test_size=0.5, |
| 151 | + random_state=42) |
| 152 | + xgb = XGBClassifier(n_estimators=3) |
| 153 | + xgb.fit(x_train, y_train) |
| 154 | + conv_model = convert_xgboost( |
| 155 | + xgb, initial_types=[('input', FloatTensorType(shape=[1, 'None']))]) |
75 | 156 | self.assertTrue(conv_model is not None) |
76 | | - dump_binary_classification(xgb, suffix="RegLog") |
| 157 | + dump_data_and_model( |
| 158 | + x_test.astype("float32"), |
| 159 | + xgb, |
| 160 | + conv_model, |
| 161 | + basename="SklearnXGBClassifierMultiDiscreteIntLabels", |
| 162 | + allow_failure="StrictVersion(" |
| 163 | + "onnx.__version__)" |
| 164 | + "< StrictVersion('1.3.0')", |
| 165 | + ) |
77 | 166 |
|
78 | 167 |
|
79 | 168 | if __name__ == "__main__": |
|
0 commit comments