NumPy in Python. Part 2

  • Tutorial

Translator's Preface


We continue to translate the article on numpy in python. For those who have not read the first part, here: Part 1 . And everyone else - enjoy reading.



Other ways to create arrays


The arange function is similar to the range function, but returns an array:

>>> np.arange(5, dtype=float)
array([ 0.,  1.,  2.,  3.,  4.])
>>> np.arange(1, 6, 2, dtype=int)
array([1, 3, 5])

The zeros and ones functions create new dimensioned arrays filled with these values. These are probably the easiest to use functions for creating arrays:

>>> np.ones((2,3), dtype=float)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
>>> np.zeros(7, dtype=int)
array([0, 0, 0, 0, 0, 0, 0])

The zeros_like and ones_like functions can transform an already created array, filling it with zeros and ones, respectively:

>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> np.zeros_like(a)
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
>>> np.ones_like(a)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

There are also a number of functions for creating special matrices. To create a square matrix with the main diagonal, which is filled with units, we use the identity method:

>>> np.identity(4, dtype=float)
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

The eye function returns a matrix with ones on the k-th diagonal:

>>> np.eye(4, k=1, dtype=float)
array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.]])

Mathematical operations on arrays


When we use standard mathematical operations for arrays, the principle must be followed: element - element. This means that arrays must be the same size during addition, subtraction, and the like:

>>> a = np.array([1,2,3], float)
>>> b = np.array([5,2,6], float)
>>> a + b
array([6., 4., 9.])
>>> a – b
array([-4., 0., -3.])
>>> a * b
array([5., 4., 18.])
>>> b / a
array([5., 1., 2.])
>>> a % b
array([1., 0., 3.])
>>> b**a
array([5., 4., 216.])

For two-dimensional arrays, multiplication remains element-wise and does not correspond to matrix multiplication. There are special functions for this, which we will study later.

>>> a = np.array([[1,2], [3,4]], float)
>>> b = np.array([[2,0], [1,3]], float)
>>> a * b
array([[2., 0.], [3., 12.]])

If there is a discrepancy in size, errors are thrown:

>>> a = np.array([1,2,3], float)
>>> b = np.array([4,5], float)
>>> a + b
Traceback (most recent call last):
File "", line 1, in 
ValueError: operands could not be broadcast together with shapes (3,) (2,)

However, if the dimension of the arrays does not match, they will be converted to perform mathematical operations. This often means that a smaller array will be used several times to complete operations. Consider this example:

>>> a = np.array([[1, 2], [3, 4], [5, 6]], float)
>>> b = np.array([-1, 3], float)
>>> a
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
>>> b
array([-1.,  3.])
>>> a + b
array([[ 0.,  5.],
       [ 2.,  7.],
       [ 4.,  9.]])

Here, the one-dimensional array b was converted to a two-dimensional one, which corresponds to the size of the array a. Essentially, b was repeated several times, for each "line" a. Otherwise, it can be represented as follows:

array([[-1.,  3.],
       [-1.,  3.],
       [-1.,  3.]])

Python automatically converts arrays in this case. Sometimes, however, when a transformation plays a role, we can use the newaxis constant to change the transformation:

>>> a = np.zeros((2,2), float)
>>> b = np.array([-1., 3.], float)
>>> a
array([[ 0.,  0.],
       [0.,  0.]])
>>> b
array([-1., 3.])
>>> a + b
array([[-1.,  3.],
       [-1.,  3.]])
>>> a + b[np.newaxis,:]
array([[-1.,  3.],
       [-1.,  3.]])
>>> a + b[:,np.newaxis]
array([[-1., -1.],
       [ 3.,  3.]])

In addition to standard operators, numpy includes a library of standard mathematical functions that can be applied element-wise to arrays. Functions proper: abs, sign, sqrt, log, log10, exp, sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh, arcsinh, arccosh, and arctanh.

>>> a = np.array([1, 4, 9], float)
>>> np.sqrt(a)
array([ 1.,  2.,  3.])

The floor, ceil, and rint functions return the lower, upper, or closest (rounded) value:

>>> a = np.array([1.1, 1.5, 1.9], float)
>>> np.floor(a)
array([ 1.,  1.,  1.])
>>> np.ceil(a)
array([ 2.,  2.,  2.])
>>> np.rint(a)
array([ 1.,  2.,  2.])

Also included in numpy are two important mathematical constants:

>>> np.pi
3.1415926535897931
>>> np.e
2.7182818284590451

Iterating over array elements


You can iterate through arrays in the same way as lists:

>>> a = np.array([1, 4, 5], int)
>>> for x in a:
...   print x
1
4
5

For multidimensional arrays, iteration will be carried out along the first axis, so that each passage of the loop will return the "row" of the array:

>>> a = np.array([[1, 2], [3, 4], [5, 6]], float)
>>> for x in a:
...   print x
[ 1.  2.]
[ 3.  4.]
[ 5.  6.]

Multiple assignment is also available during iteration:

>>> a = np.array([[1, 2], [3, 4], [5, 6]], float)
>>> for (x, y) in a:
...   print x * y
2.0
12.0
30.0

Basic Array Operations


To obtain any properties of arrays, there are many functions. Elements can be summed or multiplied:

>>> a = np.array([2, 4, 3], float)
>>> a.sum()
9.0
>>> a.prod()
24.0

In this example, array functions were used. You can also use your own numpy functions:

>>> np.sum(a)
9.0
>>> np.prod(a)
24.0

For most cases, both options can be used.
Some functions make it possible to operate with statistical data. These are mean functions (arithmetic mean), variation and deviation:

>>> a = np.array([2, 1, 9], float)
>>> a.mean()
4.0
>>> 
a.var()
12.666666666666666
>>> a.std()
3.5590260840104371

You can find the minimum and maximum in the array:

>>> a = np.array([2, 1, 9], float)
>>> a.min()
1.0
>>> a.max()
9.0

The argmin and argmax functions return the index of the minimum or maximum element:

>>> a = np.array([2, 1, 9], float)
>>> a.argmin()
1
>>> a.argmax()
2

For multidimensional arrays, each of the functions can take an additional argument axis and, depending on its value, execute functions along a specific axis, placing the execution results in an array:

>>> a = np.array([[0, 2], [3, -1], [3, 5]], float)
>>> a.mean(axis=0)
array([ 2.,  2.])
>>> a.mean(axis=1)
array([ 1.,  1.,  4.])
>>> a.min(axis=1)
array([ 0., -1.,  3.])
>>> a.max(axis=0)
array([ 3.,  5.])

Like lists, arrays can be sorted:

>>> a = np.array([6, 2, 5, -1, 0], float)
>>> sorted(a)
[-1.0, 0.0, 2.0, 5.0, 6.0]
>>> a.sort()
>>> a
array([-1.,  0.,  2.,  5.,  6.])

The values ​​in the array can be “reduced” to belong to a given range. This is the same as applying min (max (x, minval), maxval) to each element of x:

>>> a = np.array([6, 2, 5, -1, 0], float)
>>> a.clip(0, 5)
array([ 5.,  2.,  5.,  0.,  0.])

Unique items can be extracted like this:

>>> a = np.array([1, 1, 4, 5, 5, 5, 7], float)
>>> np.unique(a)
array([ 1.,  4.,  5.,  7.])

For two-dimensional arrays, the diagonal can be obtained as follows:

>>> a = np.array([[1, 2], [3, 4]], float)
>>> a.diagonal()
array([ 1.,  4.])

So the second article came to an end. Thank you for your attention and good luck in your endeavors! See you soon.

Also popular now: