Chapters

Hide chapters

Machine Learning by Tutorials

Second Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section I: Machine Learning with Images

Section 1: 10 chapters
Show chapters Hide chapters

4. Getting Started with Python & Turi Create
Written by Audrey Tam & Matthijs Hollemans

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Congratulations! If you’ve made it this far, you’ve developed a strong foundation for absorbing machine learning material. However, before we can move forward, we need to address the 10,000 pound snake in the room… Python. Until this point, you’ve made do with Xcode and Swift, however, if you’re going to get serious about Machine Learning, then it’s best you prepare yourself to learn some Python. In this chapter,

  • You’ll learn how to set up and use tools from the Python ecosystem for data science and machine learning (ML).
  • You’ll install Anaconda, a very popular distribution of Python (and R).
  • You’ll use terminal commands to create ML environments which you’ll use throughout this book.
  • Finally, you’ll use Jupyter Notebooks, which are very similar to Swift Playgrounds, to explore the Python language, data science libraries, and Turi Create, Apple’s ML-as-a-Service.

Starter folder

The starter folder for this chapter contains:

  • A notebook folder: The sample Jupyter Notebook data files.
  • .yaml files: Used to import pre-configured environments, if you want to skip the instructions for configuring the environments yourself.

Python

Python is the dominant programming language used for data science and machine learning. As such, there’s a myriad of tools available for the Python community to support data science and machine learning development. These include:

Packages and environments

Python is already installed on macOS. However, using this installation may cause version conflicts because some people use Python 2.7 while others use Python 3.x, which are incompatible branches of the same language. To further complicate things, working on machine learning projects requires integrating the correct versions of numerous software libraries, also known as “packages”.

Conda

The data science community developed Conda to make life easier. Conda handles Python language versions, Python packages, and associated native libraries. It’s both an environment manager and a package manager. And, if you need a package that Conda doesn’t know about, you can use pip within a conda environment to grab the package.

Installing Anaconda

In a browser, navigate to https://www.anaconda.com/download/#macos, and download the 64-bit Command Line installer with Python 3.7, as highlighted in the image below:

Using Anaconda Navigator

Anaconda comes with a desktop GUI that you can use to create environments and install packages in an environment. However, in this book, you’ll do everything from the command line. Given this fact, it’s worth going over some basic commands with Conda which you’ll do in the next section.

Useful Conda commands

As mentioned before, Conda is a package and environment management system. When working with Python projects, you’ll often find it useful to create new environments, installing only the packages you need before writing your code. In this section, we’ll explore many useful commands you’ll reuse many times when working with Python and Conda.

Basic workflow

Create a new environment:

Listing environments or packages

List the environments you’ve created; the one with the * is the currently active environment:

Setting up a base ML environment

In this section, you’ll set up some environments. If you prefer a quicker start, create an environment from myenv.yaml and skip down to the Jupyter Notebooks section. You can do this by importing mlenv.yaml into Anaconda Navigator or by running the following command from a Terminal window:

Python libraries for data science

Begin by creating a custom base environment for ML, with NumPy, Pandas, Matplotlib, SciPy and scikit-learn. You’ll be using these data science libraries in this book, but they’re not automatically included in new Conda environments.

An important note about package versions

Technology moves fast, also in the world of Python. Chances are that by the time you read this book, newer versions are available for the packages that we’re using. It’s quite possible these newer versions may not be 100% compatible with older versions.

Jupyter Notebooks

With Jupyter Notebooks, which are a lot like Swift Playgrounds, you can write and run code, and you can write and render markdown to explain the code.

Starting Jupyter

From Terminal, first activate your environment and then start Jupyter:

Pandas and Matplotlib

The notebook has a single empty cell. In that cell, type the following lines:

Differences between Python and Swift

In this section, you’ll spend some time getting familiar with common Python syntax.

Transfer learning with Turi Create

Despite the difference in programming languages, deep down Turi Create shares a lot with Create ML, including transfer learning. With Turi Create v5, you can even do transfer learning with the same VisionFeaturePrint_Scene model that Create ML uses.

Creating a Turi Create environment

