#### 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.
Python Machine Learning Cookbook
Credits
www.PacktPub.com
Preface
Free Chapter
The Realm of Supervised Learning
Visualizing Data
Index

## Plotting date-formatted time series data

Let's look at how to plot time series data using date formatting. This is useful in visualizing stock data over time.

### How to do it…

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

```import numpy
import matplotlib.pyplot as plt
from matplotlib.mlab import csv2rec
import matplotlib.cbook as cbook
from matplotlib.ticker import Formatter```
2. Define a function to format the dates. The `__init__` function sets the class variables:

```# Define a class for formatting
class DataFormatter(Formatter):
def __init__(self, dates, date_format='%Y-%m-%d'):
self.dates = dates
self.date_format = date_format```
3. Extract the value at any given time and return it in the following format:

```    # Extact the value at time t at position 'position'
def __call__(self, t, position=0):
index = int(round(t))
if index >= len(self.dates) or index < 0:
return ''

return self.dates[index].strftime(self.date_format)```
4. Define the...