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

Animating dynamic signals


When we visualize real-time signals, it's nice to look at how the waveform builds up. In this recipe, we will see how to animate dynamic signals and visualize them as they are encountered in real time.

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 matplotlib.animation as animation 
  2. Create a function to generate a damping sinusoid signal:

    # Generate the signal
    def generate_data(length=2500, t=0, step_size=0.05):
        for count in range(length):
            t += step_size
            signal = np.sin(2*np.pi*t)
            damper = np.exp(-t/8.0)
            yield t, signal * damper 
  3. Define an initializer function to initialize parameters of the plot:

    # Initializer function
    def initializer():
        peak_val = 1.0
        buffer_val = 0.1
  4. Set these parameters:

        ax.set_ylim(-peak_val * (1 + buffer_val), peak_val * (1 + buffer_val))
        ax.set_xlim(0, 10)
        del x_vals[:]
        del y_vals[:]
        line.set_data(x_vals...