Array dimensionality
1. Array dimensionality
Now that we've created one- and two-dimensional arrays, let's dive into one of NumPy's key strengths: the ability to handle data of any dimension. Since data saved in higher dimensions can be harder to work with, we will also learn how to inspect and update array shapes. Let's dive in!2. 3D arrays
Just as we can create a 2D array by feeding NumPy a list of lists, we can create a 3D array by creating a list of lists of lists. Alternatively, we could create an array of 2D arrays. To do this, we pass a list of 2D arrays to np-dot-array. We can visualize a 3D array as a bunch of 2D arrays with the same shape stacked on top of each other.3. 4D arrays
Four-dimensional arrays can be harder to visualize since we don't have a fourth dimension. Think of a 4D array as a 2D array filled with 3D arrays.4. Vector arrays
Programmers and the NumPy documentation sometimes refer to arrays as vectors, matrices, or tensors. These are mathematical terms rather than NumPy terms; they all describe types of arrays. The difference between them is the number of dimensions an array has. A vector refers to an array with one dimension. There is no difference between row and column (or horizontal and vertical) vectors in NumPy since no second axis is specified for 1D arrays. To create an array that is explicitly horizontal or vertical, it must be a 2D array so that NumPy understands what axis it lies on, like the arrays on the right. Since these arrays are two-dimensional rather than one-dimensional, it would not be correct to call them vectors.5. Matrix and tensor arrays
Instead, in mathematics, a two-dimensional array is called a matrix. And an array with three or more dimensions is called a tensor.6. Shapeshifting
We're ready to learn our first array attribute and array methods! Array attributes are properties of an instance of an array, such as the array's shape. Array methods like dot-flatten and dot-reshape are called directly on the array object itself rather than passing the array as an argument like we do with NumPy functions such as np-dot-array.7. Finding an array's shape
dot-shape describes the shape of an array and returns a tuple of the length of each dimension. A shape of (3, 5) indicates a 2D array with three rows and five columns.8. Rows and columns
Referring to rows and columns only get us so far in NumPy, since many arrays have more than two dimensions. Instead of referring to rows, we can refer to the first dimension. Instead of referring to columns, we can refer to the second dimension. However, when dealing with 2D arrays it is commonplace to fall back on row and column terminology.9. Flattening an array
It's often easier to manipulate one-dimensional data than large multi-dimensional arrays. np-dot-flatten can help here: it takes all array elements and puts them in just one dimension inside a 1D array.10. Reshaping an array
The dot-reshape method allows us to redefine the shape of an array without changing the elements that make up the array. In this example, we take a 2D array with three rows and two columns and change it into a 2D array with two rows and three columns. The shape tuple passed to dot-reshape must be compatible with the number of elements in an array. This array could not be reshaped to a shape of (3, 3), since three times three is nine, and the array has only six elements.11. Let's practice!
Alright, time to play around with array dimensions on your own!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.