|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +from nimbusml import Pipeline |
| 6 | +from nimbusml.preprocessing import OnnxRunner |
| 7 | +from nimbusml.preprocessing.normalization import MinMaxScaler |
| 8 | + |
| 9 | + |
| 10 | +def get_tmp_file(suffix=None): |
| 11 | + fd, file_name = tempfile.mkstemp(suffix=suffix) |
| 12 | + fl = os.fdopen(fd, 'w') |
| 13 | + fl.close() |
| 14 | + return file_name |
| 15 | + |
| 16 | +# Generate the train and test data |
| 17 | +np.random.seed(0) |
| 18 | +x = np.arange(100, step=0.1) |
| 19 | +y = x * 10 + (np.random.standard_normal(len(x)) * 10) |
| 20 | +train_data = {'c1': x, 'c2': y} |
| 21 | +train_df = pd.DataFrame(train_data).astype({'c1': np.float32, 'c2': np.float32}) |
| 22 | + |
| 23 | +test_data = {'c1': [2.5, 30.5], 'c2': [1, 1]} |
| 24 | +test_df = pd.DataFrame(test_data).astype({'c1': np.float32, 'c2': np.float32}) |
| 25 | + |
| 26 | +# Fit a MinMaxScaler Pipeline |
| 27 | +r1 = Pipeline([MinMaxScaler()]) |
| 28 | +r1.fit(train_df) |
| 29 | + |
| 30 | +# Export the pipeline to ONNX |
| 31 | +onnx_path = get_tmp_file('.onnx') |
| 32 | +r1.export_to_onnx(onnx_path, 'com.microsoft.ml', onnx_version='Stable') |
| 33 | + |
| 34 | +# Perform the transform using the standard ML.Net backend |
| 35 | +result_standard = r1.transform(test_df) |
| 36 | +print(result_standard) |
| 37 | +# c1 c2 |
| 38 | +# 0 0.025025 0.000998 |
| 39 | +# 1 0.305305 0.000998 |
| 40 | + |
| 41 | +# Perform the transform using the ONNX backend. |
| 42 | +# Note, the extra columns and column name differences |
| 43 | +# is a known issue with the ML.Net backend. |
| 44 | +onnxrunner = OnnxRunner(model_file=onnx_path) |
| 45 | +result_onnx = onnxrunner.fit_transform(test_df) |
| 46 | +print(result_onnx) |
| 47 | +# c1 c2 c12.0 c22.0 |
| 48 | +# 0 2.5 1.0 0.025025 0.000998 |
| 49 | +# 1 30.5 1.0 0.305305 0.000998 |
0 commit comments