Data Analysis using Jupyter Notebooks Part 2

Benjamin J. Morgan

Tutorial 3

2D numpy arrays

In all the examples using numpy arrays that you have seen so far, these arrays have been one dimensional.

It can often be useful to collect different data together in a table.

One way to do this is by combining one-dimensional numpy arrays into larger, two-dimensional, arrays.

import numpy as np

# create an array `x` with the integers 1 to 5

x = np.arange(1,6)

# create three new arrays by performing calculations on `x`

y1 = x**2

y2 = x+3

y3 = x/2 + 1

print('x=',x)

print('y1=',y1)

print('y2=',y2)

print('y3=',y3)
In [ ]:
 

You can combine numpy arrays into a table as columns using np.column_stack()

# combine x, y1, y2, and y3 as columns in a new table

column_table = np.column_stack( ( x, y1, y2, y3 ) )

print( column_table )
In [ ]:
 

Or as rows using np.row_stack()

# arrange x, y1, y2, and y3 as rows in a new table

row_table = np.row_stack( ( x, y1, y2, y3 ) )

print( row_table )
In [ ]:
 

Remember that a 1D numpy array can be indexed like a list.

my_1D_array = np.array( [ 1, 2, 3, 4, 5, 6] )

my_1D_array[2:5] 

# [2:5] selects from 2 jumps, up to, but not including, 5 jumps
In [ ]:
 

A 2D numpy array can be treated like a list of lists, and indexing returns selected rows.

row_table[1] # return the 2nd row (1 jump from the start)
In [ ]:
 

Because each row is a 1D numpy array, you can use a second index to select a single entry.

row_table[1][3]
In [ ]:
 

These two indices can be combined into a single bracket, where the first value selects one of more rows, and the second value selects one or more columns.

row_table[1,3]
In [ ]:
 

To select a single row, we make use of the range character :. Remember, for a list or 1D array, : lets us select a range of elements, and leaving out one of the numbers selects all elements up to the start, or end, of the list.

my_list = [ 'a', 'b', 'c', 'd', 'e' ]

my_list[1:]
In [ ]:
 

Leaving out both numbers extends our selection up to both ends of the list or array.

my_list[:]
In [ ]:
 

For a 2D array, you can think of this as “every row” or “every column”.

print( row_table )

print()

print( row_table[:,3] ) # all rows, jump 3 columns
In [ ]:
 
Use a combination of row and column indexing to select [ 6., 7., 8.] from row_table
In [ ]:
 

You can also convert between 1D and 2D numpy arrays (or even higher dimensions) using np.reshape()

a = ( [ 1, 2, 3, 4, 5, 6 ] )

print( a )

b = np.reshape( a, (3, 2) ) # reshape into a 3×2 2D array

print( b )
In [ ]:
 

and back again

c = np.reshape( b, (6) )

print( c )
In [ ]:
 
Create a numpy 1D array containing the numbers 1–100.
Reshape this 1D array into a 10×10 2D array.
In [ ]: