Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $9.99/month after trial. Cancel anytime.

Machine Learning for iOS Developers
Machine Learning for iOS Developers
Machine Learning for iOS Developers
Ebook602 pages4 hours

Machine Learning for iOS Developers

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Harness the power of Apple iOS machine learning (ML) capabilities and learn the concepts and techniques necessary to be a successful Apple iOS machine learning practitioner!

Machine earning (ML) is the science of getting computers to act without being explicitly programmed. A branch of Artificial Intelligence (AI), machine learning techniques offer ways to identify trends, forecast behavior, and make recommendations. The Apple iOS Software Development Kit (SDK) allows developers to integrate ML services, such as speech recognition and language translation, into mobile devices, most of which can be used in multi-cloud settings. Focusing on Apple’s ML services, Machine Learning for iOS Developers is an up-to-date introduction to the field, instructing readers to implement machine learning in iOS applications.

Assuming no prior experience with machine learning, this reader-friendly guide offers expert instruction and practical examples of ML integration in iOS. Organized into two sections, the book’s clearly-written chapters first cover fundamental ML concepts, the different types of ML systems, their practical uses, and the potential challenges of ML solutions. The second section teaches readers to use models—both pre-trained and user-built—with Apple’s CoreML framework. Source code examples are provided for readers to download and use in their own projects. This book helps readers:

  • Understand the theoretical concepts and practical applications of machine learning used in predictive data analytics
  • Build, deploy, and maintain ML systems for tasks such as model validation, optimization, scalability, and real-time streaming
  • Develop skills in data acquisition and modeling, classification, and regression.
  • Compare traditional vs. ML approaches, and machine learning on handsets vs. machine learning as a service (MLaaS)
  • Implement decision tree based models, an instance-based machine learning system, and integrate Scikit-learn & Keras models with CoreML

Machine Learning for iOS Developers is a must-have resource software engineers and mobile solutions architects wishing to learn ML concepts and implement machine learning on iOS Apps.

LanguageEnglish
PublisherWiley
Release dateFeb 12, 2020
ISBN9781119602903
Machine Learning for iOS Developers

Read more from Abhishek Mishra

Related to Machine Learning for iOS Developers

Related ebooks

Computers For You

View More

Related articles

