Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit fa905e5

Browse files
piethsganik
authored andcommitted
Add example for OnnxRunner. (#422)
1 parent 08a501a commit fa905e5

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/python/nimbusml.pyproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="nimbusml\examples\BootStrapSample.py" />
9393
<Compile Include="nimbusml\examples\CharTokenizer.py" />
9494
<Compile Include="nimbusml\examples\ColumnConcatenator.py" />
95+
<Compile Include="nimbusml\examples\examples_from_dataframe\OnnxRunner_df.py" />
9596
<Compile Include="nimbusml\examples\examples_from_dataframe\RobustScaler_df.py" />
9697
<Compile Include="nimbusml\examples\examples_from_dataframe\TimeSeriesImputer_df.py" />
9798
<Compile Include="nimbusml\examples\examples_from_dataframe\VotingRegressor.py" />
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)