Book Image

Python Machine Learning Cookbook

By : Prateek Joshi, Vahid Mirjalili
Book Image

Python Machine Learning Cookbook

By: Prateek Joshi, Vahid Mirjalili

Overview of this book

Machine learning is becoming increasingly pervasive in the modern data-driven world. It is used extensively across many fields such as search engines, robotics, self-driving cars, and more. With this book, you will learn how to perform various machine learning tasks in different environments. We’ll start by exploring a range of real-life scenarios where machine learning can be used, and look at various building blocks. Throughout the book, you’ll use a wide variety of machine learning algorithms to solve real-world problems and use Python to implement these algorithms. You’ll discover how to deal with various types of data and explore the differences between machine learning paradigms such as supervised and unsupervised learning. We also cover a range of regression techniques, classification algorithms, predictive modeling, data visualization techniques, recommendation engines, and more with the help of real-world examples.
Table of Contents (19 chapters)
Python Machine Learning Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

Machine learning is becoming increasingly pervasive in the modern data-driven world. It is used extensively across many fields, such as search engines, robotics, self-driving cars, and so on. In this book, you will explore various real-life scenarios where you can use machine learning. You will understand what algorithms you should use in a given context using this exciting recipe-based guide.

This book starts by talking about various realms in machine learning followed by practical examples. We then move on to discuss more complex algorithms, such as Support Vector Machines, Extremely Random Forests, Hidden Markov Models, Conditional Random Fields, Deep Neural Networks, and so on. This book is for Python programmers looking to use machine learning algorithms to create real-world applications. This book is friendly to Python beginners but familiarity with Python programming will certainly be helpful to play around with the code. It is also useful to experienced Python programmers who are looking to implement machine learning techniques.

You will learn how to make informed decisions about the types of algorithm that you need to use and how to implement these algorithms to get the best possible results. If you get stuck while making sense of images, text, speech, or some other form of data, this guide on applying machine learning techniques to each of these will definitely come to your rescue!

What this book covers

Chapter 1, The Realm of Supervised Learning, covers various supervised-learning techniques for regression. We will learn how to analyze bike-sharing patterns and predict housing prices.

Chapter 2, Constructing a Classifier, covers various supervised-learning techniques for data classification. We will learn how to estimate the income brackets and evaluate a car based on its characteristics.

Chapter 3, Predictive Modeling, discusses predictive-modeling techniques using Support Vector Machines. We will learn how to apply these techniques to predict events occurring in buildings and traffic on the roads near sports stadiums.

Chapter 4, Clustering with Unsupervised Learning, explains unsupervised learning algorithms, including k-means and Mean Shift clustering. We will learn how to apply these algorithms to stock market data and customer segmentation.

Chapter 5, Building Recommendation Engines, teaches you about the algorithms that we use to build recommendation engines. We will learn how to apply these algorithms to collaborative filtering and movie recommendations.

Chapter 6, Analyzing Text Data, explains the techniques that we use to analyze text data, including tokenization, stemming, bag-of-words, and so on. We will learn how to use these techniques to perform sentiment analysis and topic modeling.

Chapter 7, Speech Recognition, covers the algorithms that we use to analyze speech data. We will learn how to build speech-recognition systems.

Chapter 8, Dissecting Time Series and Sequential Data, explains the techniques that we use to analyze time series and sequential data including Hidden Markov Models and Conditional Random Fields. We will learn how to apply these techniques to text sequence analysis and stock market predictions.

Chapter 9, Image Content Analysis, covers the algorithms that we use for image content analysis and object recognition. We will learn how to extract image features and build object-recognition systems.

Chapter 10, Biometric Face Recognition, explains the techniques that we use to detect and recognize faces in images and videos. We will learn about dimensionality reduction algorithms and build a face recognizer.

Chapter 11, Deep Neural Networks, covers the algorithms that we use to build deep neural networks. We will learn how to build an optical character recognition system using neural networks.

Chapter 12, Visualizing Data, explains the techniques that we use to visualize various types of data in machine learning. We will learn how to construct different types of graphs, charts, and plots.

What you need for this book

There is a lot of debate going on between Python 2.x and Python 3.x. While we believe that the world is moving forward with better versions coming out, a lot of developers still enjoy using Python 2.x. A lot of operating systems have Python 2.x built into them. This book is focused on machine learning in Python as opposed to Python itself. It also helps in maintaining compatibility with libraries that haven't been ported to Python 3.x. Hence the code in the book is oriented towards Python 2.x. In that spirit, we have tried to keep all the code as agnostic as possible to the Python versions. We feel that this will enable our readers to easily understand the code and readily use it in different scenarios.

Who this book is for

This book is for Python programmers who are looking to use machine learning algorithms to create real-world applications. This book is friendly to Python beginners, but familiarity with Python programming will certainly be useful to play around with the code.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

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: "Here, we allocated 25% of the data for testing, as specified by the test_size parameter."

A block of code is set as follows:

import numpy as np
import matplotlib.pyplot as plt

import utilities 

# Load input data
input_file = 'data_multivar.txt'
X, y = utilities.load_data(input_file)

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

$ python object_recognizer.py --input-image imagefile.jpg --model-file erf.pkl --codebook-file codebook.pkl

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: "If you change the explode array to (0, 0.2, 0, 0, 0), then it will highlight the Strawberry section."

Note

Warnings or important notes appear in a box like this.

Tip

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:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Machine-Learning-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

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/PythonMachineLearningCookbook_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.