Translator's Preface
Continuing to translate the numpy article in python. For those who have not read the first part, here:
Part 1 . And all the rest - 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 arrays with a set dimension 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 convert an already created array by 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 a 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 the ones on the diagonal kth line:
>>> 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 should be observed: element - element. This means that arrays must be the same size during addition, subtraction, and similar operations:
>>> 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, the multiplication remains element-wise and does not correspond to the multiplication of matrices. For this there are special features that we will explore 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 the size, errors are thrown out:
>>> a = np.array([1,2,3], float) >>> b = np.array([4,5], float) >>> a + b Traceback (most recent call last): File "<stdin>", line 1, in <module> 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 two-dimensional, which corresponds to the size of the array a. Essentially, b was repeated several times, for each "string" a. Otherwise, it can be represented as:
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 the standard operators, numpy includes a library of standard mathematical functions that can be applied elementwise to arrays. The functions themselves are 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 lower, upper, or closest (rounded) values:
>>> 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.])
Numpy also includes two important mathematical constants:
>>> np.pi 3.1415926535897931 >>> np.e 2.7182818284590451
Enumerating array elements
It is possible to iterate arrays like lists:
>>> a = np.array([1, 4, 5], int) >>> for x in a: ... print x 1 4 5
For multidimensional arrays, the iteration will be carried out along the first axis, so that each loop through will return the “string” 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 summarized or multiplied:
>>> a = np.array([2, 4, 3], float) >>> a.sum() 9.0 >>> a.prod() 24.0
In this example, the 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 the 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 axis argument and, depending on its value, perform functions on a specific axis, putting the results of the execution into 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 abbreviated to belong to a given range. This is the same as applying min (max (x, minval), maxval) to each element 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.])
Here is the second article came to an end. Thank you for your attention and good luck in your endeavors! See you soon.