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

Operating on time series data

Now that we know how to slice data and extract various subsets, let's discuss how to operate on time series data. You can filter the data in many different ways. The pandas library allows you to operate on time series data in any way that you want.

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:

    # Input file containing data
    input_file = 'data_timeseries.txt'
  3. We will use both the third and fourth columns in this text file:

    # Load data
    data1 = convert_data_to_timeseries(input_file, 2)
    data2 = convert_data_to_timeseries(input_file, 3)
  4. Convert the data into a pandas data frame:

    dataframe = pd.DataFrame({'first': data1, 'second': data2})
  5. Plot the data in the given year range:

    # Plot data
    plt.title('Data overlapped on top...