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

Visualizing heat maps

Let's look at how to visualize heat maps in this recipe. This is a pictorial representation of data where two groups are associated point by point. The individual values that are contained in a matrix are represented as color values in the plot.

How to do it…

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

```import numpy as np
import matplotlib.pyplot as plt```
2. Define the two groups:

```# Define the two groups
group1 = ['France', 'Italy', 'Spain', 'Portugal', 'Germany']
group2 = ['Japan', 'China', 'Brazil', 'Russia', 'Australia']```
3. Generate a random 2D matrix:

```# Generate some random values
data = np.random.rand(5, 5)```
4. Create a figure:

```# Create a figure
fig, ax = plt.subplots()```
5. Create the heat map:

```# Create the heat map
heatmap = ax.pcolor(data, cmap=plt.cm.gray)```
6. Plot these values:

```# Add major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1]) + 0.5, minor=False)

# Make it look like a table
ax.invert_yaxis...```