DEV Community

Bernice Waweru
Bernice Waweru

Posted on • Edited on

NumPy Basics : Part 2

This article is a continuation of my previous post on NumPy here.

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() 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.

array_x = np.array([5,6,7,8,9]))
array1 = np.append(array_x, 1)
array1
Enter fullscreen mode Exit fullscreen mode

Output: array([ 5, 6, 7, 8, 9, 1])

  • Removing an element from an array

Use numpy.delete() 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.

array2 = np.delete(array1,2)
array2
Enter fullscreen mode Exit fullscreen mode

Output:array([ 5, 6, 8, 9, 1])

  • Sorting an array.
array3 = np.sort(array2)
array3
Enter fullscreen mode Exit fullscreen mode

Output:array([1, 5, 6, 8, 9])

  • Reshaping an array

Use np.reshape() 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.

arr2 = np.reshape(array_A,(3,2))
arr2
Enter fullscreen mode Exit fullscreen mode

Output: array([[2, 4],[6, 1],[3, 5]])

  • Flattening an array

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

arr2.flatten()
Enter fullscreen mode Exit fullscreen mode

Output:array([2, 4, 6, 1, 3, 5])

You can also use ravel() to flatten the array.

array_A.ravel()
Enter fullscreen mode Exit fullscreen mode

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.

import numpy as np
a = np.array([20,30,40,50,60])
b = np.array([4,5,6,7,8])
np.add(a, b)
Enter fullscreen mode Exit fullscreen mode

Output: array([24, 35, 46, 57, 68])

c = a+b
print(c)
print(type(c))
Enter fullscreen mode Exit fullscreen mode

Output:

[24 35 46 57 68]
<class 'numpy.ndarray'>
Enter fullscreen mode Exit fullscreen mode

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

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)
Enter fullscreen mode Exit fullscreen mode

Output: [16 -2 34 43 52]

Using np.subtract() the second argument is subtracted from the first.

import numpy as np
a = np.array([20,30,40,50,60])
b = np.array([4,5,6,7,8])
np.subtract(b, a)
Enter fullscreen mode Exit fullscreen mode

Output:array([-16, -25, -34, -43, -52])

3.Multiplication

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

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)
Enter fullscreen mode Exit fullscreen mode

Output:
[ 80 150 240 350 480]
[ 80 150 240 350 480]

4.Division

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

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)
Enter fullscreen mode Exit fullscreen mode

Output:
[5. 6. 6.66666667 7.14285714 7.5]
[5. 6. 6.66666667 7.14285714 7.5]

  1. Statistical Functions

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

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))
Enter fullscreen mode Exit fullscreen mode

Output:

The sum is  22.200000000000003
The mean is  4.44
The average is  4.44
The variance is  1.9664000000000001
Enter fullscreen mode Exit fullscreen mode

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.

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))
Enter fullscreen mode Exit fullscreen mode

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.

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))
Enter fullscreen mode Exit fullscreen mode

Output:

[2 3 1 1 0]
[2 3 1 1 0]
Enter fullscreen mode Exit fullscreen mode

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

import numpy as np

a = np.array([2.5,3.5 ,4.3,5.4,6.5])
print(np.reciprocal(a))
Enter fullscreen mode Exit fullscreen mode

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.

array_x = np.array([5,6,7,8,9])
print('The max is' ,array_x.max())
print('The min is' ,array_x.min())
Enter fullscreen mode Exit fullscreen mode

Output:

The max is 9
The min is 5
Enter fullscreen mode Exit fullscreen mode

This is wraps up the summary of some of the most commonly used NumPy operations.

Additional Resources

Top comments (0)