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

Visualizing the characters in an optical character recognition database


We will now look at how to use neural networks to perform optical character recognition. This refers to the process of identifying handwritten characters in images. We will use the dataset available at http://ai.stanford.edu/~btaskar/ocr. The default file name after downloading is letter.data. To start with, let's see how to interact with the data and visualize it.

How to do it…

  1. Create a new Python file, and import the following packages:

    import os
    import sys
    
    import cv2
    import numpy as np
  2. Define the input file name:

    # Load input data 
    input_file = 'letter.data' 
  3. Define visualization parameters:

    # Define visualization parameters 
    scaling_factor = 10
    start_index = 6
    end_index = -1
    h, w = 16, 8
  4. Keep looping through the file until the user presses the Esc key. Split the line into tab-separated characters:

    # Loop until you encounter the Esc key
    with open(input_file, 'r') as f:
        for line in f.readlines():
            data = np.array...