How to use the coremltools.converters.sklearn.convert function in coremltools

To help you get started, we’ve selected a few coremltools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github onnx / onnxmltools / tests / coreml / test_cml_OneHotEncoderConverter.py View on Github external
def test_conversion_one_column(self):
        scikit_data = [[0], [1], [2], [4], [3], [2], [4], [5], [6], [7]]
        scikit_data = numpy.asarray(scikit_data, dtype='d')
        scikit_data_multiple_cols = [[0, 1],  [1, 0], [2, 2], [3, 3], [4, 4]]
        scikit_data_multiple_cols = numpy.asarray(scikit_data_multiple_cols, dtype='d')
        scikit_model = OneHotEncoder()

        # scikit_model.fit(scikit_data)
        # model_coreml = coremltools.converters.sklearn.convert(scikit_model, 'single_feature', 'out')

        scikit_model.fit(scikit_data_multiple_cols)
        try:
            model_coreml = coremltools.converters.sklearn.convert(scikit_model, ['feature_1', 'feature_2'], 'out')
        except Exception as e:
            warnings.warn("Unable to run convert OneHotEncoder with coreml.")
            return
        model_onnx = convert(model_coreml)                                     
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(scikit_data, scikit_model, model_onnx, basename="CmlOneHotEncoder-SkipDim1")
github onnx / onnxmltools / tests / coreml / test_cml_TreeEnsembleRegressorConverter.py View on Github external
def test_tree_ensemble_regressor(self):
        X, y = make_regression(n_features=4, random_state=0)
        model = RandomForestRegressor().fit(X, y)
        model_coreml = coremltools.converters.sklearn.convert(model)
        model_onnx = convert(model_coreml.get_spec())
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(X.astype(numpy.float32), model, model_onnx,
                            basename="CmlRegRandomForestRegressor-Dec3")
github onnx / onnxmltools / tests / coreml / test_cml_SupportVectorRegressorConverter.py View on Github external
def test_support_vector_regressor(self):
        X, y = make_regression(n_features=4, random_state=0)
     
        svm = SVR(gamma=1./len(X))
        svm.fit(X, y)
        svm_coreml = coremltools.converters.sklearn.convert(svm)
        svm_onnx = convert(svm_coreml.get_spec())
        self.assertTrue(svm_onnx is not None)
        dump_data_and_model(X.astype(numpy.float32), svm, svm_onnx, basename="CmlRegSVR-Dec3")
github onnx / onnxmltools / tests / coreml / test_cml_ScalerConverter.py View on Github external
def test_scaler(self):
        model = StandardScaler()
        data = numpy.array([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]], dtype=numpy.float32)
        model.fit(data)
        model_coreml = coremltools.converters.sklearn.convert(model)
        model_onnx = convert(model_coreml.get_spec())
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(data, model, model_onnx, basename="CmlStandardScalerFloat32")
github onnx / onnxmltools / tests / coreml / test_cml_GLMClassifierConverter.py View on Github external
def test_glm_classifier(self):
        iris = load_iris()
        X = iris.data[:, :2]
        y = iris.target
        y[y == 2] = 1

        # scikit-learn has changed the default value for multi_class.
        lr = LogisticRegression(multi_class='ovr')
        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)
        self.validate_zipmap(lr_onnx)
        dump_data_and_model(X.astype(numpy.float32), lr, lr_onnx, basename="CmlbinLogitisticRegression",
                            allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')")

        # Ensure there is a probability output
        svm = LinearSVC()
        svm.fit(X, y)
        svm_coreml = coremltools.converters.sklearn.convert(svm)
        svm_onnx = convert(svm_coreml.get_spec())
        self.assertTrue(svm_onnx is not None)
        self.validate_zipmap(svm_onnx)
        dump_data_and_model(X.astype(numpy.float32), svm, svm_onnx, basename="CmlBinLinearSVC-NoProb",
                            allow_failure=True)
github onnx / onnxmltools / tests / coreml / test_cml_ImputerConverter.py View on Github external
def test_imputer(self):
        try:
            model = Imputer(missing_values='NaN', strategy='mean', axis=0)
        except TypeError:
            model = Imputer(missing_values=np.nan, strategy='mean')
            model.axis = 0
        data = [[1, 2], [np.nan, 3], [7, 6]]
        model.fit(data)
        from onnxmltools.convert.coreml.convert import convert
        import coremltools  # noqa
        try:
            model_coreml = coremltools.converters.sklearn.convert(model)
        except ValueError as e:
            if 'not supported' in str(e):
                # Python 2.7 + scikit-learn 0.22
                return
        model_onnx = convert(model_coreml.get_spec())
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(np.array(data, dtype=np.float32),
                            model, model_onnx, basename="CmlImputerMeanFloat32")
github onnx / onnxmltools / tests / coreml / test_cml_GLMClassifierConverter.py View on Github external
y[y == 2] = 1

        # scikit-learn has changed the default value for multi_class.
        lr = LogisticRegression(multi_class='ovr')
        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)
        self.validate_zipmap(lr_onnx)
        dump_data_and_model(X.astype(numpy.float32), lr, lr_onnx, basename="CmlbinLogitisticRegression",
                            allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')")

        # Ensure there is a probability output
        svm = LinearSVC()
        svm.fit(X, y)
        svm_coreml = coremltools.converters.sklearn.convert(svm)
        svm_onnx = convert(svm_coreml.get_spec())
        self.assertTrue(svm_onnx is not None)
        self.validate_zipmap(svm_onnx)
        dump_data_and_model(X.astype(numpy.float32), svm, svm_onnx, basename="CmlBinLinearSVC-NoProb",
                            allow_failure=True)
github onnx / onnxmltools / tests / coreml / test_cml_DictVectorizerConverter.py View on Github external
def test_dict_vectorizer(self):
        model = DictVectorizer()
        data = [{'amy': 1., 'chin': 200.}, {'nice': 3., 'amy': 1.}]
        model.fit_transform(data)
        model_coreml = coremltools.converters.sklearn.convert(model)
        model_onnx = convert(model_coreml.get_spec())
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(data, model, model_onnx, basename="CmlDictVectorizer-OneOff-SkipDim1",
                            allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')")
github mfpierre / coreml-scikit-example / main.py View on Github external
# Load the iris dataset
iris = datasets.load_iris()

# Train a logistic regression
model = LogisticRegression()
model.fit(iris.data, iris.target)

# Make a prediction
print 'prediction with scikit model:'
print iris.target_names[model.predict([[1.0, 2.0, 2.0, 3.0]])]

# Dumping the model with joblib for comparison
joblib.dump(model, 'iris.pkl')

# Export and save the CoreML model
coreml_model = coremltools.converters.sklearn.convert(model, iris.feature_names, 'iris class')
coreml_model.save('iris.mlmodel')

# Load back the model
loaded_model =  coremltools.models.MLModel('iris.mlmodel')

# You can check the model's specifications
print loaded_model.get_spec()

input_data = {
    'sepal length (cm)': 1.0,
    'sepal width (cm)': 2.0,
    'petal length (cm)': 2.0,
    'petal width (cm)': 3.0
}
print 'prediction with coreml model:'
print loaded_model.predict(input_data)