First, you need a new environment with the turicreate package installed. You’ll clone the mlenv environment to create turienv, then you’ll install turicreate in the new environment. Conda doesn’t know about turicreate, so you’ll have to pip install it from within Terminal.

List pip-installed packages

In Terminal, use this command to list all of the packages in the active environment or a specific package:

Turi Create notebook

Note: If you skipped the manual environment setup and imported turienv.yaml into Anaconda Navigator, use the Jupyter Launch button on the Anaconda Navigator Home Tab instead of the command line below, then navigate in the browser to starter/notebook.

Kki muqqj jabv uy ttu MTjiwa
Dfi padqc xufw uj rne TTriwo

Epsboyi wba tquutiqv oniqew
Ebsgabi nsi xfeimadp egihur

Loipohl iq ug esevo meby pahgcehxag
Ziecokf eh ur iqelu jayd junbgedtaz

Getting the class labels

OK, now you know how to extract the class name for a single image, but there are over 4,800 images in the dataset. As a Swift programmer, your initial instinct may be to use a for loop, but if you’re really Swift-y, you’ll be itching to use a map function. SFrame has a handy apply() method that, like Swift’s map or forEach, lets you apply a function to every row in the frame:

Zke KLgaka kuk pos o liq juwupk
Lmo VNhufo zeb xuq u zuk faqobm

Jemhuvc riy zvo yuwiz suxaqq
Vodxukz xeh gje ruvat cecetd

Let’s do some training

Once you have your data in an SFrame, training a model with Turi Create takes only a single line of code (OK, it’s three lines, but only because we have to fit it on the page):

Validation

After 15 iterations, validation accuracy is close to training accuracy at ~90%. At 20 iterations, training accuracy starts to pull away from validation accuracy, and races off to 100%, while validation accuracy actually drops… Massive overfitting happening here! If the validation accuracy gets worse while the training accuracy still keeps improving, you’ve got an overfitting problem.

Testing

Run these commands to load the test dataset and get the class labels:

Wwo bupneteam dibgiq
Wwo wopfewuos diljay

Exporting to Core ML

In the next cell, Shift-Enter this command:

Shutting down Jupyter

To shut down Jupyter, click the Logout button in this browser window and also in the window showing your ML directory.

Deactivating the active environment

If you activated turienv at the terminal command line, enter this command to deactivate it:

Docker and Colab

There are two other high-level tools for supporting machine learning in Python: Docker and Google Colaboratory. These can be useful for developing machine learning projects, but we’re not covering them in detail in this book.

Docker

Docker is like a virtual machine but simpler. Docker is a container-based system that allows you to re-use and modularize re-usable environments, and is a fundamental building block to scaling services and applications on the Internet efficiently. Installing Docker gives you access to a large number of ML resources distributed in Docker images as Jupyter notebooks like hwchong/kerastraining4coreml or Python projects like the bamos/openface face recognition model. Our Beginning Machine Learning with Keras & Core ML (bit.ly/36cS6KU) tutorial builds and runs a keras-mnist Docker image, and you can get comfortable using Docker with our Docker on macOS: Getting Started tutorial here: bit.ly/2os0KnY.

Weojql Lakgul Wiy liy obaro wfehjazaab
Moerhm Vaywek Pom vep igoqi fpucrowaes

Google Colaboratory

Google Research’s Colaboratory at colab.research.google.com is a Jupyter Notebook environment that runs in a browser. It comes with many of the machine learning libraries you’ll need, already installed. Its best feature is, you can set the runtime type of a notebook to GPU to use Google’s GPU for free. It even lets you use Google’s TPUs (tensor processing units).

Key points

  • Get familiar with Python. Its widespread adoption with academics in the machine learning field means if you want to keep up to date with machine learning, you’ll have to get on board.
  • Get familiar with Conda. It will make working with Python significantly more pleasant. It allows you to try Python libraries in a controlled environment without damaging any existing environment.
  • Get familiar with Jupyter notebooks. Like Swift playgrounds, they provide a means to quickly test all things Python especially when used in combination with Conda.

Where to go from here?

You’re all set to continue learning about machine learning for image classification using Python tools. The next chapter shows you a few more Turi Create tricks. After that, you’ll be ready to learn how to create your own deep learning model in Keras.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now