Python and NumPy: Array Creation

Here we examine a range of functions commonly used to create NumPy Arrays.

Tutorial Video

Function array (Create an Array Manually)

So far we have seen how to load the NumPy library and create an array manually.

Python
[0 1 2 3 4]

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 1 & 2 & 3 & 4 \end{array}} \right]

There are however a number of functions to quickly create arrays.

Function arange (Creates an Array with a Lower Bound, Upper Bound and Specified Step Size)

For example the array above could be re-created using:

Python
[0 1 2 3 4]

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 1 & 2 & 3 & 4 \end{array}} \right]

The function arange creates integer steps from 0 to a specified maximum via a single input argument. Note as seen before with Pythons indexing, we specify a maximum value for instance in the case 5 and go up to this maximum value of 5 in integer steps but don’t include it, so in this case we go to 4.

Not too much time is saved with the code above, because we are only looking at a small number but imagine you wanted datapoints from 0 up to 10, 100, 1000 or more you can see immediately why a function like this would save you time.

Now if instead of starting from 0, you wanted to start from 1, you could specify two inputs arguments, the start value and the end value:

Python

Note that the code above is equivalent to the code below. One can explicitly state the names of the input arguments, which may help for learning or code readability or just input them if they know the function well.

Python
[1 2 3 4 5 6 7 8 9]

\displaystyle \left[ {\begin{array}{*{20}{r}} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \end{array}} \right]

This time we are seen to start at 1 and once again we go up to 10 in integer steps but don’t include 10 so stop at 9. If we want to specify our step size we can use a third input argument:

Python
array([1, 3, 5, 7, 9])

\displaystyle \left[ {\begin{array}{*{20}{r}} 1 & 3 & 5 & 7 & 9 \end{array}} \right]

This time we are starting from 1 and going up to 10 but not including 10, using steps of 2. If instead we used, this time we used 9 instead of 10 as the upper limit we would go up to 9 but not including 9, which is 8. However the step before 8 is 7 and 7 plus 2 is 9 which is higher than the upper bound of 8 meaning 7 is the last value:

Python
array([1, 3, 5, 7])

\displaystyle \left[ {\begin{array}{*{20}{r}} 1 & 3 & 5 & 7 \end{array}} \right]

For this reason arange, when using a specified lower limit, upper limit and step size may bias slightly more towards the lower bound.

Function linspace (Creates a Linearly Spaced Array)

If instead we wanted to specify a number of equally spaced steps between the upper bound of 9 and the lower bound of 8 we could use the function linspace. It has 3 input arguments; start, the starting point, stop, the point to stop at and num, the number of steps. Once again there is the option to explicitly state these inputs when calling the function:

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

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \end{array}} \right]

The input arguments for linspace are the lower bound, the upper bound and the number of spaces between the lower and upper bound. Note that linspace counts directly up to the upper bound and doesn’t stop a step before it like regular Python indexing or the arange function does. If we increase the number of steps to 19 we get:

Python
Python
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. ])

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & {0.5} & 1 & {1.5} & 2 & {2.5} & 3 & {3.5} & 4 & {4.5} & 5 & {5.5} & 6 & {6.5} & 7 & {7.5} & 8 & {8.5} & 9 \end{array}} \right]

Function zeros and ones (Creates Arrays of Zeros and Ones)

In some cases it is useful to have an array of zeros or ones. There are two inbuilt functions for these, the zeros and ones functions:

Python
[0. 0. 0. 0.]

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 0 & 0 & 0 \end{array}} \right]

Python
[1. 1. 1. 1.]

\displaystyle \left[ {\begin{array}{*{20}{r}} 1 & 1 & 1 & 1 \end{array}} \right]

zeros and ones can be used to create arrays of 0’s and 1’s respectively by using an input argument enclosed in ( ) specifying the number of rows and columns respectively:

Python
[[0. 0.]
 [0. 0.]
 [0. 0.]]

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 0 \\ 0 & 0 \\ 0 & 0 \end{array}} \right]

