Technology

An Introduction to Machine Learning in iOS With Swift and Playgrounds

Apple’s framework CoreML and machine learning are not that hard or complicated but many people have many thoughts regarding this. If you are interested in machine learning in iOS, you can go with it. But before you think or study about it, you need to know why you need machine learning and how it helps. In this post, we are going to look into that, so stay put and read the full article.

What is Machine Learning in iOS?

Machine learning helps you to take large data sets and apply complex mathematical problems faster and more frequently in iOS. Swift, Xcode, and Playgrounds are tools that you need to build a model in iOS. As a beginner learner, you will find an all-in-one package where the entry-level for machine learning has been demonstrated. If you have no machine learning experience at all, you can go with CreateML. Otherwise, follow this post and the information we have provided to learn how to train models.

Training Models in iOS

Do a dataset before opening the Xcode. The dataset is a collection of data where there are reviews of movies, locations of certain places, images, etc. Here we have used the ppStore_description.csv file for your reference and it helped us train the models. You can use kaggle.com to find datasets where you can find AppStore app descriptions and the apps’ names in a list. Those descriptions will help you identify an app for the user based on their text input. The model we are training is named TextClassifier that includes the labels of features or our input texts. The input texts are formed with sentences, paragraphs, or an entire document.

After downloading your dataset, open Xcode and follow the steps below.

  1. Create a new playground using the macOS template and select You need to use Mac OS because the CreatML framework is not available on iOS devices.
  2. In the Playground, delete the codes you see and import Foundation and CreateML
  3. Add the database to the Resources folder in Playground.

Here is the output you will see after that:

import Foundation

import CreateML

//: Create a URL path to your dataset and load it into a new MLDataTable

let filePath = Bundle.main.url(forResource: "appStore_description", withExtension: "csv")

let data = try MLDataTable(contentsOf: filePath)

//: Create two mutually exclusive, randomly divided subsets of the data table

//: The trainingData will hold the larger portion of rows

let (trainingData, testData) = data.randomSplit(by: 0.8)

//: Create your TextClassifier model using the trainingData

//: This is where the `training` happens and will take a few minutes

let model = try MLTextClassifier(trainingData: trainingData, textColumn: "app_desc", labelColumn: "app_name")

//: Test the performance of the model before saving it. See an example of the error report below

let metrics = model.evaluation(on: testData, textColumn: "app_desc", labelColumn: "app_name")

print(metrics.classificationError)

let modelPath = URL(fileURLWithPath: "/Users/joshuawalsh/Desktop/AppReviewClassifier.mlmodel")

try model.write(to: modelPath)

Run this code in your Playground to start the training and the model. Wait for a few minutes for the process to run.

Add the Model to Your iOS App

Create a new iOS project after saving the model on your computer. Have a look at the steps below:

  1. We will call the app AppPredictor and use the Storyboards
  2. Find where you have saved your model and bring it on your Xcode
  3. In ViewController.swift import UIKit, NaturalLanguage and CoreML.
import UIKit

import NaturalLanguage

import CoreML

Using the Custom Text Classifier

You will have to add some elements to your view controller in the storyboard. The elements include text field, label, and the button named IBAction. The other two fields will be IBOutlets.

@IBOutlet weak var textField: UITextField!

@IBOutlet weak var appNameLabel: UILabel!

@IBAction func predictApp(_ sender: Any) {

}

Here is a reference to your classifier that you will have to add:

private lazy var reviewClassifier: NLModel? = {

    // Create a custom model trained to classify or tag natural language text.

    // NL stands for Natual Language

    let model = try? NLModel(mlModel: AppReviewClassifier().model)

    return model

}()

Now the function will take in a string and return a string according to the user’s input.

private func predict(_ text: String) -> String? {

    reviewClassifier?.predictedLabel(for: text)

}

Add the predict function and leave the text fields text in the argument:

appNameLabel.text = predict(textField.text ?? "")

Now build your app and run it on iOS. As you can see, the machine learning process is a matter of a few simple codes. And you can always find the right dataset on the kaggle.com site. Or hire iPhone developer instead who will do all the work for you.

Author Bio –

Hermit Chawla is a MD at AIS Technolabs which is a Web/App design and Development Company, helping global businesses to grow by Global Clients. He love to share his thoughts on Web & App Development, Clone App Development and Game Development.

For More:

LinkedIn:  https://www.linkedin.com/in/hermit-chawla-lion-82b6513b

Twitter: https://twitter.com/aistechnolabs

Facebook: https://www.facebook.com/aistechnolabs

Shares: