Python for data analysis: Getting to grips with NumPy multidimensional arrays


NumPy stands for Numerical Python. It’s a library that contains a collection of routines for processing arrays.

An array, is a list. A multi dimensional array, is essentially a grid or table (it’s an array that contains 2 or more arrays).

All elements within a numpy array must be of the same datatype.

Import NumPy and generate some random data

In the below, we generate our N dimensional arrays and populate them with random data. You can see that by defining ‘np.random.randn(2,3), we’re creating an array with 2 dimensions, each of which have three elements. I’ve also added an example, where I’ve created a 5 dimensional array, where each dimension has 3 elements.

Basic Array Interaction

We can interact with our arrays. In the below example, I’ve multiplied our random_data2 array by 4. Each element of each dimension has been multiplied.

We can also add the array to itself. You can see below that I’ve added random_data to itself, doubling each of the element values.

Inspect our arrays

The ndarray has two main inspection functions – shape and dtype. Shape tells you how many dimensions are in the array & how many elements are in each dimension. The dtype function shows you the datatype of the array elements (as above, all elements must be the same data type).

Converting lists to ndarrays

In the below, I convert the list called ‘ages’ to an ndarray and then convert two lists (age_groups) to a 2 dimensional array.

Explicitly define array data type

In the below, I define our first array as being of type float64. I then proceed to change the type to int64.

Extracting data from ndarrays

In the below, I extract each of the two lists stored within the array:

Now, I select a specific element from within the array, within the array.

Multidimensional Arrays

An array within an array is called a multidimensional array. Let’s look at an example of a 2 dimensional array.

This is a two dimensional array as arrays only go to two levels: the main array and the manufacturer specific arrays. 2D arrays are important, as tables are presented in two dimensions:

Selling PriceSalesProfit

Math with arrays

Below, I show how we can multiply, divide, add and subtract two arrays. In every case, the index in array 1 is matched to the same index in array 2.For example, bmw[1] = 300 and nissan[1] = 321. So the result of the addition for position 1 of the output array is 621.

We can sum all elements in an array using the below: