Book Image

Scala for Machine Learning, Second Edition - Second Edition

Book Image

Scala for Machine Learning, Second Edition - Second Edition

Overview of this book

The discovery of information through data clustering and classification is becoming a key differentiator for competitive organizations. Machine learning applications are everywhere, from self-driving cars, engineering design, logistics, manufacturing, and trading strategies, to detection of genetic anomalies. The book is your one stop guide that introduces you to the functional capabilities of the Scala programming language that are critical to the creation of machine learning algorithms such as dependency injection and implicits. You start by learning data preprocessing and filtering techniques. Following this, you'll move on to unsupervised learning techniques such as clustering and dimension reduction, followed by probabilistic graphical models such as Naïve Bayes, hidden Markov models and Monte Carlo inference. Further, it covers the discriminative algorithms such as linear, logistic regression with regularization, kernelization, support vector machines, neural networks, and deep learning. You’ll move on to evolutionary computing, multibandit algorithms, and reinforcement learning. Finally, the book includes a comprehensive overview of parallel computing in Scala and Akka followed by a description of Apache Spark and its ML library. With updated codes based on the latest version of Scala and comprehensive examples, this book will ensure that you have more than just a solid fundamental knowledge in machine learning with Scala.
Table of Contents (27 chapters)
Scala for Machine Learning Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Index

Preface

Not a single day passes that we do not hear about big data in the news media, technical conferences, and even coffee shops. The ever-increasing amount of data collected in process monitoring, research, or simple human behavior becomes valuable only if you extract knowledge from it. Machine learning is the essential tool to mine data for knowledge. This book covers the what, why, and how of machine learning:

  • What are the objectives and the mathematical foundations of machine learning?

  • Why is Scala the ideal programming language to implement machine learning algorithms?

  • How can you apply machine learning to solve real-world problems?

Throughout this book, machine learning algorithms are described with diagrams, mathematical formulations, and documented snippets of Scala code, allowing you to understand these key concepts in your own unique way.

What this book covers

Chapter 1, Getting Started, introduces the basic concepts of statistical analysis, classification, regression, prediction, clustering, and optimization. This chapter covers the Scala languages, features, and libraries, followed by the implementation of a simple application.

Chapter 2, Data Pipelines, describes a typical workflow for classification, the concept of bias/variance trade-off, and validation using the Scala dependency injection applied to the technical analysis of financial markets.

Chapter 3, Data Preprocessing, covers time series analyses and leverages Scala to implement data preprocessing and smoothing techniques such as moving averages, discrete Fourier transform, and the Kalman recursive filter.

Chapter 4, Unsupervised Learning, covers key clustering methods such as K-means clustering, Gaussian mixture Expectation-Maximization and function approximation.

Chapter 5, Dimension Reduction, describes the Kullback-Leibler divergence, the principal component analysis for linear models followed by an overview of manifold applied to non-linear models.

Chapter 6, Naive Bayes Classifiers, focuses on the probabilistic graphical models and more specifically the implementation of Naive Bayes models and its application to text mining.

Chapter 7, Sequential Data Models, introduces the Markov processes followed by a full implementation of the hidden Markov model, and conditional random fields applied to pattern recognition in financial market data.

Chapter 8, Monte Carlo Inference, describes Gaussian sampling using Box-Muller technique, Bootstrap replication with replacement, and the ubiquitous Metropolis-Hastings algorithm for Markov Chain Monte Carlo.

Chapter 9, Regression and Regularization, covers a typical implementation of the linear and least squares regression, the ridge regression as a regularization technique, and finally, the logistic regression.

Chapter 10, Multilayer Perception, describes feed-forward neural networks followed by a full implementation of the multilayer perceptron classifier.

Chapter 11, Deep Learning, implements a sparse auto encoder and a restricted Boltzmann machines for dimension reduction in Scala followed by an overview of the convolutional neural network.

Chapter 12, Kernel Models and Support Vector Machines, covers the concept of kernel functions with implementation of support vector machine classification and regression, followed by the application of the one-class SVM to anomaly detection.

Chapter 13, Evolutionary Computing, covers describes the basics of evolutionary computing and the implementation of the different components of a multipurpose genetic algorithm.

Chapter 14, Multiarmed Bandits, Multiarmed Bandits, introduces the concept of exploration-exploitation trade-off using Epsilon-greedy algorithm, the Upper confidence bound technique and the context-free Thompson sampling.

Chapter 15, Reinforcement Learning, covers introduces the concept of reinforcement learning with an implementation of the Q-learning algorithm followed by a template to build a learning classifier system.

Chapter 16, Parallelism in Scala and Akka, describes some of the artifacts and frameworks to create scalable applications and evaluate the relative performance of Scala parallel collections and Akka-based distributed computation.

Chapter 17, Apache Spark MLlib, covers the architecture and key concepts of Apache Spark, machine learning leveraging resilient distributed datasets, reusable ML pipelines, extending MLlib with distributed divergences and an example of Spark streaming library.

Appendix A, Basic Concepts, describes the Scala language constructs used throughout the book, elements of linear algebra and optimization techniques.

Appendix B, References, provides a chapter-wise list of references [source, entry] for each chapter.

What you need for this book

A decent command of the Scala programming language is a prerequisite. Reading through a mathematical formulation, conveniently defied in an information box, is optional. However, some basic knowledge of mathematics and statistics might be helpful to understand the inner workings of some algorithms.

The book uses the following libraries:

  • Scala 2.11.8 or higher

  • Java 1.8.0_25

  • SBT 0.13 or higher

  • JFreeChart 1.0.17

  • Apache Commons Math library 3.5 (Chapter 3, Data Pre-processing, Chapter 4, Unsupervised Learning, and Chapter 9, Regression and Regularization)

  • Indian Institute of Technology Bombay CRF 0.2 (Chapter 7, Sequential Data Models)

  • LIBSVM 0.1.6 (Chapter 8, Kernel Models and Support Vector Machines)

  • Akka 2.3.8 or higher (or Typesafe activator 1.2.10 or higher) (Chapter 16, Parallelism in Scala and Akka)

  • Apache Spark 2.1.0 or higher (Chapter 17, Apache Spark MLlib)

    Note

    Understanding the mathematical formulation of a model is optional.

Who this book is for

This book is for software developers with a background in Scala programming who want to learn how to create, validate, and apply machine learning algorithms. The book is also beneficial to data scientists who want to explore functional programming or improve the scalability of their existing applications using Scala.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

"Finally, the environment variables JAVA_HOME, PATH, and CLASSPATH have to be updated accordingly."

A block of code is set as follows:

[default]
val lsp = builder.model(lrJacobian)
.weight(wMatrix)
.target(labels)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

[default]
val lsp = builder. model(lrJacobian)
.weight(wMatrix)
.target(labels)

The source code block is described using a reference number embedded as a code comment:

[default]
val lsp = builder. model(lrJacobian) //1
.weight(wMatrix)
.target(labels)

The reference number is used in the chapter as follows: "The model instance is initialized with the Jacobian matrix, lrJacobian (line 1)".

Any command-line input or output is written as follows:

sbt/sbt assembly

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The loss function is then known as the hinge loss."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ScalaforMachineLearningSecondEdition_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.