Core ML Supports Scikit-learn?

Hey everyone,

So I have creted a classifier using scikit learn. However, the data model I get is a .pkl (Pickle) file. I am wondering if Core ML supports the conversion of .pkl files or only .csv files. Also, if Core ML does not support .pkl files, how can I convert my file into a .csv file? Thanks!


EDIT: So basically I would like to know if I can convert .pkl files to a .mlmodel and how can I do so

Replies

Core ML supports models converted from scikit-learn. The coremltools documentation has an example of converting a scikit-learn model. Here is the code from that documentation:


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')


Hope this helps!

Pickle is a Python protocol that is used to seralize arbitary Python code and classes. You should be able to write code to load the scikit-learn model from your pickle file (that code is usually custom to your model) and then you can convert the scikit-learn model to CoreML format using coremltools.

I know how to convert a model to a Core ML model but what I can't figure out is how to write code to load the scikit-learn model from the pickle file. The model I am using comes from this tutorial: http://radimrehurek.com/data_science_python/

At the end of the tutorial: I load the model with this command:

svm_detector_reloaded = cPickle.load(open('sms_spam_detector.pkl'))

What I don't know is how to take this file and turn it into a Scikit-Learn file. Thanks!

Did you ever have any success with this? I also have created a custom model, saved in a pickle file. I now want to bring that file over to COREML so that I can deploy it in a Vision app.