Magicode logo
Magicode
0

02.02 The Basics Of NumPy Arrays

15 min read

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:


Each array has attributes ndim (the number of dimensions), shape (the size of each dimension), and size (the total size of the array):


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):


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:


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:


array([5, 0, 3, 3, 7, 9])


5


7

To index from the end of the array, you can use negative indices:


9


7

In a multi-dimensional array, items can be accessed using a comma-separated tuple of indices:


array([[3, 5, 2, 4], [7, 6, 8, 8], [1, 6, 7, 7]])


3


1


7

Values can also be modified using any of the above index notation:


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!


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:

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


array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])


array([0, 1, 2, 3, 4])


array([5, 6, 7, 8, 9])


array([4, 5, 6])


array([0, 2, 4, 6, 8])


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:


array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])


array([5, 3, 1])

Multi-dimensional subarrays

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


Discussion

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