If 3 inputs are instead input, the 0th input, a stack is instead created, where the 0th element corresponds to the number of pages, the 1st element corresponds to the number of rows and the 2nd element corresponds to the number of columns:

Python
[[[0. 0. 0. 0.]
  [0. 0. 0. 0.]]

 [[0. 0. 0. 0.]
  [0. 0. 0. 0.]]

 [[0. 0. 0. 0.]
  [0. 0. 0. 0.]]]

The function ones works in a similar manner. Note there is no function twos, as twos can be created by multiplying ones by the scalar 2.

Function reshape (Reshapes Array)

The reshape function can be used to reshape an array. For instance we can input the array k and then specify its new dimensions (number of rows and number of columns) in a square bracket:

Python
[0 1 2 3 4 5 6 7 8 9]

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

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{r}} 0 & 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 & 9 \end{array}} \right]

This can also be used to create stacks:

Python
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])



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

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

Note reshape works with input arguments that aren’t vectors:

Python
[[1 2]
 [3 4]]

[[1]
 [2]
 [3]
 [4]]

[[1 2 3 4]]

\displaystyle \left[ {\begin{array}{*{20}{r}} 1 & 2 \\ 3 & 4 \end{array}} \right]\to \left[ {\begin{array}{*{20}{r}} 1 & 2 & 3 & 4 \end{array}} \right]\to \left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{r}} 1 & 2 \\ 3 & 4 \end{array}} \right]\to \left[ {\begin{array}{*{20}{r}} 1 & 2 & 3 & 4 \end{array}} \right]\to \left[ {\begin{array}{*{20}{r}} 1 & 2 & 3 & 4 \end{array}} \right]

When used on matrices, it selects the values in row order. The values in the first row are taken and then the values of the second row are appended and so on and so forth. They are then reshaped as specified.

Function diag (Diagonal)

It is also possible to quickly create a diagonal matrix using the diag function:

Python
[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

\displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 0 & 0 & 0 \\ 0 & 2 & 0 & 0 \\ 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 4 \end{array}} \right]

Function eye (Identity Matrix)

Or an identity matrix using the function eye. Recall the identity matrix is an important matrix as it leaves a matrix unchanged upon matrix multiplication:

Python
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

\displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array}} \right]

Function magic (Create a Magic Square)

A magic square is a square where all sides, including the diagonal sum to an individual value. Unfortunately it isn’t included in the numerical python library but there is a third party library for it. To install this library go to start and then open up your Anaconda folder and select Anaconda Windows Powershell. In the Powershell window, type in:

Shell

Once installed you can use, the function magic:

Python
array([[16,  2,  3, 13],
       [ 5, 11, 10,  8],
       [ 9,  7,  6, 12],
       [ 4, 14, 15,  1]])

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]

You can also use:

Python
True

34

Function transpose (Transposing a Matrix)

We can transpose a matrix using the function transpose. Here rows and columns are swapped and the transpose of a transposed matrix returns the original matrix:

Python
[[16  2  3 13]
 [ 5 11 10  8]
 [ 9  7  6 12]
 [ 4 14 15  1]]


[[16  5  9  4]
 [ 2 11  7 14]
 [ 3 10  6 15]
 [13  8 12  1]]


[[16  2  3 13]
 [ 5 11 10  8]
 [ 9  7  6 12]
 [ 4 14 15  1]]

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 5 & 9 & 4 \\ 2 & {11} & 7 & {14} \\ 3 & {10} & 6 & {15} \\ {13} & 8 & {12} & 1 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]

Because transposing is common, the following also works:

Python
[[16  5  9  4]
 [ 2 11  7 14]
 [ 3 10  6 15]
 [13  8 12  1]]

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 5 & 9 & 4 \\ 2 & {11} & 7 & {14} \\ 3 & {10} & 6 & {15} \\ {13} & 8 & {12} & 1 \end{array}} \right]

Function flip, fliplr and flipud (Flipping a Matrix)

