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 npround()
The round function is used to round the numbers in an array.
Input:
weight = [100.56, 300.76, 500.897, 200.453]
arr = np.array(weight)
arr = np.round(weight, 1)
arrOutput:
array([100.6, 300.8, 500.9, 200.5])sum()
The sum function adds all the values in an array.
Input:
np.sum(arr)Output:
np.float64(1102.8)max()
max finds the highest value.
Input:
np.max(arr)Output:
np.float64(500.9)min()
min finds the lowest value.
Input:
np.min(arr)Output:
np.float64(100.6)mean
mean is also called average (arithmetical average).
Input:
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:
arr = np.arange(10)
arrOutput:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])Input:
arr = np.arange(1, 11)
arrOutput:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])Input:
arr = np.arange(1, 11, 2)
arrOutput:
array([1, 3, 5, 7, 9])Print all even numbers from 1 to 100:
Input:
arr = np.arange(2, 101, 2)
arrOutput:
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 valuehigh: highest valuesize: number of values
Input:
arr = np.random.randint(1, 10, 5)
arrOutput:
array([3, 4, 7, 8, 5], dtype=int32)random.uniform()
Generates random float values.
Parameters:
low: lowest valuehigh: highest valuesize: number of values
Input:
arr = np.random.uniform(1, 10, 5)
arrOutput:
array([2.9819103 , 3.0879235 , 3.69738972, 3.89013076, 5.73595479])random.choice()
Chooses random values from given choices.
Input:
arr = np.random.choice(['rock', 'paper', 'scissor'])
arrOutput:
np.str_('rock')Input:
arr = np.random.choice(['rock', 'paper', 'scissor'], 2)
arrOutput:
array(['rock', 'scissor'], dtype='<U7')Example: Create a dictionary using random values
Input:
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
Create a list of Maths scores.
Convert the list into an array.
Give the Total Score, Maximum Score, Minimum and Mean (Average) Score.
Create a NumPy array of a table of 5 (e.g., multiples of 5 or a 5x5 table — interpret as needed).
Generate a reverse array from 10 to 1.
Generate an array of 20 random integers.
Generate an array of 20 random floats.
Create a dictionary of 5 people with random height, weight, age, hair color.
(You can implement these using the NumPy functions shown above.)