素材巴巴 > 程序开发 >

Numpy Shape and Reshape

程序开发 2023-09-03 18:36:06

Numpy Shape and Reshape(-1)

In NumPy, -1 in reshape(-1) refers to an unknown dimension that the reshape() function calculates for you.

It is like saying: “I will leave this dimension for the reshape() function to determine”.

A common use case is to flatten a nested array of an unknown number of elements to a 1D array.

For example:

import numpy as np
 A = np.array([[1, 2], [3, 4]])
 B = A.reshape(-1) # result: [1, 2, 3, 4]
 

But this is not the only use case for reshape(-1). It can also be used the other way around.

For instance, let’s convert a 1D array to a 3D array with 2×2 elements:

import numpy as np
 A = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 B = A.reshape(2, 2, -1)
 print(B)
 

Output:

[[[1 2][3 4]][[5 6][7 8]]]
 

This is the quick answer.

However, if you are new to NumPy arrays and shapes, this is probably not enough to make things clear to you.

In this guide, we are going to take a look into the shape and reshaping process of a NumPy array.

You are going to learn:

What is the shape of a NumPy array.
What is reshaping and how does the reshape() function work.
What is reshaping with -1 (the “unknown dimension”).
What Is the Shape in NumPy?
In NumPy, matrices are commonly represented as nested NumPy arrays.

In this guide, I am going to use the words array and matrix interchangeably.

In NumPy, each array has a shape.

The shape is a tuple that represents the number of elements in each dimension.

Here are some examples of arrays of different shapes:

import numpy as np
 # 1 x 4 matrix
 A = np.array([1, 2, 3, 4]
 )
 print(A.shape) # returns (4,)
 # 2 x 3 matrix
 B = np.array([[1, 2, 3],[3, 2, 1]]
 )
 print(B.shape) # returns (2, 3)
 # three 2x4 matrices
 C = np.array([[[1, 2, 3, 4],[5, 6, 7, 8]],[[8, 7, 6, 5],[4, 3, 2, 1]],[[1, 2, 3, 4],[5, 6, 7, 8]],]
 )
 print(C.shape) # returns (3, 2, 4)
 

Let me explain what each of these shapes mean:

The matrix A represents a 1 x 4 vector. It has one dimension with four elements. Thus its shape is (4, ). (The trailing comma might look strange but it makes the shape return a tuple.)
The matrix B represents a 2 x 3 matrix. It is an array that contains two arrays with three numbers. Thus its shape is (2, 3).
2 refers to the number of rows in the matrix
3 refers to the number elements in each row.
The matrix C represents a matrix whose elements are 2 x 4 matrices. Thus the shape is (3, 2, 4). By the way, a matrix whose elements are matrices are called tensors. Anyway:
3 refers to the number of 2 x 4 matrices.
2 refers to the number of rows in each 2 x 4 matrix.
4 refers to the number of columns in each 2 x 4 matrix.
Scalar vs Vector vs Matrix vs Tensor
Now that you understand what is the shape of an array, let’s talk about reshaping.

What Is Reshaping in NumPy?
Reshaping an array means changing its shape.

In other words:

Adding/removing dimensions.
Adding/removing the number of elements in the dimensions.
Here is an illustration of reshaping a 1 x 6 vector to a 2 x 3 matrix:
Reshaping a 1 x 6 matrix to a 2 x 3 matrix

To reshape an array, there is a built-in function numpy.array.reshape() you can use.

Notice, however, that a successful reshaping requires getting the dimensions right.

For instance, you cannot convert an array that represents a 1 x 4 matrix to a 3 x 3 matrix unless you add new elements to it.

Anyway, let’s see an example of reshaping in action by converting a 1D array to a 2D array.

import numpy as np
 # 1 x 8 matrix
 A = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 # 2 x 4 matrix
 B = A.reshape(2, 4)
 print(B)
 

Output:

