Approx. read time: 2.8 min.
Post: Learn about Numpy Arrays in Python programming
Lesson: Introduction to Numpy Arrays in Python
Getting Started with Numpy Arrays
Numpy arrays are a powerful alternative to Python lists. They are particularly useful for handling large amounts of data due to their speed and efficiency. Numpy arrays allow you to perform mathematical operations across entire arrays, which is faster and more intuitive than using Python lists.
Some key advantages of Numpy arrays:
- Fast: Numpy arrays are more efficient than Python lists.
- Easy to work with: You can easily create and manipulate arrays.
- Mathematical operations: You can perform calculations across entire arrays in one go.
In this lesson, we will explore the basics of Numpy arrays with examples.
Example 1: Creating a Numpy Array
You can convert Python lists into Numpy arrays using the numpy.array()
function. Let’s create two arrays from height and weight data.
Explanation:
- We first create two lists:
height
andweight
. - We use
np.array()
to convert these lists into Numpy arrays:np_height
andnp_weight
. - The
type()
function is used to confirm thatnp_height
is indeed a Numpy array.
Output:
Example 2: Element-wise Calculations
One of the major benefits of Numpy arrays is the ability to perform element-wise operations. Let’s calculate the Body Mass Index (BMI) for each observation using the formula:
Explanation:
- We compute BMI by dividing the weight by the square of the height for each observation.
- This is an element-wise operation performed on the entire array.
Output (example BMI values):
Example 3: Subsetting Numpy Arrays
You can easily subset Numpy arrays to extract specific values. Let’s find the BMI values that are greater than 23.
Explanation:
- We use a boolean condition
bmi > 23
to create a mask (array ofTrue
orFalse
values). - We use this mask to filter and extract only the BMI values that satisfy the condition.
Output (example):
Exercise: Convert Weights from Kilograms to Pounds
Task: Convert the list of weights from kilograms to pounds. Use the scalar conversion factor of 1 kg=2.2 lbs.
Steps:
- Convert the list of weights in kilograms to a Numpy array.
- Convert each weight from kilograms to pounds using element-wise multiplication.
- Print the resulting array of weights in pounds.
Expected Output:
Final Assignment: Numpy Arrays Practice
Problem: You are given two lists: one containing the heights of individuals in meters and the other containing their weights in kilograms. You need to:
- Convert both lists into Numpy arrays.
- Calculate the BMI for each individual.
- Find and print the BMI values greater than 25.
- Convert the weights from kilograms to pounds and print the result.
Here is the data:
- Heights:
[1.72, 1.85, 1.65, 1.90, 1.75]
(in meters) - Weights:
[65.0, 80.5, 72.3, 90.0, 85.6]
(in kilograms)
Final Assignment Solution (Answer Key)
Expected Output:
Conclusion
In this lesson, we learned how to:
- Create Numpy arrays from Python lists.
- Perform element-wise operations on Numpy arrays.
- Subset arrays based on conditions.
- Use Numpy to quickly convert units of measurement.
Numpy arrays are an excellent tool for handling large datasets efficiently, and with these basic skills, you can now start exploring more advanced features of Numpy.
Assignment Idea: a. Try working with larger datasets and practice subsetting and element-wise operations. b. Explore other Numpy functions like np.mean()
, np.median()
, and np.std()
for basic statistical calculations.
Related Videos:
Related Posts:
Learn about JavaScript FUNCTIONS