- The Computer Vision Workshop
- Hafsa Asad Vishwesh Ravi Shrimali Nikhil Singh
- 1491字
- 2021-06-18 18:20:30
NumPy Arrays
Let's learn how to create a NumPy array in Python.
First, we need to import the NumPy module using the import numpy as np command. Here, np is used as an alias. This means that instead of writing numpy.function_name, we can use np.function_name.
We will have a look at four ways of creating a NumPy array:
- Array filled with zeros – the np.zeros command
- Array filled with ones – the np.ones command
- Array filled with random numbers – the np.random.rand command
- Array filled with values specified – the np.array command
Let's start with the np.zeros and np.ones commands. There are two important arguments for these functions:
- The shape of the array. For a 2D array, this is (number of rows, number of columns).
- The data type of the elements. By default, NumPy uses floating-point numbers for its data types. For images, we will use unsigned 8-bit integers – np.uint8. The reason behind this is that 8-bit unsigned integers have a range of 0 to 255, which is the same range that's followed for pixel values.
Let's have a look at a simple example of creating an array full of zeros. The array size should be 4x3. We can do this by using np.zeros(4,3). Similarly, if we want to create a 4x3 array full of ones, we can use np.ones(4,3).
The np.random.rand function, on the other hand, only needs the shape of the array. For a 2D array, it will be provided as np.random.rand(number_of_rows, number_of_columns).
Finally, for the np.array function, we provide the data as the first argument and the data type as the second argument.
Once you have a NumPy array, you can use npArray.shape to find out the shape of the array, where npArray is the name of the NumPy array. We can also use npArray.dtype to display the data type of the elements in the array.
Let's learn how to use these functions by completing the first exercise of this chapter.
Exercise 1.01: Creating NumPy Arrays
In this exercise, we will get some hands-on experience with the various NumPy functions that are used to create NumPy arrays and to obtain their shape. We will be using NumPy's zeros, ones, and rand functions to create the arrays. We will also have a look at their data types and shapes. Follow these steps to complete this exercise:
- Create a new notebook and name it Exercise1.01.ipynb. This is where we will write our code.
- First, import the NumPy module:
import numpy as np
- Next, let's create a 2D NumPy array with 5 rows and 6 columns, filled with zeros:
npArray = np.zeros((5,6))
- Let's print the array we just created:
print(npArray)
The output is as follows:
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
- Next, let's print the data type of the elements of the array:
print(npArray.dtype)
The output is float64.
- Finally, let's print the shape of the array:
print(npArray.shape)
The output is (5, 6).
- Print the number of rows and columns in the array:
Note
The code snippet shown here uses a backslash ( \ ) to split the logic across multiple lines. When the code is executed, Python will ignore the backslash, and treat the code on the next line as a direct continuation of the current line.
print("Number of rows in array = {}"\
.format(npArray.shape[0]))
print("Number of columns in array = {}"\
.format(npArray.shape[1]))
The output is as follows:
Number of rows in array = 5
Number of columns in array = 6
- Notice that the array we just created used floating-point numbers as the data type. Let's create a new array with another data type – an unsigned 8-bit integer – and find out its data type and shape:
npArray = np.zeros((5,6), dtype=np.uint8)
- Use the print() function to display the contents of the array:
print(npArray)
The output is as follows:
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
- Print the data type of npArray, as follows:
print(npArray.dtype)
The output is uint8.
- Print the shape of the array, as follows:
print(npArray.shape)
The output is (5, 6).
Note
The code snippet shown here uses a backslash ( \ ) to split the logic across multiple lines. When the code is executed, Python will ignore the backslash, and treat the code on the next line as a direct continuation of the current line.
- Print the number of rows and columns in the array, as follows:
print("Number of rows in array = {}"\
.format(npArray.shape[0]))
print("Number of columns in array = {}"\
.format(npArray.shape[1]))
The output is as follows:
Number of rows in array = 5
Number of columns in array = 6
- Now, we will create arrays of the same size, that is, (5,6), and the same data type (an unsigned 8-bit integer) using the other commands we saw previously. Let's create an array filled with ones:
npArray = np.ones((5,6), dtype=np.uint8)
- Let's print the array we have created:
print(npArray)
The output is as follows:
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
- Let's print the data type of the array and its shape. We can verify that the data type of the array is actually a uint8:
print(npArray.dtype)
print(npArray.shape)
The output is as follows:
uint8
(5, 6)
- Next, let's print the number of rows and columns in the array:
print("Number of rows in array = {}"\
.format(npArray.shape[0]))
print("Number of columns in array = {}"\
.format(npArray.shape[1]))
The output is as follows:
Number of rows in array = 5
Number of columns in array = 6
- Next, we will create an array filled with random numbers. Note that we cannot specify the data type while building an array filled with random numbers:
npArray = np.random.rand(5,6)
- As we did previously, let's print the array to find out the elements of the array:
print(npArray)
The output is as follows:
[[0.19959385 0.36014215 0.8687727 0.03460717 0.66908867 0.65924373] [0.18098379 0.75098049 0.85627628 0.09379154 0.86269739 0.91590054] [0.79175856 0.24177746 0.95465331 0.34505896 0.49370488 0.06673543] [0.54195549 0.59927631 0.30597663 0.1569594 0.09029267 0.24362439] [0.01368384 0.84902871 0.02571856 0.97014665 0.38342116 0.70014051]]
- Next, let's print the data type and shape of the random array:
print(npArray.dtype)
print(npArray.shape)
The output is as follows:
float64
(5, 6)
- Finally, let's print the number of rows and columns in the array:
print("Number of rows in array = {}"\
.format(npArray.shape[0]))
print("Number of columns in array = {}"\
.format(npArray.shape[1]))
The output is as follows:
Number of rows in array = 5
Number of columns in array = 6
- Finally, let's create an array that looks like the one shown in the following figure:
Figure 1.2: NumPy array
The code to create and print the array is as follows:
npArray = np.array([[1,2,3,4,5,6],
[7,8,9,10,11,12],
[13,14,15,16,17,18],
[19,20,21,22,23,24],
[25,26,27,28,29,30]],
dtype=np.uint8)
print(npArray)
The output is as follows:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]
[25 26 27 28 29 30]]
- Now, just like in the previous cases, we will print the data type and the shape of the array:
print(npArray.dtype)
print(npArray.shape)
print("Number of rows in array = {}"\
.format(npArray.shape[0]))
print("Number of columns in array = {}"\
.format(npArray.shape[1]))
The output is as follows:
uint8
(5, 6)
Number of rows in array = 5
Number of columns in array = 6
In this exercise, we saw how to create NumPy arrays using different functions, how to specify their data types, and how to display their shape.
Note
To access the source code for this specific section, please refer to https://packt.live/3ic9R30.
Now that we are armed with the prerequisites, let's formally jump into the world of image processing by discussing the building blocks of images – pixels.
- Mastering PHP Design Patterns
- Scratch 3游戲與人工智能編程完全自學(xué)教程
- JSP開發(fā)案例教程
- Web程序設(shè)計(jì)(第二版)
- C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo) (第2版)
- Advanced Express Web Application Development
- Go語言開發(fā)實(shí)戰(zhàn)(慕課版)
- Kubernetes進(jìn)階實(shí)戰(zhàn)
- Vue.js 3應(yīng)用開發(fā)與核心源碼解析
- 數(shù)據(jù)結(jié)構(gòu):Python語言描述
- Java語言程序設(shè)計(jì)實(shí)用教程(第2版)
- Software Architecture with Python
- Developing Java Applications with Spring and Spring Boot
- 多接入邊緣計(jì)算實(shí)戰(zhàn)
- PhoneGap 3.x Mobile Application Development Hotshot