Numpy is the main library for Python in regards to mathematics. You need to master it in perfection if you plan to connect your life with science, large data or artificial intelligence. In Python, the Numpy library is indispensable for working with numeric arrays, vectors and matrices, build graphs and histograms. Let’s get acquainted with the basic possibilities closer.
There are different ways to make arrays using numpy. For example, you can generate a normal list in Python or you can use the special array function:
a = np.array ([1,2,3])
The type of the array coincides with the type of elements. In this example, this will be int64. If the types are different then the array is assigned more accurate:
a = np.array ([1,2,3.5])
We get:
array ([1., 2., 3.5])
Note the form of the entry: without square quotes, the creation of a python numpy array is doomed to failure, since the system recognizes arguments in numbers and not sequence.
A common mistake is to call an array with several numeric arguments and not to provide one list of numbers as an argument.
a = np.array (1,2,3) # Wrong entry
a = np.array ([1,2,3]) # Correct
Creating a matrix is written in the same way:
a = np.array ([[1,2,3], [3,4,5]])
It is also possible to specify the type of the array at creation time:
a = np.array ([[0,1], [2,3]], dtype = complex)
Then the resulting unit matrix will have the form:
array ([[0. + 0.j, 1. + 0.j],
[2. + 0.j, 3. + 0.j]])
By the way, by default the dtype of the created array is float64.
Making an array, the elements are often unknown but the size is known. For such cases NumPy provides several functions for autocomplete:
- the numpy zeros function constructs an array of zeros;
- the ones function permits you to make an array filled with ones;
- the numpy empty function creates an array with random as original content and depends on the memory state.
- the numpy arange function allows you to make an array of numbers where the initial element is the first argument, the final one is the second argument, and the third argument is the step:
np.arange (1, 3, 0.5)
We get:
array ([1., 1.5, 2., 2.5])
In the case of arange, you need to be careful if the argument is a floating-point number. In this case, it is difficult to predict the exact number of elements. To solve the problem, the linspace function is better where instead of the size of the steps their number is used:
np.linspace (1, 2, 4)
We get:
array ([1., 1.25, 1.5, 1.75])
Basic Operations
Simple arithmetic operations are performed by arrays element-wise. A new array is created and populated with the result.
a = np.array ([[1,2], [3,4]])
b = np.array ([[4,5], [6,7]])
c = a + b;
We get:
array ([[5, 7], [9, 11]])
Using the * operator also means a numpy element-wise multiplication:
c = a * b
array ([[4, 10], [18, 28]])
In python numpy, matrix multiplication is performed using the dot operator:
a.dot (b)
As a result of multiplying the matrices, we get:
array ([[16, 19], [36, 43]])
As in other programming languages, in numpy operations with matrices, such as + = and * = replaces the elements in the current one rather than create a new instance. Many unary operations, for example the sum of all elements of an array can be performed using methods of the ndarray class:
a = np.array ([0,1,2])
a.sum ()
We get the number 3. Another method python numpy min – searches for the minimum value, max – max, sort – sorts in ascending order, etc. A complete list of methods can be found by clicking on the link.
Also in numpy there are familiar mathematical functions, such as sin, cos and exp, here they are called universal functions (ufunc):
np.exp (a)
We get:
array ([1., 2.71828183, 7.3890561])
Described – a very small part of the real capabilities of the library numpy python 3. If you are interested in this information. On the official resource you can find many more documents, examples, tutorials. In the future we will talk about the development of this library with a bias towards science – SciPy.