Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Deep Learning from the Basics

You're reading from   Deep Learning from the Basics Python and Deep Learning: Theory and Implementation

Arrow left icon
Product type Paperback
Published in Mar 2021
Publisher Packt
ISBN-13 9781800206137
Length 316 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Shigeo Yushita Shigeo Yushita
Author Profile Icon Shigeo Yushita
Shigeo Yushita
Koki Saitoh Koki Saitoh
Author Profile Icon Koki Saitoh
Koki Saitoh
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface Introduction 1. Introduction to Python FREE CHAPTER 2. Perceptrons 3. Neural Networks 4. Neural Network Training 5. Backpropagation 6. Training Techniques 7. Convolutional Neural Networks 8. Deep Learning Appendix A

Matplotlib

In deep learning experiments, drawing graphs and visualizing data is important. With Matplotlib, you can draw visualize easily by drawing graphs and charts. This section describes how to draw graphs and display images.

Drawing a Simple Graph

You can use Matplotlib's pyplot module to draw graphs. Here is an example of drawing a sine function:

import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.arange(0, 6, 0.1) # Generate from 0 to 6 in increments of 0.1
y = np.sin(x)
# Draw a graph
plt.plot(x, y)
plt.show()

Here, NumPy's arange method is used to generate the data of [0, 0.1, 0.2, …, 5.8, 5.9] and name it x. NumPy's sine function, np.sin(), is applied to each element of x, and the data rows of x and y are provided to the plt.plot method to draw a graph. Finally, a graph is displayed by plt.show(). When the preceding code is executed, the image shown in Figure 1.3 is displayed:

Figure 1.3: Graph of a sine function
Figure 1.3: Graph of a sine function

Features of pyplot

Here, we will draw a cosine function (cos) in addition to the sine function (sin) we looked at previously. We will use some other features of pyplot to show the title, the label name of the x-axis, and so on:

import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.arange(0, 6, 0.1) # Generate from 0 to 6 in increments of 0.1
y1 = np.sin(x)
y2 = np.cos(x)
# Draw a graph
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle = "--", label="cos") # Draw with a dashed line
plt.xlabel("x") # Label of the x axis
plt.ylabel("y") # Label of the y axis
plt.title('sin & cos') # Title
plt.legend()
plt.show()

Figure 1.4 shows the resulting graph. You can see that the title of the graph and the label names of the axes are displayed:

Figure 1.4: Graph of sine and cosine functions
Figure 1.4: Graph of sine and cosine functions

Displaying Images

The imshow() method for displaying images is also provided in pyplot. You can use imread() in the matplotlib.image module to load images, as in the following example:

import matplotlib.pyplot as plt 
from matplotlib.image import imread
img = imread('lena.png') # Load an image (specify an appropriate path!) 
plt.imshow(img)
plt.show()

When you execute this code, the image shown in Figure 1.5 is displayed:

Figure 1.5: Displaying an image
Figure 1.5: Displaying an image

Here, it is assumed that the image, lena.png, is located in the current directory. You need to change the name and path of the file as required, depending on your environment. In the source code provided with this book, lena.png is located under the dataset directory as a sample image. For example, to execute the preceding code from the ch01 directory in the Python interpreter, change the path of the image from lena.png to ../dataset/lena.png for proper operation.

You have been reading a chapter from
Deep Learning from the Basics
Published in: Mar 2021
Publisher: Packt
ISBN-13: 9781800206137
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image