SKLearn — coremltools API Reference 8.3 documentation (original) (raw)

coremltools.converters.sklearn._converter.convert(sk_obj, input_features=None, output_feature_names=None)[source]

Convert scikit-learn pipeline, classifier, or regressor to Core ML format.

Parameters:

sk_obj: model | [model] of scikit-learn format.

Scikit learn model(s) to convert to a Core ML format.

The input model may be a single scikit learn model, a scikit learn pipeline model, or a list of scikit learn models.

Currently supported scikit learn models are:

The input model, or the last model in a pipeline or list of models, determines whether this is exposed as a Transformer, Regressor, or Classifier.

Note that there may not be a one-to-one correspondence between scikit learn models and the Core ML models chosen to represent them. For example, many scikit learn models are embedded in a pipeline to handle processing of input features.

input_features: str | dict | list

Optional name(s) that can be given to the inputs of the scikit-learn model. Defaults to "input".

Input features can be specified in a number of forms.

output_feature_names: string or list of strings

Optional name(s) that can be given to the inputs of the scikit-learn model.

The output_feature_names is interpreted according to the model type:

Returns:

model:MLModel

Returns an MLModel instance representing a Core ML model.

Examples

from sklearn.linear_model import LinearRegression import pandas as pd

Load data

data = pd.read_csv('houses.csv')

Train a model

model = LinearRegression() model.fit(data[["bedroom", "bath", "size"]], data["price"])

Convert and save the scikit-learn model

import coremltools coreml_model = coremltools.converters.sklearn.convert(model, ["bedroom", "bath", "size"], "price") coreml_model.save('HousePricer.mlmodel')