@@ -341,6 +341,27 @@ def test_xgb_best_tree_limit(self):
341341 assert_almost_equal (bst_loaded .predict (dtest , output_margin = True ), res [1 ], decimal = 5 )
342342 assert_almost_equal (bst_loaded .predict (dtest ), res [0 ])
343343
344+ def test_onnxrt_python_xgbclassifier (self ):
345+ x = np .random .randn (100 , 10 ).astype (np .float32 )
346+ y = ((x .sum (axis = 1 ) + np .random .randn (x .shape [0 ]) / 50 + 0.5 ) >= 0 ).astype (np .int64 )
347+ x_train , x_test , y_train , y_test = train_test_split (x , y )
348+ bmy = np .mean (y_train )
349+
350+ for bm , n_est in [(None , 1 ), (None , 3 ), (bmy , 1 ), (bmy , 3 )]:
351+ model_skl = XGBClassifier (n_estimators = n_est ,
352+ learning_rate = 0.01 ,
353+ subsample = 0.5 , objective = "binary:logistic" ,
354+ base_score = bm , max_depth = 2 )
355+ model_skl .fit (x_train , y_train , eval_set = [(x_test , y_test )], verbose = 0 )
356+
357+ model_onnx_skl = convert_xgboost (
358+ model_skl , initial_types = [('X' , FloatTensorType ([None , x .shape [1 ]]))],
359+ target_opset = TARGET_OPSET )
360+ with self .subTest (base_score = bm , n_estimators = n_est ):
361+ oinf = InferenceSession (model_onnx_skl .SerializeToString ())
362+ res2 = oinf .run (None , {'X' : x_test })
363+ assert_almost_equal (model_skl .predict_proba (x_test ), res2 [1 ])
364+
344365
345366if __name__ == "__main__" :
346367 unittest .main ()
0 commit comments