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.

- Array oriented computing.
- Efficiently implemented multi-dimensional arrays.
- Designed for scientific computation.

NumPys main object is the homogeneous multidimensional array.

- It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.
- In NumPy dimensions are called axes. The number of axes is rank.
- NumPys array class is called
**ndarray.**It is also known by the alias**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 :

- Type of data (integer, float or Python object).
- Size of data.
- Byte order (little-endian or big-endian).
- In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.
- If data type is a subarray, its shape and data type.

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 −

- Object − That will be convert to data type object.
- Align − If true, adds padding to the field to make it similar to C-struct.
- Copy − Makes a new copy of dtype object. If false, the result is reference to builtin data type object.

**OUTPUT:**

**Each built-in data type has a character code that uniquely identifies it.**

- 'b' − boolean
- 'i' − (signed) integer
- 'u' − unsigned integer
- 'f' − floating-point
- 'c' − complex-floating point
- 'm' − timedelta'M' − datetime
- 'O' − (Python) objects
- 'S', 'a' − (byte-)string
- 'U' − Unicode
- 'V' − raw data (void)

Once you have created the arrays, you can do basic Numpy operations.

- Let’s look at a one-dimensional array. I have 2 arrays, array1 and array2, created through two different techniques:

**OUTPUT:**

- You can also perform arithmetic operations on these arrays. For example, if you add the arrays, the arithmetic operator will work element-wise. The output will be an array of the same dimension.

**OUTPUT:**

- You can also run an arithmetic operation on the array with a scalar value. For example, this code multiplies each element of the array by 2.

**OUTPUT:**

- To find the square of the numbers, use **.

**OUTPUT:**

- If you would like to cube the individual elements, or even higher up, use the power function. Here, each element of the array is raise to the power 3.

**OUTPUT:**

- You can also use conditionals to find the values that match your criteria. Since array1 is an array, the result of a conditional operation is also an array. When we perform a conditional check, the output is an array of booleans.

**OUTPUT:**

- Let’s create 2 two-dimensional arrays, A and B.

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.