Numpy Arrays
Getting started
Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with, and give users the opportunity to perform calculations across entire arrays.
In the following example, you will first create two Python lists. Then, you will import the numpy package and create numpy arrays out of the newly created lists.
# Create 2 new lists height and weight
height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
# Import the numpy package as np
import numpy as np
# Create 2 numpy arrays from height and weight
np_height = np.array(height)
np_weight = np.array(weight)
Print out the type of np_height
print(type(np_height))
Element-wise calculations
# Calculate bmi
bmi = np_weight / np_height ** 2
# Print the result
print(bmi)
Subsetting
# For a boolean response
bmi > 23
# Print only those observations above 23
bmi[bmi > 23]
Output :
<class 'numpy.ndarray'>
[23.34925219 27.88755755 28.75558507 25.48723993 23.87257618 25.84368152]