Using Core ML in iOS from A to Z


Core ML (Core Machine Learning) has been released by Apple in iOS 11. There are many articles about “How to using Core ML in iOS”. However, in those articles, authors just told “How to USING the created model” but they didn’t talk “How to CREATE a model”, so I write this article to introduce how to using Core ML from scratch, include how to create a model by yourself.

Let’s start!

You can download the dataset that I use in this article here:

I. The dataset:

In this dataset, we have 5 fields: UserID, Gender, Age, EstimatedSalary, Purchased. From this dataset, I want to create a simple DSS (Decision Support System) is: Decide to purchase or not by using Age and Estimated Salary.

II. Create training model:

  1. Preparing:

a. Python:
Check your python version by using this command:
python — version

The reason we need to check python version because we need python version 2.7, if your python is version above 3.0, you need to install python version 2.7.

Why? I’ll talk in next section.

b. Install coremltools:
Apple has provided a tool has name “coremltools” to convert a model has been created by python to model that can use in iOS.

coremltools only run with python version 2.7, that is the reason I told you install python 2.7 in previous section. In past, my machine has python 3.6 so I can’t using coremltools.

If your machine only has python 2.7, just open the terminal and enter:
pip install coremltools

If your machine has both python 3.x and python 2.7, open the terminal and enter:
sudo pip2 install coremltools

For more details about install coremltools:

c. Install other packages for machine learning:
Depend of your machine learning method, you need other packages, just visit for more details.

In this article, we need these packages: sklearn, numpy, pandas (Google for how to install these packages).

Note: Install these packages with python 2.7

2. Create the model by python:

For example, I use Linear SVM and I create a python file has name with content:

import numpy as np
import pandas as pd
import pickle

To read dataset from dataset file, continue with

# Import dataset
dataset = pd.read_csv(‘Social_Network_Ads.csv’);
# Read data from Age, EstimatedSalary
X = dataset.iloc[:, [2,3]].values
# Read data from Purchased
y = dataset.iloc[:, 4].values

Because values of Age and EstimatedSalary have a large gap so we need to standardize them to a same field to avoid wrong result.

Why? I couldn't answer here because it's about another domain: “MACHINE LEARNING”. So you need to read about machine learning to understand why.

Countinue with

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
#Create the classifier
from sklearn import svm
classsifier = svm.SVC(kernel=’linear’, C=1.0),y)
#save the model to disk
from sklearn.externals import joblib
filename = ‘Social_Network_Ads_model.sav’
pickle.dump(classsifier, open(filename, ‘wb’))

Now, open your terminal and enter:

If you have both python 3.x and python 2.7:

3. Create model for Core ML:

Create another file has name with content:

import coremltools
from sklearn.externals import joblib
#Load the created model
classifier = joblib.load(‘Social_Network_Ads_model.sav’)
#Convert the created model to Core ML model
coremlModel = coremltools.converters.sklearn.convert(classifier, [“Age”,”EstimatedSalary”],”Purchased”)
# Save the converted model‘SocialNetworkAds.mlmodel’)

III. Using the model:
I create a demo project has name “DemoAI” look like:

Drag and drop file SocialNetworkAds.mlmodel to project, select this file we will see:

Look at section “Model Class”, check the Target Membership box if you don’t see the message “(Swift generated source)”

Xcode will generate a Object with name is the name of model file that we have imported to project.

Open ViewController.swift and modify like this:

Now, run the app and enjoy!

Happy Coding! Good luck!