Skip to content

Numpy Arrays

Basic Ways to Build Arrays

We can cast an ordinary python list as a NumPy one-dimensional array.

import numpy as np
my_list = [1,2,3]
np.array(my_list)   # cast a list into a 1D array
Output:
array([1,2,3])

We can also cast a python list of lists to a NumPy two-dimensional array.

my_matrix= [[1,2,3],[4,5,6],[7,8,9]]
np.array(my_matrix) # cast a list of lists into a 2D array
Output:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Usually we will build arrays by using NumPy's constructors. For example, we can use arange(), which is analogous to pythons own range() command. Add an optional third argument for step size.

np.arange(0,10)
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

To create an array of 0's or 1's we can use the built in commands: .zeros() or .ones().

np.zeros(4)
Output:
array([0., 0., 0., 0.])

To create a two-dimensional array use a tuple for the number of rows and columns.

np.ones((2,3))
Output:
array([[1., 1., 1.],
       [1., 1., 1.]])

To create a two-dimensional identity matrix use the .eye() command.

np.eye(3)
Output:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

.linspace()

Another useful command for building arrays is .linspace(). This will create an array of a specified number of evenly spaced points between a starting and stopping value. For example, np.linspace(0,5,10) will create 10 evenly spaced out points starting at \(0\) and ending at 5.

np.linspace(0,5,10)
Output:
array([0.        , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
       2.77777778, 3.33333333, 3.88888889, 4.44444444, 5.        ])

In general, np.linspace(a,b,n+1) creates \(n+1\) points, \(a_0, a_1, \ldots,a_n\), starting at \(a\) and ending at \(b\), each spaced out by \(\Delta x = \frac{b-a}{n}\), where \(a_k = a_0 + k\Delta x\).

Building Random Arrays

NumPy has a few ways to build random number arrays. These methods are contained in the random library. In particular we will look at random.rand, random.randn, and random.randint.

To create an array of a specific length with random numbers sampled from a uniform distribution of [0,1] use the rand command.

np.random.rand(3)
Output:
array([0.48398785, 0.80470077, 0.29997432])

We can also create a matrix with random entries:

np.random.rand(2,3)
Output:
array([[0.01140483, 0.72514179, 0.30120971],
       [0.40598326, 0.73229916, 0.11843428]])

We can also get arrays of random samples from a standard normal distribution about 0 using the randn command.

np.random.randn(3)
Output:
array([ 0.43481184, -1.10779481, -0.6873335 ])

To generate a random integer from \(a\) (inclusive) to \(b\) (exclusive) use the randint command.

np.random.randint(1,100)
Output:
43

To generate an array of random integers use an optional third argument:

np.random.randint(1,100,10)
Output:
array([48, 80, 15, 60, 97, 15, 35, 67, 53, 90])

Shaping and Arrays

First we look at how to reshape an array. We start by generating an array of random integers, then calling the reshape method will produce a new array with the desired dimensions.

randarray = np.random.randint(0,50,10)
randarray.reshape(2,5)
Output:
array([[30, 49,  2,  4, 21],
       [16, 33,  0, 38, 40]])

The shape attribute will return the shape of the array.

arr = randarray.reshape(2,5)
arr.shape
Ouput:
(2,5)

Max and Min Values of an Arrays

There are useful methods for finding max or min values, or to find their index locations using argmin or argmax.

# recall randarray = [30,49,2,4,21,16,33,0,38,40] from above
randarray.min()
Output:
0
randarray.argmin()
Output:
7

The dtype attribute returns the datatype of data in the array.

randarray.dtype
Output:
dtype('int64')