NumPy Basics : Part 2
Tags: python, beginners, numpy
Published: 2022-02-21 05:11:00

This article is a continuation of my previous post on NumPy [here](https://dev.to/wanguiwaweru/numpy-basics-part-1-47e1).
> The NumPy ndarray class is used to represent both matrices and vectors. 
A **vector** is a one-dimensional array.
A **matrix** is a two-dimensional array. 
A **tensor** is a 3-D or higher dimensional array.

## NumPy Operations

- Adding an element to an array

Use [numpy.append()](https://numpy.org/doc/stable/reference/generated/numpy.append.html) to add an element to the end of an array.

It takes an array and the value to be appended and returns a new array with the value appended to the end.

```python
array_x = np.array([5,6,7,8,9]))
array1 = np.append(array_x, 1)
array1
```
Output: `array([ 5,  6,  7,  8,  9, 1])`

- Removing an element from an array

Use [numpy.delete()](https://numpy.org/doc/stable/reference/generated/numpy.delete.html) to remove the element at a particular index.
The method takes in an array and the index of the value to be deleted and retuns a new aray with the value deleted.
 
```python
array2 = np.delete(array1,2)
array2
```
Output:`array([ 5,  6,  8,  9, 1])`

- Sorting an array.

```python
array3 = np.sort(array2)
array3
```
Output:`array([1, 5, 6, 8, 9])`

- Reshaping an array

Use [np.reshape()](https://numpy.org/doc/stable/reference/generated/numpy.reshape.html) which takes the array to be reshaped and a tuple of the new shape and returns a copy of the new shaped array.

The output must have the same number of elements.
For instance, a 3x4 array with 12 elements,can be reshaped to 6x2 or 4x3 or 2x6.
You can use numpy.reshape() directly on a numpy array.

```python
arr2 = np.reshape(array_A,(3,2))
arr2
```
Output: `array([[2, 4],[6, 1],[3, 5]])`

- Flattening an array 

Use flatten() to return a one-dimensional array.

```python
arr2.flatten()
```
Output:`array([2, 4, 6, 1, 3, 5])`

You can also use [ravel()](https://numpy.org/doc/stable/reference/generated/numpy.ravel.html) to flatten the array.

```python
array_A.ravel()
```
Output:`array([2, 4, 6, 1, 3, 5])`

**Note:** reshape(), flatten(),ravel() do not affect the original array.


## NumPy Arithmetic Operations

In this section, we will cover specific functions in NumPy used in arithmetic operations.
**Note:** You can only perform arithmetic operations if the arrays have the same structure and dimensions.


1.Addition
 
Use np.add() or the + arithmetic operator. The output is an ndarray object.

```python
import numpy as np
a = np.array([20,30,40,50,60])
b = np.array([4,5,6,7,8])
np.add(a, b)
```
Output: `array([24, 35, 46, 57, 68])`

```python
c = a+b
print(c)
print(type(c))
```
Output:
```
[24 35 46 57 68]
<class 'numpy.ndarray'>
```

2.Subtraction
Use np.subtract() or the -arithmetic operator.

```python
import numpy as np
a = np.array([20,3,40,50,60])
b = np.array([4,5,6,7,8])
c = a-b
print(c)
```
Output: `[16 -2 34 43 52]`

Using np.subtract() the second argument is subtracted from the first.
```python
import numpy as np
a = np.array([20,30,40,50,60])
b = np.array([4,5,6,7,8])
np.subtract(b, a)
```
Output:`array([-16, -25, -34, -43, -52])`

3.Multiplication

Use np.multiply() or the * arithmetic operator.

```python
import numpy as np
a = np.array([20,30,40,50,60])
b = np.array([4,5,6,7,8])

print(np.multiply(b, a))
print(a*b)
```
Output:
`[ 80 150 240 350 480]
 [ 80 150 240 350 480]
`
4.Division

Use np.divide() or using the / arithmetic operator.

```python
import numpy as np
a = np.array([20,30,40,50,60])
b = np.array([4,5,6,7,8])

print(np.divide(b, a))
print(a/b)
```
Output:
[5. 6. 6.66666667 7.14285714 7.5]
[5. 6. 6.66666667 7.14285714 7.5]

5. Statistical Functions

You can get the sum,mean, average and variance of all the elements in an array.

```python
import numpy as np

a = np.array([2.5,3.5 ,4.3,5.4,6.5])

print('The sum is ', np.sum(a))
print('The mean is ', np.mean(a))
print('The average is ', np.average(a))
print('The variance is ', np.var(a))
```

Output:
```
The sum is  22.200000000000003
The mean is  4.44
The average is  4.44
The variance is  1.9664000000000001
```
6.Power function
The power() function performs the power of two arrays where the first argument is the base raised to the power of the second argument.

```python
import numpy as np

a = np.array([2,3,4,5,6])
b = np.array([5,4,3,2,1])

print(np.power(a, b))
```
Output:
[32 81 64 25  6]

7.Remainder and Modulus

The remainder() function gives the remainder of the two arrays similar to the mod() function.

```python
import numpy as np

a = np.array([2,3,4,5,6])
b = np.array([5,4,3,2,1])

print(np.mod(a, b))
print(np.remainder(a,b))
```
Output:

```
[2 3 1 1 0]
[2 3 1 1 0]
```

8.Reciprocal
The reciprocal() function returns the reciprocal of each element in the array.

```python
import numpy as np

a = np.array([2.5,3.5 ,4.3,5.4,6.5])
print(np.reciprocal(a))
```
Output: 
`[0.4,0.28571429, 0.23255814, 0.18518519, 0.15384615]`

9.Minimum and Maximum

Use the min() function to get the minimum and the max() function to get the maximum.

```python
array_x = np.array([5,6,7,8,9])
print('The max is' ,array_x.max())
print('The min is' ,array_x.min())
```
Output:
```
The max is 9
The min is 5
```
This is wraps up the summary of some of the most commonly used NumPy operations.

## Additional Resources
- [Numpy](https://www.mlstack.cafe/blog/numpy-interview-questions)
- [Summary](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf)
- [Summary](https://s3.amazonaws.com/dq-blog-files/numpy-cheat-sheet.pdf)
- [Numpy](https://medium.com/@puneett.bhatnagr/50-numpy-interview-questions-along-with-detailed-answers-4e980135e324)