IjasAug. 25, 2019
Python has several modules. NumPy is one of its type. It is a module in python. The name is an acronym for "Numeric Python" or "Numerical Python". It is an extension module for Python, mostly written in C. This makes sure that the precompiled mathematical and numerical functions and functionalities of Numpy guarantee great execution speed, implementing multi-dimensional arrays and also matrices. These data structures guarantee efficient calculations with matrices and arrays. Like NumPy there is another module in python name as SciPy or scientific python. SciPy needs Numpy, as it is based on the data structures of Numpy and furthermore its basic creation and manipulation functions.
Both NumPy and SciPy are not part of a basic Python installation. They will be install after the Python installation. NumPy has to be installed before installing SciPy.
Take your terminal and type this command.
NumPys main object is the homogeneous multidimensional array.
OUTPUT:
OUTPUT:
OUTPUT:
The syntax of arange:
arange([start,]stop[, step], [, dtype=None])
It is similar to the python built in function range. Arrange returns evenly spaced values within a given interval. The values are generated within the half-open interval '[start,stop)'. The function is used with integers. But
arrange returns an ndarray rather than a list iterator as range does. If the 'start' parameter is not given, it will be set to 0. The end of the interval is determined by the parameter 'stop'. The spacing between two adjacent values of the output array is set with the optional parameter 'step'. The default value for 'step' is 1.
OUTPUT:
The syntax of linspace:
linspace(start,stop, num=50, endpoint=True, retstep=False)
Linspace returns an ndarray, consisting of 'num' equally spaced samples in the closed interval [start, stop] or the half-open interval [start,stop). If a closed or a half open interval will be returned, depends on whether 'endpoint' is True or False. The parameter 'start' defines the start value of the sequence which will be created. 'stop' will be the end value of the sequence, unless 'endpoint' is set to False. 'endpoint' is set to True (the default), 'stop' will be the last sample of the sequence. If the optional parameter 'retstep' is set, the function will also return the value of the spacing between adjacent values.
OUTPUT:
We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM). The only required condition is:
a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM. (i.e original size of array remains unchanged.)
OUTPUT:
We can use flatten method to get a copy of array collapsed into one dimension.
OUTPUT:
OUTPUT:
Contents of ndarray object is access and modify by indexing or slicing. That is, just like Python's in-built container objects.
Just like lists in python, NumPy arrays can be sliced. As arrays can be multidimensional, you need to specify a slice for each dimension of the array.
In this method, lists will be passing for indexing each dimension. One to one mapping of corresponding elements is done to construct a new arbitrary array.
This method is used when we want to pick elements from array which satisfy some condition.
OUTPUT:
NumPy supports a much more variety of numerical types than Python does. The following table shows different scalar data types defined in NumPy.
The data type object 'dtype' is an instance of numpy.dtype class. It will be create with Numpy.dtype. A data type object describes interpretation of fixed block of memory corresponding to an array, depending on the following aspects :
The byte order is decided by prefixing '<' or '>' to data type. '<' means that encoding is little-endian (least significant is stored in smallest address). '>' means that encoding is big-endian (most significant byte is stored in smallest address).
A dtype object is construct using the following syntax −
numpy.dtype(object, align, copy)
The parameters are −
OUTPUT:
Each built-in data type has a character code that uniquely identifies it.
Once you have created the arrays, you can do basic Numpy operations.
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
Now, let’s try adding the arrays. Unsurprisingly, the elements at the respective positions in the arrays will be add together.
OUTPUT:
All the arithmetic operations work in a similar way. You can also multiply or divide the arrays.
To perform a typical matrix multiplication (or matrix product), you can use the operator ‘@.’
OUTPUT:
This is how it works: the cell (1,1) (value: 13) in the output is a Sum-Product of Row 1 in matrix A (a two-dimensional array A) and Column 1 in matrix B.
Similarly, the cell (1,2) in the output is a Sum-Product of Row 1 in matrix A and Column 2 in matrix B.
And so on, the values will be populating for all the cells.
Next, let’s use the random function to generate a two-dimensional array.
OUTPUT:
There are several functions that you can use to perform arithmetic operations on this array. For example, the sum function adds all the values in the array and gives a scalar output.
OUTPUT:
The min function finds the lowest value in the array.
OUTPUT:
Numpy also provides logic functions like logical_and, logical_or etc., in a similar pattern to perform logical operations.
Logical_and:
OUTPUT:
Logical_or:
OUTPUT:
In addition to arithmetic operators, Numpy also provides functions to perform arithmetic operations. You can use functions like add, subtract, multiply, divide in order to perform array operations.