[[1 2 3 4][5 6 7 8]]
 

As another example, let’s convert a 1D array to a 3D array.

Given a 1D array of 12 elements, there are many ways you could reshape the array. One of which is to create three pieces of 2 x 2 matrices.

Here is how it looks in code:

import numpy as np
 # 1 x 12 matrix
 A = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
 # A matrix with three 2 x 2 matrices (a tensor)
 B = A.reshape(3, 2, 2)
 print(B)
 

As a result, you get an array of arrays of arrays. Or more shortly, a matrix that contains three 2 x 2 matrix elements:

[[[ 1  2][ 3  4]][[ 5  6][ 7  8]][[ 9 10][11 12]]]
 

Now you understand what reshaping means in the context of NumPy arrays.

Next, let’s take a look at a convenient shorthand you can use to determine a “missing dimension”.

The Unknown Dimension: How to Use -1 in the reshape() Function
The numpy.array.reshape() function can be called with an “unknown dimension”.

This is possible by specifying -1 as the unspecified dimension.

The reshape() function treats -1 as an unknown dimension that it calculates from the context.

Let’s go back to the examples in the previous chapter.

In the first example, we turned a 1 x 8 matrix into a 2 x 4 matrix using the reshape() function. When we did this, we specified both dimensions 2 and 4 into the reshape() function call.

B = A.reshape(2, 4)
 

However, the reshape() function is clever enough to determine the shape of the array without knowing all the dimensions.

This means you can only specify one of the two dimensions in the above example and leave the other “undefined” as -1.

For instance, given array A with 8 elements, you can convert it to a 2 x 4 array by:

B = A.reshape(2, -1)
 

This produces the exact same result as this expression:

B = A.reshape(2, 4)
 

The reshape() function knows that if the number of inner arrays is 2, and there are 8 elements to share, then each array must have 4 elements in the result.

As another example, let’s repeat the 1D to a 3D array example.

Given a 1D array A with 12 elements, you can turn it into a 3D (3 x 2 x 2) array by:

B = A.reshape(3, 2, 2)
 

However, as the reshape function knows how to infer one dimension from the context, you could use any one of these expressions to get the same result:

B = A.reshape(-1, 2, 2)
 B = A.reshape(3, -1, 2)
 B = A.reshape(3, 2, -1)
 

Awesome! Now you know how to reshape with the unknown dimension.

Finally, let’s take a look at how you can conveniently reshape a multidimensional array to a 1D one.

Flatten an Array with reshape(-1)
Calling reshape() with a single argument -1 flattens an array of any dimensions to a 1D array.

For instance:

import numpy as np
 # 2D array that represents a 4 x 3 matrix
 A = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
 B = A.reshape(-1)
 print(B)
 

Output:

[ 1  2  3  4  5  6  7  8  9 10 11 12]
 

Again, the reshape() function treats the -1 as an unknown dimension.

In other words, the reshape() function calculates the number of elements in the 1D array we are trying to produce.

Another way to flatten this array would be to specify the total number of elements in the multi-dimensional one:

import numpy as np
 # 2D array that represents a 4 x 3 matrix
 A = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
 B = A.reshape(12)
 print(B)
 

Output:

[ 1  2  3  4  5  6  7  8  9 10 11 12]
 

However, using -1 is more convenient as you do not need to know the exact number of elements in the array.

Before wrapping up, please remember two things:

To reshape an array, the desired new dimensions must make sense. Otherwise reshaping fails.
When reshaping, you can leave one and only one dimension out and use -1 instead.
Conclusion
Today you learned what -1 means in the reshape() function call with NumPy arrays.

To recap, -1 is an “unknown dimension”. The reshape() function calculates this dimension for you based on the context.

You can only specify one dimension as an “unknown dimension”.

Furthermore, the other dimensions must make sense for the reshape() function to work properly.


标签:

上一篇: js实现图片自动轮播 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。