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

# Quick Ways to Create Arrays

There are other ways you can create arrays, other than explicitly typing the elements of the array using the array() function. Here are some of them.

## Arrays from Lists

Python provides functions range() and numpy.arange() to quickly generate containers with values with specified start value, constant interval and less than a specified end value. While range() generates a list, arange() generates a numpy.ndarray.

a = range(5)
print type(a)
<type 'list'>
print a
[0 1 2 3 4]
x = array(a)
print type(x)
<type 'numpy.ndarray'>
print x
[0 1 2 3 4]

Similarly, we can use arange() functin to directly generate a numpy.ndarray.

y = numpy.arange(5)
print type(y)
<type 'numpy.ndarray'>
print y
[0 1 2 3 4]

Another function related to range() and numpy.arange() is numpy.linspace(). It takes as input, a start value, an end value and the number of equally spaced points between the start and end values. To generate the same array as above, the command is

x = numpy.linspace(0, 4, 5)
print type(x)
<type 'numpy.ndarray'>
print x
[ 0.  1.  2.  3.  4.]

You will notice some differences:

1. Function numpy.linspace() returns a numpy.ndarray with elements of type numpy.float64
2. The end value is included in the range of values generated, and the increment depends on the number of points specified

## Arrays with All Elements Zeros

Let us create a one-dimensioned array with 5 elements, all of which are zeros

x = numpy.zeros(5)
print type(x)
<type 'numpy.ndarray'>
print type(x)
<type 'numpy.float64'>
print x
[0. 0. 0. 0. 0.]
print x.shape
(5L,)

The same result could be achieved if we had used the following command to create the array:

x = numpy.zeros((5,))

The following points are worth noting:

1. Elements of array x are float64, not int32
2. The function zeros() takes as its input, the shape of the array to be created. When you wish to create a one dimensioned array, its shape can be input either as a single integer number or as a tuple (5,). When a tuple consists of only one element, the comma after the element is required

We can similarly create two- or three- or multi-dimensioned arrays with all elements zeros.

a = numpy.zeros((2, 4))
print a
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
b = numpy.zeros((2, 3, 4))
print b
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]

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

## Arrays with All Elements Ones

The function ones() is similar to the function zeros().

x = numpy.ones(5)
print x
[ 1. 1. 1. 1. 1.]
y = numpy.ones((2, 3))
print y
[[ 1. 1. 1.]
[ 1. 1. 1.]]
z = numpy.ones((2, 3, 4))
print z
[[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]

[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]]

## Arrays with Random Numbers as Elements

The NumPy function numpy.random.rand() creates arrays of specified shape whose elements are random numbers from a uniform distribution.

x = numpy.random.rand(2,3)
print x
[[ 0.39162673 0.88469785 0.45560386]
[ 0.12829358 0.24247348 0.70161319]]

Note the following points:

1. The input arguments indicate that the array created must be two-dimensioned, with two rows and three columns. That is, its shape must be (2, 3)
2. Elements are random numbers between 0.0 and 1.0 with a uniform distribution

## Identity Matrices

The function numpy.eye() creates a two-dimensioned array with the elements on the main diagonal unity and all others zeros. It can be used in two ways. When only one input argument is supplied, it creates a square identity matrix. When two input arguments are supplied, it creates a square or a rectangular matrix with elements along the main diagonal equal to unity.

a = numpy.eye(3)
print a
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
b = numpy.eye(3,3)
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
c = numpy.eye(3,4)
print c
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. 0.]]

## Arrays with Specified Elements Along Main Diagonal

The function numpy.diag() takes as its input a one-dimensioned array and places it along the main diagonal or along a diagonal above or below the main diagonal.

a = numpy.diag([1, 2, 3])
print a
[[1 0 0]
[0 2 0]
[0 0 3]]
b = numpy.diag([1, 2, 3], 1)
print b
[[0 1 0 0]
[0 0 2 0]
[0 0 0 3]
[0 0 0 0]]
c = numpy([1, 2, 3], -1)
print c
[[0 0 0 0]
[1 0 0 0]
[0 2 0 0]
[0 0 3 0]]

To specify that the input argument is to be placed above the main diagonal, the second argument must be positive and represents the offset of the diagonal from the main diagonal. If the second argument is negative, it is assumed to be an offset below the main diagonal.