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
About the Author
About the Reviewer

Creating a vector quantizer

You can use neural networks for vector quantization as well. Vector quantization is the N-dimensional version of "rounding off". This is very commonly used across multiple areas in computer vision, natural language processing, and machine learning in general.

How to do it…

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

    import numpy as np
    import matplotlib.pyplot as plt
    import neurolab as nl
  2. Let's load the input data from the data_vq.txt file:

    # Define input data
    input_file = 'data_vq.txt'
    input_text = np.loadtxt(input_file)
    data = input_text[:, 0:2]
    labels = input_text[:, 2:]
  3. Define a learning vector quantization (LVQ) neural network with two layers. The array in the last parameter specifies the percentage weightage to each output (they should sum up to 1):

    # Define a neural network with 2 layers:
    # 10 neurons in input layer and 4 neurons in output layer
    net =, 10, [0.25, 0.25, 0.25, 0.25])
  4. Train the LVQ neural network...