## Introduction to Python 3 NumPy

Python 3 NumPy is an array library. It also provides functions for working with matrices, Fourier transforms, and the domain of linear algebra. Travis Oliphant established NumPy in 2005. We can use it for free because it is open source. Numerical python is abbreviated as NumPy. Numpy has many characteristics; it is a Python library for working with arrays that can be used for various tasks.

### What is Python 3 NumPy?

- It’s the most important Python package for scientific computing. For example, NumPy may conduct logical and mathematical operations on arrays. So python has a package called NumPy.
- NumPy is frequently used in conjunction with other Python libraries such as SciPy and Matplotlib. This combination frequently replaces MatLab, a popular technical computing platform. On the other hand, python is today considered a more modern and comprehensive programming language than MatLab.

### Why Use Python 3 NumPy?

- Lists are similar to arrays in python, although they are slower to process. NumPy intends to provide a quicker array object than typical Python lists, up to 50 times faster.
- NumPy’s array object is named ndarray, and it comes with a slew of helper functions to make working with it a breeze.
- When resources and speed are essential in data research, arrays are widely employed.

### Python 3 NumPy Installation

Numerical python is the abbreviation of NumPy. It’s a multidimensional array object library with array processing routines.

The below step shows installing python 3 NumPy on the windows operating system.

- To install NumPy on windows, we need to run the below commands.

`pip install numpy`

The below example shows how NumPy used in python array are as follows.

#### 1. Arrays in NumPy

It’s a table with an integer that indexes a table of items of the same kind. Axes are what NumPy calls dimensions. The rank of the axes determines the number of them. Ndarray is the name of NumPy’s array class. The alias array also refers to it. The below example shows arrays in NumPy are as follows.

**Code:**

```
import numpy as py_np
py_arr = py_np.array ( [[ 13, 29, 36],
[ 43, 25, 59]] )
print ("Array type: ", type(py_arr))
print ("Dimension: ", py_arr.ndim)
print ("Shape: ", py_arr.shape)
print ("Size : ", py_arr.size)
print ("Element type: ", py_arr.dtype)
```

#### 2. Array creation by using NumPy

In NumPy, we may make arrays in a variety of ways. For example, the array function generates an array from a conventional Python list or tuple. The array element is frequently unknown initially, but its size is known. As a result, NumPy provides numerous functions for creating arrays with placeholder content.

This eliminates the need to build arrays, which is a costly process. NumPy has a range-like function is returns arrays for creating numerical sequences. linspace returns values that are evenly spaced within intervals. The number of elements returned is num.

The order argument is accepted. ‘C’ is the default value (for row-major order). For column-major order, type ‘F.’ The example below shows array creation using NumPy.

**Code:**

```
import numpy as py_np
py1 = py_np.array ([[15, 29, 42], [51, 85, 73]], dtype = 'float')
print ("Array list:\n", py1)
py2 = py_np.array ((19 , 34, 27))
print ("\nArray tuple:\n", py2)
py3 = py_np.zeros ((31, 45))
print ("\nAn Array initialized :\n", py3)
py4 = py_np.full ((32, 36), 62, dtype = 'complex')
print ("\nAn array initialized :\n", py4)
py5 = py_np.random.random ((23, 26))
print ("\nA Array:\n", py5)
py6 = py_np.arange (10, 34, 52)
print ("\nA Sequential array :\n", py6)
py7 = py_np.linspace (20, 52, 16)
print ("\nA Sequential array", py7)
py_ar = py_np.array ([[18, 23, 37, 46],
[55, 23, 48, 29],
[74, 22, 10, 19]])
py_narr = py_ar.reshape (21, 24, 36)
print ("\nArray:\n", py_ar)
print ("Reshaped arr:\n", py_narr)
py_ar = py_np.array ([[31, 23, 38], [45, 57, 61]])
py_farr = py_ar.flatten ()
print ("\nArray:\n", py_ar)
print ("Fattened :\n", py_farr)
```

#### 3. Array indexing using NumPy

NumPy has several methods for indexing arrays. Array if NumPy is sliced similarly to lists in python. We must define a slice because arrays can be multidimensional. Integer array indexing – This technique accepts lists as input for each dimension. One-to-one mapping of related elements is done to create a new arbitrary array.

When we wish to choose entries from an array that satisfies a criterion, we utilize the Boolean array indexing function. The below example shows array indexing by using NumPy.

**Code:**

```
import numpy as py_np
py_arr = py_np.array([[-11, 23, 0, 43],
[43, -0.5, 63, 0],
[3.6, 0, 79, 83],
[36, -72, 43, 8.0]])
py1 = py_arr[:2, ::2]
print ("Array :\n", py1)
py1 = py_arr[[0, 1, 2, 3], [3, 2, 1, 0]]
print ("\nElements :\n", py1)
py_con = py_arr > 0
py1 = py_arr[py_con]
print ("\n Element:\n", py1)
```

#### 4. Operation on a single array by using python 3 NumPy

We can utilize overloaded arithmetic operators to execute element-wise operations on an array and construct a new array. The below example shows that operations on the single array using python 3 NumPy are as follows.

**Code:**

```
import numpy as py_np
py1 = py_np.array([23, 29, 54, 36])
print ("Adding element:", py1+1)
print ("Subtracting element:", py1-3)
```

#### 5. Unary Operators

Setting an axis argument allows us to apply these functions row-by-row or column-by-column. The below example shows the unary operators are as follows.

**Code:**

```
import numpy as py_np
py_arr = py_np.array([[14, 58, 65],
[41, 76, 29],
[32, 13, 98]])
print ("Large element:", py_arr.max())
print ("Max element:", py_arr.max(axis = 1))
```

#### 6. Binary Operators

These operations are applied to the elements of an array, resulting in the creation of a new array. All basic arithmetic operators, such as +, -, /, and so forth, are available. When using the operators +=, -=, and =. The below example shows binary operators as follows.

**Code:**

```
import numpy as py_np
py1 = np.array([[24, 39],
[31, 46]])
py2 = np.array([[41, 37],
[26, 19]])
print ("Sum:\n", py1 + py2)
print ("Multiplication:\n", py1 * py2)
```

#### 7. Sorting Array

We are using the sort method to sort the method by using NumPy in python. The below example shows a sorting array.

**Code:**

```
import numpy as py_np
py1 = py_np.array([[19, 42, 29],
[39, 45, 67]]),
print ("Sorted array", py_np.sort (py1, axis = None))
```

### Conclusion

NumPy is frequently used in conjunction with other Python libraries such as SciPy and Matplotlib. Numerical python is abbreviated as NumPy. Numpy has many characteristics; it is a Python library for working with arrays that can be used for various tasks.

### Recommended Articles

This is a guide to Python 3 NumPy. Here we discuss the definition and why use Python 3 NumPy along with its step-by-step installation process. You may also have a look at the following articles to learn more –