Reviews for Machine Learning for iOS Developers

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Machine Learning for iOS Developers - Abhishek Mishra

    Introduction

    Machine learning is one of the hottest trends in computing and deals with the problem of creating computer programs that can generalize and predict information reliably, quickly, and with accuracy, resembling what a human would do with similar information. With the recent hype in mainstream media around novel applications of machine learning, you may be inclined to think that machine learning is a relatively new discipline, but that is far from the truth. In fact, machine learning has been around for several decades, and it is because of recent advances in storage, processor, and GPU technology that it is possible to build and deploy machine learning systems at scale and get results in real time.

    This book is targeted at intermediate/advanced iOS developers who are looking to come to grips with the fundamentals of machine learning, learn about some of the common tools used by data scientists, and learn how to build and deploy models into their iOS applications. This book at all times attempts to balance theory and practice, giving you enough visibility into the underlying concepts while providing you with the best practices and practical advice that you can apply to your workplace right away.

    Machine learning is a rapidly evolving field. I have made every attempt to keep the content up-to-date and relevant. Even though this makes the book susceptible to being outdated on a few rare instances, I am confident the content will remain useful and relevant through the next releases of iOS.

    What Does This Book Cover?

    This book covers the fundamental concepts of machine learning as well as the use of these concepts to build real-world models and use them in your iOS apps.

    Chapter 1: Introduction to Machine Learning This chapter introduces the different types of machine learning models commonly found in real-world applications as well as tools and libraries used by data scientists to build these models. The chapter also includes examples of real-world applications of machine learning and sources of training data.

    Chapter 2: The Machine-Learning Approach This chapter examines a hypothetical scenario in which a rule-based system is used to process credit card applications. The limitations of the rule-based system are examined, and a machine learning system is devised to address some of those limitations. The chapter concludes with an overview of the steps involved in building a typical machine learning solution.

    Chapter 3: Data Exploration and Preprocessing This chapter focuses on the data exploration and feature engineering stage, specifically the use of popular Python libraries NumPy, Pandas, and Scikit-learn for tabular data. The chapter also explores feature selection techniques.

    Chapter 4: Implementing Machine Learning on Mobile Apps This chapter explores the options available to you as an iOS developer to integrate machine learning techniques on your apps. The chapter compares the pros and cons of an edge-based versus server-based deployment model and introduces both Apple offerings as well as other third-party offerings that can be used from within your apps.

    Chapter 5: Object Detection Using Pre-trained Models This chapter focuses on the use of pre-trained models for object detection in your iOS apps. The chapter also covers the basics of artificial neural networks (ANNs) and convolutional neural networks (CNNs).

    Chapter 6: Creating an Image Classifier with the Create ML App This chapter covers the use of Apple's Create ML app to train a machine learning model that can detect the dominant object in an image. The model is trained on a subset of the Kaggle Dogs vs. Cats dataset and exported to the Core ML format. The exported model is used within an iOS app.

    Chapter 7: Creating a Tabular Classifier with Create ML This chapter covers the use of Apple's Create ML app to train a classification model on tabular data. The model is trained on the popular UCI ML wine dataset and exported to the Core ML format using the Create ML app. The trained model is then used in an iOS app that allows users to input the chemical characteristics of wine and learn the quality of the beverage.

    Chapter 8: Creating a Decision Tree Classifier This chapter focuses on the use of Scikit-learn to create a decision tree classification model on the popular Iris flowers dataset. The trained model is then exported to the Core ML format using the Core ML Tools Python library and used in an iOS app.

    Chapter 9: Create a Logistic Regression Model Using Scikit-learn and Core ML This chapter focuses on the use of Scikit-learn to create a logistic regression model on the popular Pima Indians diabetes dataset. The trained model is then exported to the Core ML format using the Core ML Tools Python library and used in an iOS app.

    Chapter 10: Building a Deep Convolutional Neural Network with Keras This chapter covers the creation and training of a popular deep convolutional neural network architecture called Inception V4 using the Keras functional API. The Inception V4 network is trained on a small publicly available dataset and then used in an iOS app.

    Appendix A: Anaconda and Jupyter Notebook Setup This appendix helps you install Anaconda Navigator on your computer, set up a Python environment that includes several common machine learning libraries, and configure Jupyter Notebook.

    Appendix B: Introduction to NumPy and Pandas This appendix shows you how to use NumPy and Pandas. These libraries are commonly used during the data exploration and feature engineering phases of a project.

    Additional Resources

    In addition to this book, here are some other resources that can help you learn more about machine learning.

    Apple Machine Learning Journal:https://machinelearning.apple.com

    Scikit-learn User Guide:https://scikit-learn.org/stable/user_guide.html

    Core ML Developer Documentation:https://developer.apple.com/documentation/coreml

    Core ML Tools Documentation:https://apple.github.io/coremltools/

    Keras Documentation:https://keras.io

    Reader Support for This Book

    We provide support for this book in a couple of ways.

    Companion Download Files

    As you work through the examples in this book, the project files you need are all available for download from www.wiley.com/go/machinelearningforiosdevelopers.

    How to Contact the Publisher

    If you believe you've found a mistake in this book, please bring it to our attention. At John Wiley & Sons, we understand how important it is to provide our customers with accurate content, but even with our best efforts an error may occur.

    To submit your possible errata, please email it to our customer service team at [email protected] with the subject line Possible Book Errata Submission.

    Part 1

    Fundamentals of Machine Learning

    Chapter 1: Introduction to Machine Learning

    Chapter 2: The Machine-Learning Approach

    Chapter 3: Data Exploration and Preprocessing

    Chapter 4: Implementing Machine Learning on Mobile Apps

    Chapter 1

    Introduction to Machine Learning

    WHAT'S IN THIS CHAPTER

    Introduction to the basics of machine learning

    Tools commonly used by data scientists

    Applications of machine learning

    Types of machine learning systems

    Hello, and welcome to the exciting world of machine learning. If you have never heard of machine learning until now, you may be tempted to think that it is a recent innovation in computer science that will result in sentient computer programs, significantly more intelligent than humans that will one day make humans obsolete. Fortunately, there is very little truth in that idea of machine learning. For starters, it is not a recent development; computer scientists have been researching for decades ways to make computers more intelligent by attempting to find ways to teach computers to make generalizations and predictions much like humans do. However, intelligence is not just about recognizing things, and even the best machine learning systems today are not capable of reasoning like human beings. The machine learning systems that exist today are essentially pattern recognizers and can, for instance, examine a picture and detect a cup of tea close to the edge of a table. They cannot, however, reason that the cup could accidentally fall off the table, as it is too close to the edge.

    Machine learning specifically deals with the problem of creating computer programs that can generalize and predict information reliably, quickly, and with accuracy resembling what a human would do with similar information. Machine learning algorithms require a lot of processing and storage space and until recently were only possible to deploy in large companies or in academic institutions. Recent advances in storage, processor, and GPU technology have provided the processing power required to build and deploy machine learning systems at scale and get results in real time.

    In the past, lack of quality data was also a factor that prevented widespread adoption of machine learning. With the advent of social media and analytics applications, developers have access to lot more data about their customers than they did in the past.

    Another factor that has contributed to the recent increase in machine learning applications is the availability of excellent tools and frameworks such as Core ML, Create ML, Pandas, Matplotlib, TensorFlow, Scikit-learn, PyTorch, and Jupyter Notebooks, which have made it possible for newcomers to start building real-world machine learning applications without having to delve into the complex underlying mathematical concepts. In this chapter, you will learn about what machine learning is, how machine learning systems are classified, and examples of real-world applications of machine learning.

    What Is Machine Learning?

    Machine learning is a discipline within artificial intelligence that deals with creating algorithms that learn from data. Machine learning traces its roots to a computer program created in 1959 by a computer scientist Arthur Samuel while working for IBM. Samuel's program could play a game of checkers and was based on assigning each position on the board a score that indicated the likelihood of leading toward winning the game. The positional scores were refined by having the program play against itself, and with each iteration, the performance of the program improved. The program was in effect learning from experience, and the field of machine learning was born.

    A machine learning system can be described as a set of algorithms based on mathematical principles that can mine data to find patterns in the data and then make predictions on new data as it is encountered. Rule-based systems can also make predictions on new data; however, rule-based systems and machine learning systems are not the same. A rule-based system requires a human to find patterns in the data and define a set of rules that can be applied by the algorithm. The rules are typically a series of if-then-else statements that are executed in a specific sequence. A machine learning system, on the other hand, discovers its own patterns and can continue to learn with each new prediction on unseen data.

    Tools Commonly Used by Data Scientists

    As an iOS developer, Core ML is likely to be your framework of choice when it comes to deploying a machine learning model in your app. Training a machine learning model, on the other hand, involves several steps and, except for the simplest of cases, is performed offline and using a different set of tools. Apple provides a number of tools to train Core ML models and convert pre-trained models built using other frameworks such as Scikit-learn and Keras to the Core ML format. The reason you may want to use a non-Apple framework like Scikit-learn or Keras to train a model is because the library may provide an implementation of a model that is not possible to train using Apple's toolset or may simply be updated more frequently.

    Depending on the type of model you are trying to build, there may be a lot of steps involved even before you get to the point when you can start training—such as data preprocessing, feature engineering, and data visualization. Data scientists frequently use a set of tools to assist them with these steps. In this section, you will learn about some of the tools commonly used by data scientists to build machine learning solutions.

    Although R has historically been the language of choice for statisticians, most data scientists and machine learning engineers today work in Python. The popularity of Python in the machine learning space is due to the abundance of machine learning–specific libraries that assist with all steps of the machine learning process from preparing data, feature engineering, information visualization, to training models with the latest algorithms. Where Python code is presented, this book will use Python 3.6.5. The following are the most popular Python machine learning tools:

    Jupyter Notebook: This is a popular web-based interactive development environment for data science projects. A notebook combines code, execution results, and visualization results all in a single document. Jupyter Notebook is a successor to an older project called IPython Notebook. You can find out more about Jupyter Notebooks at http://jupyter.org. Appendix A contains instructions on installing Anaconda Navigator and setting up Jupyter Notebooks on your own computer.

    Anaconda Navigator: This is a commonly used package manager for data scientists. It allows users to conveniently install and manage Python libraries and quickly switch between different sets of libraries and Python versions. It also includes popular tools such as Jupyter Notebooks and Spyder Python IDE. You can find more information on Anaconda at https://www.anaconda.com.

    Scikit-learn: This is a Python library that provides implementations of several machine learning algorithms for classification, regression, and clustering applications. It also provides powerful data preprocessing and dimensionality reduction capabilities. You can find more information on Scikit-learn at https://www.scikit-learn.org.

    NumPy: This is a Python library that is commonly used for scientific computing applications. It contains several useful operations such as random number generation and Fourier transforms. The most popular NumPy features for data scientists are N-dimensional data arrays (known as ndarrays) and functions that manipulate these arrays. NumPy ndarrays allow you to perform vector and matrix operations on arrays, which is significantly faster than using loops to perform element-wise mathematical operations. You can find more information on NumPy at https://www.numpy.org.

    Pandas: This is a Python library that provides a number of tools for data analysis. Pandas builds upon the NumPy ndarray and provides two objects that are frequently used by data scientists—the series and the dataframe. You can find more information on Pandas at https://pandas.pydata.org.

    Matplotlib: This is a popular 2D plotting library. It is used by data scientists for data visualization tasks. You can find more information on Matplotlib at https://matplotlib.org.

    Pillow: This is a library that provides a variety of functions to load, save, and manipulate digital images. It is used when the machine learning system needs to work with images. You can find more information on Pillow at https://python-pillow.org.

    Google TensorFlow: This is a Python library for numerical computation. It was developed by Google and eventually released as an open source project in 2015. Google TensorFlow is commonly used to build deep learning systems. It uses a unique computation graph-based approach and requires users to build a computation graph where each node in the graph represents a mathematical operation and the connections between the nodes represent data (tensors). You can find out more information on TensorFlow at https://www.tensorflow.org.

    Keras: This is another popular Python library for training and using deep learning networks. It was designed to facilitate rapid experimentation with neural networks and acts like a higher-level abstraction to popular deep learning frameworks like Google TensorFlow. As of TensorFlow 2.0, Keras is included with TensorFlow.

    Common Terminology

    In this section, we examine some of the common machine learning–specific terminology that you are likely to encounter. While this list is not exhaustive, it should be useful to someone looking to get started.

    Machine learning model: This is the algorithm that is used to make predictions on data. It can also be thought of as a function that can be applied to the input data to arrive at the output predictions. The machine learning algorithm often has a set of parameters associated with it that influence its behavior; these parameters are determined by a process known as training.

    Data acquisition: This is the process of gathering the data needed to train a machine learning model. This could include activities ranging from downloading ready-to-use CSV files to scraping the Web for data.

    Input variables: These are the inputs that your machine learning model uses to generate its prediction. A collection of N input variables is generally denoted by lowercase xi with i = 1, 2, 3, … N. Input variables are also known as features.

    Feature engineering: This is the process of selecting the best set of input variables and often involves modifying the original input variables in creative ways to come up with new variables that are more meaningful in the context of the problem domain. Feature engineering is predominantly a manual task.

    Target variable: This is the value you are trying to predict and is generally denoted by a lowercase y. When you are training your model, you have a number of training samples for which you know the expected value of the target variable. The individual values of the target variable for N samples are often referred to as yi with i = 1, 2, … N.

    Training data: This is a set of data that contains all the input features as well as any engineered features and is used to train the model. For each item in the set, the value of the target variable is known.

    Test data: This is a set of data that contains all the input features (including engineering features) as well as the values of the target variable. This set is not used while training the model but instead is used to measure the accuracy of the model's predictions.

    Regression: This is a statistical technique that attempts to find a mathematical relationship between a dependent variable and a set of independent variables. The dependent variable is usually called the target, and the independent variables are called the features.

    Classification: This is the task of using an algorithm to assign observations to a label from a fixed set of predefined labels.

    Linear regression: This is a statistical technique that attempts to fit a straight line to a set of data points. Linear regression is commonly used to create machine learning models that can be used to predict continuous values (such as height, width, age, etc.).

    Logistic regression: This is a statistical technique that uses the output of linear regression and converts it to a probability between 0 and 1 using a sigmoid function. Logistic regression is commonly used to create machine learning models that can predict class-wise probabilities. An example is the probability that a person will develop an illness later in life or the probability that an applicant will default on a loan payment.

    Decision tree: This is a tree-like data structure that can be used for classification and prediction problems. Each node in the tree represents a condition, and each leaf represents a decision. Building a decision tree model involves examining the training data and determining the node structure that achieves the most accurate results.

    Error function: This is a mathematical function that takes as input the predicted and actual values and returns a numerical measure that captures the error in prediction. The goal of the training function is to minimize the error function.

    Neural networks: This is a machine learning model that mimics the structure of the human brain. A neural network consists of multiple interconnected nodes, organized into distinct layers—the input layer, the in-between layers (also known as the hidden layers), and the output layer. Nodes are commonly known as neurons. The number of neurons in the input layer correspond to the number of input features, and the number of neurons in the output layer correspond to the number of classes that are being predicted/classified.

    Deep learning: This branch of machine learning utilizes multilayer neural networks with a large number of neurons in each layer. It is also quite common for deep learning models to use multiple deep neural networks in parallel.

    Real-World Applications of Machine Learning

    Machine learning is transforming business across several industries at an unprecedented rate. In this section, you will learn about some of the applications of machine-learning-based solutions:

    Fraud detection: Machine learning is commonly used in banks and financial institutions to make a decision on the overall risk associated with a payment instruction. Payments in this context include money transfers and purchases (payments to providers) using cards. The risk decision is based on several factors including the transactional history. If the risk is low, the transaction is allowed to proceed. If the risk is too high, the transaction is declined. If the risk is deemed to lie in an acceptable threshold, the customer may be asked to perform some form of step-up authentication to allow the transaction to proceed.

    Credit scoring: Whenever a customer applies for a credit product such as a loan or credit card, a machine learning system computes a score to indicate the overall risk of the customer not being able to repay the loan.

    Insurance premium calculation: Machine learning systems are commonly used to compute the insurance premium that is quoted to a customer when they apply to purchase an insurance product.

    Behavioral biometrics: Machine learning systems can be trained to build a profile of a user based on the manner in which they use a website or a mobile application. Specifically, such systems create a profile of the user based on analyzing information on from where they access the platform, at what times of the day, where they click a page, how long they stay on a page, how quickly they move their mouse across a page, etc. Once the system has been trained, it can be used to provide real-time information on the likelihood that someone is impersonating a customer.

    Product recommendation: Machine learning systems are commonly used by online retailers (such as Amazon) to provide a list of recommendations to a customer based on their purchase history. These systems can even predict when a customer is likely to run out of groceries and send them reminders to order items.

    Churn prediction: Machine learning systems are commonly used to predict which customers are likely to cancel their subscription to a product or service in the next few days. This information gives businesses an opportunity to try to retain the customer by offering a promotion.

    Music and video recommendations: Online content providers such as Netflix and Spotify use machine learning systems to build complex recommendation engines that analyze the movies and songs you listen to and provide recommendations on other content that you may like.

    Text prediction: Smart keyboards like the one on your iPhone use machine learning models to predict the next word you are likely to type, and the top three predictions are surfaced as suggestions on top of the iPhone keyboard as you type.

    Conversational interfaces: Machine learning models are used with chatbots to map natural language inputs from users into an input that the chatbot can understand. For example, a chatbot that allows users to access banking functions will use machine learning to interpret slightly differing natural language instructions such as I want to pay somebody, I want to make a payment to my mother, Make a payment, and Transfer some money to all map to the same internal banking process, perhaps called Make payments.

    Text analysis: Machine learning models can be used to analyze text documents to extract information on entities referenced in the document and the overall sentiment of the document. The document in this context does not have to be a lengthy Microsoft Word document; it could be a short Twitter message. Such a system could be used to monitor a Twitter feed and inform you about trending topics or the overall sentiment of your followers' responses to a post you made.

    Scene analysis and recognition: Machine learning models (particularly deep learning models) are commonly used for scene analysis and object recognition in digital images and video feeds. Scene analysis involves feeding in an image (or a video) to the machine learning model and the model providing a description of the contents of the image—such as man riding a green bicycle on a sunny day. Object recognition involves feeding an image to the machine learning model and getting a list of objects detected in the image along with bounding boxes that represent the location of the objects in the image.

    Types of Machine Learning Systems

    There are several different ways in which you can go about classifying machine learning systems; one of the most common approaches is to base the classification on the manner in which the system is trained and the manner in which the system can make predictions. Based on this approach, machine learning systems can be classified as follows:

    Supervised learning

    Unsupervised learning

    Semisupervised learning

    Reinforcement learning

    Batch learning

    Incremental learning

    Instance-based learning

    Model-based learning

    These labels are not mutually exclusive; it is quite common to come across a machine learning system that falls into multiple categories. For example, a system that uses behavioral usage data to detect potential fraudsters on a banking website could be classified as a supervised model–based machine learning system. Let's examine these classification labels in more detail.

    Supervised Learning

    Supervised learning refers to the training phase of the machine learning system. During the supervised training phase, the machine learning algorithm is presented with sets of training data. Each set consists of inputs that the algorithm should use to make predictions as well as the desired (correct) result (see Figure 1.1).

    Illustration of supervised learning in which the machine learning algorithm is presented with sets of training data. Each set consists of inputs that the algorithm uses to make predictions as well as the correct result.

    FIGURE 1.1 Supervised learning

    Training consists of iterating over each set, presenting the inputs to the algorithm, and comparing the output of the algorithm with the desired result. The difference between the actual output and the desired output is used to adjust parameters of the algorithm to make the output of the algorithm closer to (or equal to) the desired output.

    Human supervision is typically needed to define the desired output for each input in the training set. Once the algorithm has learned to make predictions on the training set, it can make predictions on data it has not previously encountered.

    Most real-world machine learning applications are trained using supervised learning techniques. Some applications of supervised learning are the following:

    Finding objects in digital images

    Spam filtering

    Predicting the possibility of developing a medical condition based on lifestyle factors

    Predicting the likelihood of a financial transaction being fraudulent

    Predicting the price of property

    Recommending a product to a customer based on historical purchasing data

    A music streaming servicing suggesting a song to a customer based on what the customer has been listening to

    Unsupervised Learning

    Unsupervised learning also refers to the training phase of a machine learning system. However, unlike supervised learning, the algorithm is not given any information on the class/category associated with each item in the training set. Unsupervised learning algorithms are used when you want to discover new patterns in existing data. Unsupervised learning algorithms fall into two main categories:

    Clustering: These algorithms group the input data into a number of clusters based on patterns in the data. Visualizing these clusters can give you helpful insight into your data. Figure 1.2 shows the results of a clustering algorithm applied to the data on the heights and ages of children under six years of age. Some of the most popular clustering algorithms are k-means clustering and Hierarchical Cluster Analysis (HCA).

    Scatterplot illustration of clustering technique used to find patterns in the data.

    FIGURE 1.2 Clustering technique used to find patterns in the data

    Dimensionality reduction: These algorithms are used to combine a large number of input features into a smaller number of features without losing too much information, Typically the features that are combined have a high degree of correlation with each other. Dimensionality reduction reduces the

    Enjoying the preview?
    Page 1 of 1