I wanted to convert a trained Scikit-learn logistic regression model into a Core ML format model which could be use in iOS.
Apple provide a Python package coremltools which can be used to make this conversion.
I tried installing it using the instructions on the Apple documentation, and ran into an error message - it appears that Python version 2.7 is needed.
The following steps (this post was helpful!) worked OK on macOS Sierra:
First, install a virtualenv with Python 2.7:
# If virtualenv not already installed globally: pip install virtualenv # Then create the virtual environment and activate it: virtualenv --python=/usr/bin/python2.7 pyv27 source pyv27/bin/activate
Next install the required packages into this virtual environment:
pip install -U coremltools pip install scikit-learn
Then run the conversion from a Python script (
clf is the name of this scikit-learn model):
import coremltools coreml_model = coremltools.converters.sklearn.convert(clf, ["Age"], "Breakdown") coreml_model.save("LogRegModel.mlmodel")
The documentation suggests evaluating the model with some predictions (
model.predict)… but it appears this is not supported in my version of MacOS…
Next step is to try out this model in CoreML and Swift.