Machine Learning for Mobile
上QQ阅读APP看书,第一时间看更新

Converting the scikit model to the Core ML model

Let me explain using an example: let's assume you're from France and you only speak French and English. Imagine you went to India on vacation. And you went to your hotel restaurant, where the waiter offered you a menu that was written in a local language. Now, what would you do? Let me guess, you'd ask the waiter, or another customer/your tour guide, to explain the items to you, or you simply scan the images in Google translate.

My point is that you need a translator. That's it. Similarly, in order for the scikit model to be understood by the iOS mobile application, a converter that will translate it to the Core ML format is required.

That's all the work of the following code. It will convert the scikit-learn format to the Core ML format:

//converting the fitted model to a Core ML Model file

model = coremltools.converters.sklearn.convert(classifier, input_features=list(cancerdata.columns.values), output_feature_names='typeofcancer')

model.save("cancermodel.mlmodel")

For this, to work, you have to install coremltools using your pip. Then, write the following code on the top to import it:

import coremltools

Once you run this program, you will get a model file in your disk, named  cancermodel.mlmodel, which you'll use in your iOS project for inference.