We know machine learning is so popular in mobile and desktop applications. Therefore we need basic ML skills to follow this trend.
This article prepared, for how to use easily Machine Learning power in IOS & macOS applications.
Before starting the article, if you don’t have experience with machine learning basics, you may get information from this article.
Fundamentals of CreateML Framework
CreateML is a programming framework, to create apps with ML in IOS, macOS, and IpadOS (Apple Ecosystem).
CreateML is facilitating using ML on projects for non-data scientists. ML power can spread many apps.
But if you data scientist don’t worry, you still can create complex machine learning algorithms with CreateML.
Machine Learning WorkFlow
Basically, machine learning workflow is simple, get data, train models with these data, test your model, update your model, use this model in a real application.
- First, you need to data, you can collect data on API and public datasets.
- Second, you can store these data on CSV table or database format.
- Third, if you are professional in this subject, you should clean your data.
- Fourth, you’ll train your model with 1 lines of code in swift.
- Fifth, you can check your model efficient with CreateML (you don’t need to be an expert).
- Finaly, this part is specific for CreateML, you should save ML model as mlmodel format
After that, you may use this trained machine learning model in your own IOS,macOS, IpadOS applications with CoreML.
Step by Step: Create ML Models With CreateML
In this part, we are going to put into practice the previously learned machine learning workflow subpart.
1 – Determine Your Dataset
Before building the machine learning model, a dataset is usually determined and operations are performed on it.
You can get a dataset from various sources such as Kaggle, Trends API, mostly big companies make some data public.
Since this article will apply a linear regression example, data sets in CSV format will be more useful to you. Get the dataset used in this article here.
2 – Import CreateML and Foundation Modules
While we coding the machine learning models we need 2 essential libraries. We’ll use the foundation module for getting CSV data from Mac.
CreateML is the main library for creating data tables, creating ML models, etc. Let’s add these frameworks to your code.
// Libraries import Foundation import CreateML
3 – Create Data Table With CSV Table
In this part, we are going to create DataFrame (DataTable) for access to data in the swift project.
If you want to examine the data set you are using, print the variable containing the DataTable to the screen with print.
This, you can get important information such as column names, number of rows, general properties of data, properties of columns.
It is helpful to examine and analyze the data before importing it into a model, it is important to learn about the dataset even if you are not an expert.
4 – Separating Relevant Data From DataTable
Sometimes, datasets come with irrelevant data for the project. Like these cases, you need to separate relevant data columns with indexing.
Don’t worry, it’s very easy, you can separate data columns like separating relevant elements on the array.
Anymore, we have newly dataTable without irrelevant columns, now we can divide 2 parts these tables as train and test dataTable.
5 – Dividing DataTable as Test and Training
We need 2 separated tables: test and training tables. With these tables, we are going to train our model and we gonna evaluate the model.
Mostly, We divide it into 70% training dataset and 30% test set but it’s changeable according to your dataset and project. We’ll divide it into %80 training and %20 test.
Short information about randomsplit, randomly separates the DataTable you provide as a test and train training set at the rate you specify.
Professional Tip: Usually, training the models with more data yields better results, so if you have more data, make the test rate 5%. You will see the difference.
Professional Tip 2: If you are ready to put in a little more effort, combine the data you found into a CSV file, then train your model with %90 of your data.
Professional Tip 3: By clearing the data, you can improve the performance of your model, if you are merging the data, you should clean the dataset. Read this article.
6 – Creating Model and Train the Model
In this part, we are going to create a model with CreateML, also we’ll train this linear regression model with TrainTable.
Here, we assigned a training dataset into the first parameter, then we selected the target value in this dataset to predict target data.
CreateML is separate a very small percentage of data on the training dataset for validating the model’s progress during the training phase.
The training process measures the performance of the model against the validation data. Depending on the validation accuracy, the algorithm can change some steps.
Even, algorithm finishes the training process if the accuracy is high enough, so you don’t have to analyze the training process, everything is handled automatically.
You can get different results when you start again training algorithm because the split is done randomly.
7 – Test Your Regression
We finished creating the model and we trained this model, anymore we need prediction. Before presenting people you have to test this model for accuracy.
Yes, now the ML model has been tested with test data, and we store the results here in a separate variable.
For models that have completed this process, you’ll now give the final result. Ask questions to yourself, this model produces accurate predictions for your project, etc?
In order to answer the questions here, you need to examine the results, you can examine with these metrics “maximum error” or “mean squared error”.
8 – Saving Machine Learning Model
Big congratulations, if everything works fine so far, you are now ready to save this model and use it in your main application.
Thanks to the code here, the machine learning model we created were saved on the desktop in .mlmodel format.
don’t underestimate the model’s information, increase the version number every time you do something new and also update the description if there is a big change.
To see a model’s tags, simply select the model in Xcode project explorer after adding it to an app.
Code and Dataset in Github
You can find code and dataset on my GitHub account. Click here to go to the Github repository page.
Anymore, I share every code, dataset, UI, script, etc. in Github for supporting open-source code also to make your job easier.
Thanks for the reading…