Ccmmutty logo
Commutty IT
15 min read

02.02 The Basics Of NumPy Arrays

https://picsum.photos/seed/4636e92410054dadb04c89bd8806d536/600/800
Data manipulation in Python is nearly synonymous with NumPy array manipulation: even newer tools like Pandas (Chapter 3) are built around the NumPy array. This section will present several examples of using NumPy array manipulation to access data and subarrays, and to split, reshape, and join the arrays. While the types of operations shown here may seem a bit dry and pedantic, they comprise the building blocks of many other examples used throughout the book. Get to know them well!
We'll cover a few categories of basic array manipulations here:
  • Attributes of arrays: Determining the size, shape, memory consumption, and data types of arrays
  • Indexing of arrays: Getting and setting the value of individual array elements
  • Slicing of arrays: Getting and setting smaller subarrays within a larger array
  • Reshaping of arrays: Changing the shape of a given array
  • Joining and splitting of arrays: Combining multiple arrays into one, and splitting one array into many

NumPy Array Attributes

First let's discuss some useful array attributes. We'll start by defining three random arrays, a one-dimensional, two-dimensional, and three-dimensional array. We'll use NumPy's random number generator, which we will seed with a set value in order to ensure that the same random arrays are generated each time this code is run:
python
import numpy as np
np.random.seed(0)  # seed for reproducibility

x1 = np.random.randint(10, size=6)  # One-dimensional array
x2 = np.random.randint(10, size=(3, 4))  # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5))  # Three-dimensional array
Each array has attributes ndim (the number of dimensions), shape (the size of each dimension), and size (the total size of the array):
python
print("x3 ndim: ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
x3 ndim: 3 x3 shape: (3, 4, 5) x3 size: 60
Another useful attribute is the dtype, the data type of the array (which we discussed previously in Understanding Data Types in Python):
python
print("dtype:", x3.dtype)
dtype: int64
Other attributes include itemsize, which lists the size (in bytes) of each array element, and nbytes, which lists the total size (in bytes) of the array:
python
print("itemsize:", x3.itemsize, "bytes")
print("nbytes:", x3.nbytes, "bytes")
itemsize: 8 bytes nbytes: 480 bytes
In general, we expect that nbytes is equal to itemsize times size.

Array Indexing: Accessing Single Elements

If you are familiar with Python's standard list indexing, indexing in NumPy will feel quite familiar. In a one-dimensional array, the ithi^{th} value (counting from zero) can be accessed by specifying the desired index in square brackets, just as with Python lists:
python
x1
array([5, 0, 3, 3, 7, 9])
python
x1[0]
5
python
x1[4]
7
To index from the end of the array, you can use negative indices:
python
x1[-1]
9
python
x1[-2]
7
In a multi-dimensional array, items can be accessed using a comma-separated tuple of indices:
python
x2
array([[3, 5, 2, 4], [7, 6, 8, 8], [1, 6, 7, 7]])
python
x2[0, 0]
3
python
x2[2, 0]
1
python
x2[2, -1]
7
Values can also be modified using any of the above index notation:
python
x2[0, 0] = 12
x2
array([[12, 5, 2, 4], [ 7, 6, 8, 8], [ 1, 6, 7, 7]])
Keep in mind that, unlike Python lists, NumPy arrays have a fixed type. This means, for example, that if you attempt to insert a floating-point value to an integer array, the value will be silently truncated. Don't be caught unaware by this behavior!
python
x1[0] = 3.14159  # this will be truncated!
x1
array([3, 0, 3, 3, 7, 9])

Array Slicing: Accessing Subarrays

Just as we can use square brackets to access individual array elements, we can also use them to access subarrays with the slice notation, marked by the colon (:) character. The NumPy slicing syntax follows that of the standard Python list; to access a slice of an array x, use this:
x[start:stop:step]
If any of these are unspecified, they default to the values start=0, stop=size of dimension, step=1. We'll take a look at accessing sub-arrays in one dimension and in multiple dimensions.

One-dimensional subarrays

python
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
python
x[:5]  # first five elements
array([0, 1, 2, 3, 4])
python
x[5:]  # elements after index 5
array([5, 6, 7, 8, 9])
python
x[4:7]  # middle sub-array
array([4, 5, 6])
python
x[::2]  # every other element
array([0, 2, 4, 6, 8])
python
x[1::2]  # every other element, starting at index 1
array([1, 3, 5, 7, 9])
A potentially confusing case is when the step value is negative. In this case, the defaults for start and stop are swapped. This becomes a convenient way to reverse an array:
python
x[::-1]  # all elements, reversed
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
python
x[5::-2]  # reversed every other from index 5
array([5, 3, 1])

Multi-dimensional subarrays

Multi-dimensional slices work in the same way, with multiple slices separated by commas. For example:
python
x2

Discussion

コメントにはログインが必要です。