2. Numpy operations

There are a few NumPy operations that you should know. We are going to learn basic mathematical operations in this class.

import numpy as np

round()

The round function is used to round the numbers in an array.

Input:

In [8]
weight = [100.56, 300.76, 500.897, 200.453]
arr = np.array(weight)
arr = np.round(weight, 1)
arr

Output:

array([100.6, 300.8, 500.9, 200.5])

sum()

The sum function adds all the values in an array.

Input:

In [9]
np.sum(arr)

Output:

np.float64(1102.8)

max()

max finds the highest value.

Input:

In [10]
np.max(arr)

Output:

np.float64(500.9)

min()

min finds the lowest value.

Input:

In [11]
np.min(arr)

Output:

np.float64(100.6)

mean

mean is also called average (arithmetical average).

Input:

In [12]
np.mean(arr)

Output:

np.float64(275.7)

arange()

arange gives us an array in a range. It has 3 parameters:

  • start: where to start your range. Default value is 0.

  • stop: where to stop. No default value.

  • step: number of steps. Default value is 1.

Examples:

Input:

In [14]
arr = np.arange(10)
arr

Output:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Input:

In [15]
arr = np.arange(1, 11)
arr

Output:

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Input:

In [16]
arr = np.arange(1, 11, 2)
arr

Output:

array([1, 3, 5, 7, 9])

Print all even numbers from 1 to 100:

Input:

In [17]
arr = np.arange(2, 101, 2)
arr

Output:

array([  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34,
       36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68,
       70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])

random

NumPy's random module provides functions to generate random values:

  • random.randint() - random integers.

  • random.uniform() - random floats.

  • random.choice() - random values chosen from given choices.

random.randint()

Generates random integers.

Parameters:

  • low: lowest value

  • high: highest value

  • size: number of values

Input:

In [19]
arr = np.random.randint(1, 10, 5)
arr

Output:

array([3, 4, 7, 8, 5], dtype=int32)

random.uniform()

Generates random float values.

Parameters:

  • low: lowest value

  • high: highest value

  • size: number of values

Input:

In [21]
arr = np.random.uniform(1, 10, 5)
arr

Output:

array([2.9819103 , 3.0879235 , 3.69738972, 3.89013076, 5.73595479])

random.choice()

Chooses random values from given choices.

Input:

In [22]
arr = np.random.choice(['rock', 'paper', 'scissor'])
arr

Output:

np.str_('rock')

Input:

In [23]
arr = np.random.choice(['rock', 'paper', 'scissor'], 2)
arr

Output:

array(['rock', 'scissor'], dtype='<U7')

Example: Create a dictionary using random values

Input:

In [28]
d = {
  'name': ['raj','suresh','himesh','dharmesh','abhi','nihal','dipanshu'],
  'age': np.random.randint(20, 30, 7),
  'height': np.random.uniform(100, 200, 7),
  'weight': np.random.uniform(50, 90, 7),
  'attendance': np.random.choice(['yes', 'no'], 7)
}
print(d)

Output (example):

{'name': ['raj', 'suresh', 'himesh', 'dharmesh', 'abhi', 'nihal', 'dipanshu'],
 'age': array([25, 20, 21, 22, 24, 22, 27], dtype=int32),
 'height': array([120.89878702, 187.2947045 , 118.09080255, 188.44177107,
                  156.33514369, 156.65252408, 197.78203357]),
 'weight': array([74.9508736 , 60.0606787 , 82.93532573, 82.78538134, 89.32261961,
                  86.3725135 , 65.49127493]),
 'attendance': array(['no', 'no', 'yes', 'no', 'yes', 'yes', 'no'], dtype='<U3')}

Assignments

  1. Create a list of Maths scores.

  2. Convert the list into an array.

  3. Give the Total Score, Maximum Score, Minimum and Mean (Average) Score.

  4. Create a NumPy array of a table of 5 (e.g., multiples of 5 or a 5x5 table — interpret as needed).

  5. Generate a reverse array from 10 to 1.

  6. Generate an array of 20 random integers.

  7. Generate an array of 20 random floats.

  8. Create a dictionary of 5 people with random height, weight, age, hair color.

(You can implement these using the NumPy functions shown above.)