The function fliplr flips the matrix left right and the function flipud flips the matrix up down. The function flip is used for vectors.

Python
[[16  2  3 13]
 [ 5 11 10  8]
 [ 9  7  6 12]
 [ 4 14 15  1]]

[[13  3  2 16]
 [ 8 10 11  5]
 [12  6  7  9]
 [ 1 15 14  4]]

[[16  2  3 13]
 [ 5 11 10  8]
 [ 9  7  6 12]
 [ 4 14 15  1]]

[[ 4 14 15  1]
 [ 9  7  6 12]
 [ 5 11 10  8]
 [16  2  3 13]]

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} {13} & 3 & 2 & {16} \\ 8 & {10} & {11} & 5 \\ {12} & 6 & 7 & 9 \\ 1 & {15} & {14} & 4 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} 4 & {14} & {15} & 1 \\ 9 & 7 & 6 & {12} \\ 5 & {11} & {10} & 8 \\ {16} & 2 & 3 & {13} \end{array}} \right]

Python
[0 1 2]

[2 1 0]

\displaystyle \left[ {\begin{array}{*{20}{c}} 0 & 1 & 2 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} 2 & 1 & 0 \end{array}} \right]

Function random (Random Number Generators)

Python can also generate random numbers. There are three different types rand or randf which generate random floats between 0 and 1, randf which generates random floats normalised around the origin (follow a normal distribution) and randint, which generates integer numbers to a specified lower and upper limit. A single input argument for these will create a row vector in this case of length 6, although it should be noted that randint requires the additional low and high input arguments to specify the range of integers for the random numbers to be generated over.

In these examples, the number generation seed has been initialised to 0. In essence computer generated numbers are not truly random like lottery numbers but follow a pre-set sequence. In this case, in order to yield the same results any time the program is ran, the seed should be set to its 0 value. If we wanted to test a program to see how it responds to a different set of random numbers, we could instead initialise the seed to some other number and retest our program.

I will revisit random numbers in more detail, when I put notes together on histogram plotting.

Python
[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548  0.64589411]

[ 1.76405235  0.40015721  0.97873798  2.2408932   1.86755799 -0.97727788]

[ 6  1  4  4  8 10]

[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548  0.64589411]

\displaystyle \left[ {\begin{array}{*{20}{r}} {0.5488135} & {0.71518937} & {0.60276338} & {0.54488318} & {0.4236548} & {0.64589411} \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{r}} {1.76405235} & {0.40015721} & {0.97873798} & {2.2408932} & {1.86755799} & {-0.97727788} \end{array}} \right]

[ 6 1 4 4 8 10]

\displaystyle \left[ {\begin{array}{*{20}{r}} {0.5488135} & {0.71518937} & {0.60276338} & {0.54488318} & {0.4236548} & {0.64589411} \end{array}} \right]

Once again multiple dimensions may be input to generate either matrices of random numbers or stacks of random numbers. The following code will generate randomly generated integers for 2 pages, 3 rows and 4 columns respectively from between 1 and 11, recalling that this means up to 11 but not including the value 11 itself, so the maximum value is 1 integer less than 11 which is 10.

Python
[[[ 6  1  4  4]
  [ 8 10  4  6]
  [ 3  5  8  7]]

 [[ 9  9  2  7]
  [ 8  8  9  2]
  [ 6 10  9 10]]]

Note for the other random functions which only have the dimensions of the arrays as input arguments, one does not require to enclose the dimensions in square brackets, so for instance if we wanted 2 rows and 4 columns of randomly distributed floats between 0 and 1 we could use:

Python
[[0.5488135  0.71518937 0.60276338 0.54488318]
 [0.4236548  0.64589411 0.43758721 0.891773  ]]

\displaystyle \left[ {\begin{array}{*{20}{r}} {0.5488135} & {0.71518937} & {0.60276338} & {0.54488318} \ {0.4236548} & {0.64589411} & {0.43758721} & {0.891773} \end{array}} \right]

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.