Book Image

Scala for Machine Learning

By : Patrick R. Nicolas
Book Image

Scala for Machine Learning

By: Patrick R. Nicolas

Overview of this book

Table of Contents (20 chapters)
Scala for Machine Learning
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Not a single day passes by 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 gold (knowledge).

This book covers the "what", "why", and "how" of machine learning:

  • What are the objectives and the mathematical foundation 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 formulation, 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, Hello World!, 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, focuses on the implementation of some of the most widely used clustering techniques, such as K-means, the expectation-maximization, and the principal component analysis as a dimension reduction method.

Chapter 5, Naïve Bayes Classifiers, introduces probabilistic graphical models, and then describes the implementation of the Naïve Bayes and the multivariate Bernoulli classifiers in the context of text mining.

Chapter 6, 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 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, 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 9, Artificial Neural Networks, describes feed-forward neural networks followed by a full implementation of the multilayer perceptron classifier.

Chapter 10, Genetic Algorithms, covers the basics of evolutionary computing and the implementation of the different components of a multipurpose genetic algorithm.

Chapter 11, Reinforcement Learning, 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 12, Scalable Frameworks, covers some of the artifacts and frameworks to create scalable applications for machine learning such as Scala parallel collections, Akka, and the Apache Spark framework.

Appendix A, Basic Concepts, covers the Scala constructs used throughout the book, elements of linear algebra, and an introduction to investment and trading strategies.

Appendix B, References, provides a chapter-wise list of references for [source entry] in the respective chapters. This appendix is available as an online chapter at https://www.packtpub.com/sites/default/files/downloads/8742OS_AppendixB_References.pdf.

Short test applications using financial data illustrate the large variety of predictive, regression, and classification models.

The interdependencies between chapters are kept to a minimum. You can easily delve into any chapter once you complete Chapter 1, Getting Started, and Chapter 2, Hello World!.

What you need for this book

A decent command of the Scala programming language is a prerequisite. Reading through a mathematical formulation, conveniently defined 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.10.3 or higher

  • Java JDK 1.7.0_45 or 1.8.0_25

  • SBT 0.13 or higher

  • JFreeChart 1.0.1

  • Apache Commons Math library 3.5 (Chapter 3, Data Preprocessing, Chapter 4, Unsupervised Learning, and Chapter 6, 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.2.4 or higher (or Typesafe activator 1.2.10 or higher) (Chapter 12, Scalable Frameworks)

  • Apache Spark 1.3.0 or higher (Chapter 12, Scalable Frameworks)

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.

This book is designed as a tutorial with comparative hands-on exercises using technical analysis of financial markets.

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.

Tip

Tips and tricks appear like this.

Note

Mathematical formulas (optional to read) appear in a box like this

For the sake of readability, the elements of the Scala code that are not essential to the understanding of an algorithm such as class, variable, and method qualifiers and validation of arguments, exceptions, or logging are omitted. The convention for code snippets is detailed in the Code snippets format section in Appendix A, Basic Concepts.

You will be provided with in-text citation of papers, conference, books, and instructional videos throughout the book. The sources are listed in the Appendix B, References using in the following format:

[In-text citation]

For example, in the chapter, you will find an instance as follows:

This time around RSS increases with before reaching a maximum for > 60. This behavior is consistent with other findings [6:12].

The respective [source entry] is mentioned in Appendix B, References, as follows:

[6:12] Model selection and assessment H. Bravo, R. Irizarry, 2010, available at http://www.cbcb.umd.edu/~hcorrada/PracticalML/pdf/lectures/selection.pdf.

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 from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

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.