This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Creating NumPy Arrays and Exploring Their Attributes


We will directly start with NumPy without attempting to follow the normal approach of first learning Python and then moving up to NumPy. The reasons why I am following this approach is based on the following:

  1. Python is extremely easy to learn and use, and you can learn the Python programming language as you go along.
  2. Knowing Python is not a pre-requisite to begin using NumPy if you are ready to learn as you go.
  3. Accomplishing simple tasks early is a great motivator to learn. Spending some time learning Python when NumPy is your actual goal may be dampener to learning.

There are some extremely good resources to learn NumPy (at least, much better than this course) and you may find them useful as wel as insightful. Start with NumPy and SciPy official documentation.

Importing NumPy Module

Before using NumPy in your code, you must first import the NumPy library. However, if you are using IPython with the --pylab command line argument, this would have been already been imported into the default namespace. But importing explicitly a second time is not an error.

There are multiple ways a module can be imported. Here are some:

  1. Importing a module into its own namespace. All functions contained in the module must be called by referring to its module name immediately followed by the function name.

    import numpy
    x = numpy.array([1, 2, 3, 4, 5])
    print x
    [1 2 3 4 5]
  2. Importing a module into its own namespace and simultaneously creating an alias for the namespace. The namespace now can be referred to by its original module name or the assigned alias.

    import numpy as np
    x = np.array([1, 2, 3, 4, 5])
    print x
    [1 2 3 4 5]
  3. Importing a module into the default namespace. All functions in the imported module now become a part of the default namespace and can be called directly using their names.

    from numpy import *
    x = array([1, 2, 3, 4, 5])
    print x
    [1 2 3 4 5]
  4. Importing selected functions from a module into the default namespace. This will import only the selected functions from the module.

    from numpy import array
    x = array([1, 2, 3, 4, 5])
    print x
    [1 2 3 4 5]

The method you prefer is usually a matter of choice and may depend to some extent on the context under which you are using Python. Importing everything into the default namespace is may be a good idea while using Python interactively in a shell but is not a good idea when writing Python scripts.

Creating NumPy Arrays

In the following description, it is assumed that NumPy has been imported into the default namespace, merely to save some typing.

One-dimensioned Arrays

Let us create a one-dimensioned array x and study its properties:

x = array([1, 2, 3, 4, 5])
print type(x)
<type 'numpy.ndarray'>
print type(x[0])
<type 'numpy.int32'>
print ndim(x)
1
print x.shape
(5L,)
print len(x)
5
print size(x)
5

We can observe the following points:

  1. the NumPy function array() creates a n-dimensioned array numpy.ndarray, which is stored in the variable x.
  2. Each element of the array x  is of type numpy.int32
  3. Array x is one-dimensioned and has 5 elements
  4. The shape attribute shows that x has only one dimension and the size along the first dimension is 5
  5. The array x has a total of 5 elements, as shown by the size() function
  6. The size along the first dimension is 5, as shown by the len() function

Two-dimensioned Arrays

Observing the same attributes of a two-dimensioned array gives an insight into how Python implements n-dimensioned arrays.

y = array([ [1, 2, 3, 4], [5,6, 7, 8] ])
print type(y)

<type 'numpy.ndarray'>
print type(y[0, 0])
<type 'numpy.int32'>
print ndim(y)
2
print y.shape
(2L, 4L)
print len(y)
2
print size(y)
8

From the above, we can note the following points:

  1. Array y is of type numpy.ndarray and each element of y is of type numpy.int32
  2. Array y is two-dimensioned
  3. Array y has size 2 along the first dimension and size 4 along the second dimension. We usually refer to the first dimension as rows and the second dimension as columns. Thus array y has 2 rows and 4 columns
  4. Array y has the size 2 along the first dimension, as shown by the len() function
  5. Array y has  a total of 8 elements

Three-dimensioned Arrays

We could similarly create three-dimensioned or multi-dimensioned arrays and explore their properties. Here is an example of a three-dimensioned array to close this discussion:

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

  [13 14 15 16]
  [17 18 19 20]

  [21 22 23 24]]]
print type(z)
<type 'numpy.ndarray'>
print type(z[0, 0, 0])
<type 'numpy.int32'>
print ndim(z)
3
print z.shape
(2L, 3L, 4L)
print len(z)
2
print size(z)
24

The output above must be self explanatory.

Task Discussion