def test_transform_dataframe(self): x = pandas.DataFrame(data=[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) x.columns = "X1 X2".split() content = self.get_onnx_mul() tr = OnnxTransformer(content) tr.fit() try: tr.transform(x) except RuntimeError: pass
def test_pipeline_iris_change_dim(self): iris = load_iris() X, y = iris.data, iris.target pipe = make_pipeline(PCA(n_components=2), LogisticRegression()) pipe.fit(X, y) onx = convert_sklearn(pipe, initial_types=[ ('input', FloatTensorType((None, X.shape[1])))]) tr = OnnxTransformer(onx, change_batch_size=2, runtime='onnxruntime1') tr.fit(X) self.assertRaise(lambda: tr.transform(X), OrtInvalidArgument) y = tr.transform(X[:2]) self.assertEqual(len(y.shape), 2) self.assertEqual(y.shape[0], 2)
def test_transform_dict(self): x = {'X': np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])} content = self.get_onnx_mul() tr = OnnxTransformer(content) tr.fit() res = tr.transform(x) exp = np.array([[1., 4.], [9., 16.], [25., 36.]], dtype=np.float32) self.assertEqual(list(res.ravel()), list(exp.ravel()))
def test_transform_numpy(self): x = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=np.float32) content = self.get_onnx_mul() tr = OnnxTransformer(content) tr.fit() res = tr.transform(x) exp = np.array([[1., 4.], [9., 16.], [25., 36.]], dtype=np.float32) self.assertEqual(list(res.ravel()), list(exp.ravel())) rp = repr(tr) self.assertStartsWith("OnnxTransformer(onnx_bytes=b'\\", rp) self.assertEndsWith("')", rp)
def test_pipeline_iris_intermediate(self): iris = load_iris() X, y = iris.data, iris.target pipe = make_pipeline(PCA(n_components=2), LogisticRegression()) pipe.fit(X, y) onx = convert_sklearn(pipe, initial_types=[ ('input', FloatTensorType((None, X.shape[1])))]) tr = OnnxTransformer(onx, output_name="probabilities", reshape=True) tr.fit(X) y = tr.transform(X[:2]) self.assertEqual(len(y.shape), 2) self.assertEqual(y.shape[0], 2)
def test_grid_search(self): iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y) pca = PCA(n_components=2) pca.fit(X_train) onx = convert_sklearn(pca, initial_types=[('input', FloatTensorType( (1, X.shape[1])))]) onx_bytes = onx.SerializeToString() tr = OnnxTransformer(onx_bytes) pipe = make_pipeline(tr, LogisticRegression(solver='liblinear')) param_grid = [{'logisticregression__penalty': ['l2', 'l1']}] clf = GridSearchCV(pipe, param_grid, cv=3) clf.fit(X_train, y_train) bp = clf.best_params_ self.assertIn(bp, ({ 'logisticregression__penalty': 'l1' }, { 'logisticregression__penalty': 'l2' })) tr2 = OnnxTransformer(onx_bytes) tr2.fit() self.assertEqualArray( tr2.transform(X_test), clf.best_estimator_.steps[0][1].transform(X_test)) y_true, y_pred = y_test, clf.predict(X_test) cl = classification_report(y_true, y_pred) self.assertIn('precision', cl) sc = clf.score(X_test, y_test) self.assertGreater(sc, 0.70)