Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pipeline(self):
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
data = numpy.array([[0, 0], [0, 0], [1, 1], [1, 1]], dtype=numpy.float32)
scaler = StandardScaler()
scaler.fit(data)
model = Pipeline([('scaler1',scaler), ('scaler2', scaler)])
model_onnx = convert_sklearn(model, 'pipeline', [('input', FloatTensorType([1, 2]))])
self.assertTrue(model_onnx is not None)
dump_data_and_model(data, model, model_onnx, basename="SklearnPipelineScaler")
def test_robust_scaler_floats_no_scaling(self):
model = RobustScaler(with_scaling=False)
data = [[0., 0., 3.], [1., 1., 0.], [0., 2., 1.], [1., 0., 2.]]
model.fit(data)
model_onnx = convert_sklearn(model, 'scaler', [('input', FloatTensorType([1, 3]))])
self.assertTrue(model_onnx is not None)
dump_data_and_model(numpy.array(data, dtype=numpy.float32),
model, basename="SklearnRobustScalerNoScalingFloat32")
def test_glm_regressor(self):
X, y = make_regression(n_features=4, random_state=0)
lr = LinearRegression()
lr.fit(X, y)
lr_coreml = coremltools.converters.sklearn.convert(lr)
lr_onnx = convert(lr_coreml.get_spec())
self.assertTrue(lr_onnx is not None)
dump_data_and_model(X.astype(numpy.float32), lr, lr_onnx, basename="CmlLinearRegression-Dec4")
svr = LinearSVR()
svr.fit(X, y)
svr_coreml = coremltools.converters.sklearn.convert(svr)
svr_onnx = convert(svr_coreml.get_spec())
self.assertTrue(svr_onnx is not None)
dump_data_and_model(X.astype(numpy.float32), svr, svr_onnx, basename="CmlLinearSvr-Dec4")
def test_model_binarizer(self):
model = Binarizer(threshold=0.5)
model_onnx = convert_sklearn(model, 'scikit-learn binarizer', [('input', FloatTensorType([1, 1]))])
self.assertTrue(model_onnx is not None)
dump_data_and_model(numpy.array([[1, 1]], dtype=numpy.float32),
model, model_onnx, basename="SklearnBinarizer-SkipDim1")
def test_convert_svmr_linear_binary(self):
model, X = self._fit_binary_classification(SVR(kernel='linear'))
model_onnx = convert_sklearn(model, 'SVR', [('input', FloatTensorType([1, X.shape[1]]))])
nodes = model_onnx.graph.node
self.assertIsNotNone(nodes)
self._check_attributes(nodes[0], {'coefficients': None,
'kernel_params': None,
'kernel_type': 'LINEAR',
'post_transform': None,
'rho': None,
'support_vectors': None})
dump_data_and_model(X, model, model_onnx, basename="SklearnRegSVRLinear-Dec3")
def test_support_vector_classifier_binary_no_prob(self):
svm, X = self._fit_binary_classification(SVC(gamma=0.5))
svm_coreml = coremltools.converters.sklearn.convert(svm)
svm_onnx = convert(svm_coreml.get_spec())
self.assertTrue(svm_onnx is not None)
# This should not have a probability output and will be a single node
nodes = svm_onnx.graph.node
self.assertEqual(len(nodes), 1)
self._check_model_outputs(svm_onnx, ['classLabel'])
dump_data_and_model(X, svm, svm_onnx, basename="CmlBinSVC-Out0",
allow_failure=True)
def test_convert_svmc_linear_binary(self):
model, X = self._fit_binary_classification(SVC(kernel='linear', probability=False))
model_onnx = convert_sklearn(model, 'SVC', [('input', FloatTensorType([1, X.shape[1]]))])
nodes = model_onnx.graph.node
self.assertIsNotNone(nodes)
svc_node = nodes[0]
self._check_attributes(svc_node, {'coefficients': None,
'kernel_params': None,
'kernel_type': 'LINEAR',
'post_transform': None,
'rho': None,
'support_vectors': None,
'vectors_per_class': None})
dump_data_and_model(X, model, model_onnx, basename="SklearnBinSVCLinearPF-NoProb-Opp",
allow_failure="StrictVersion(onnxruntime.__version__) <= StrictVersion('0.1.4')")
def test_lightgbm_booster_classifier_zipmap(self):
X = [[0, 1], [1, 1], [2, 0], [1, 2]]
X = numpy.array(X, dtype=numpy.float32)
y = [0, 1, 0, 1]
data = lightgbm.Dataset(X, label=y)
model = lightgbm.train({'boosting_type': 'gbdt', 'objective': 'binary',
'n_estimators': 3, 'min_child_samples': 1},
data)
model_onnx, prefix = convert_model(model, 'tree-based classifier',
[('input', FloatTensorType([None, 2]))])
assert "zipmap" in str(model_onnx).lower()
dump_data_and_model(X, model, model_onnx,
allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')",
basename=prefix + "BoosterBin" + model.__class__.__name__)
def test_model_normalizer_float(self):
model = Normalizer(norm='l2')
model_onnx = convert_sklearn(model, 'scikit-learn normalizer', [('input', FloatTensorType([1, 1]))])
self.assertTrue(model_onnx is not None)
self.assertTrue(len(model_onnx.graph.node) == 1)
dump_data_and_model(numpy.array([[1, 1]], dtype=numpy.float32),
model, model_onnx, basename="SklearnNormalizerL2-SkipDim1")
y = iris.target
y[y == 2] = 1
prob = svmutil.svm_problem(y, X.tolist())
param = svmutil.svm_parameter()
param.svm_type = NuSVR
param.kernel_type = svmutil.RBF
param.eps = 1
if noprint:
param.print_func = noprint
libsvm_model = svmutil.svm_train(prob, param)
node = convert(libsvm_model, "LibSvmNuSvmr", [('input', FloatTensorType(shape=['None', 'None']))])
self.assertTrue(node is not None)
dump_data_and_model(X[:5].astype(numpy.float32), SkAPIReg(libsvm_model), node,
basename="LibSvmNuSvmr")