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

Slicing time series data


In this recipe, we will learn how to slice time series data using pandas. This will help you extract information from various intervals in the time series data. We will learn how to use dates to handle subsets of our data.

How to do it…

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

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    from convert_to_timeseries import convert_data_to_timeseries
  2. We will use the same text file that we used in the previous recipe to slice and dice the data:

    # Input file containing data
    input_file = 'data_timeseries.txt'
  3. We will use the third column again:

    # Load data
    column_num = 2
    data_timeseries = convert_data_to_timeseries(input_file, column_num)
  4. Let's assume that we want to extract the data between given start and end years. Let's define these, as follows:

    # Plot within a certain year range
    start = '2008'
    end = '2015'
  5. Plot the data between the given year range:

    plt.figure()
    data_timeseries[start:end].plot(...