- Lab
- Data

Getting Started with NumPy Hands-on Practice
In this lab, you'll gain hands-on experience with NumPy, a cornerstone library for numerical computing in Python. You'll create and manipulate NumPy arrays, compare their performance with Python lists, and explore essential attributes of the ndarray object. By creating arrays using various techniques and understanding their fundamental properties, you'll lay a solid foundation for advanced data analysis and scientific computing with NumPy.

Path Info
Table of Contents
-
Challenge
Exploring NumPy and its Features
Jupyter Guide
To get started, open the file on the right entitled "Step 1...". You'll complete each task for Step 1 in that Jupyter Notebook file. Remember, you must run the cells
(ctrl/cmd + Enter)
for each task before moving onto the next task in the Jupyter Notebook. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.
Exploring NumPy and its Features
To review the concepts covered in this step, please refer to the Introduction to NumPy module of the Getting Started with NumPy course.
Understanding the features and benefits of NumPy is important because it helps to understand why and when to use NumPy in data analysis.
In this step, you will create a very simple NumPy array with integers and perform some basic operations on it. You will also compare the performance of NumPy arrays with Python lists to understand the efficiency of NumPy. You'll run some provided code to time the execution of numpy array operations vs performing those operations on lists.
Task 1.1: Creating a NumPy Array
Use the provided code to import numpy into the evironment and then create a simple NumPy array with integers from 1 to 10 using the
np.array
function. Print the array you create.π Hint
Use the
np.array
function and pass a list of integers from 1 to 10 as an argument.π Solution
import numpy as np # Create a numpy array with integers from 1 to 10 array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(array)
Task 1.2: Performing Basic Operations
Perform addition, subtraction, multiplication, and division operations on the array you created. Print the results.
π Hint
Use the
+
,-
,*
, and/
operators to perform addition, subtraction, multiplication, and division respectively on the array.π Solution
# Perform addition, subtraction, multiplication, and division on the array print("addition", array + 10) print("subtraction", array - 5) print("multiplication", array * 3) print("division", array / 2)
Task 1.3: Comparing Performance with Python Lists
Use the provided code to compare the performance of NumPy arrays with Python lists by timing the execution of operations on both. The provided code adds 10 to every number from 1 to 1,000,000 using a list and using a numpy array. Run the code a few times and see which method is faster.
π Hint
We use the
time.time
function to get the current time before and after the operation, then we subtract the start time from the end time to get the execution time. The code does this for both the list and the array.π Solution
import time # Create a Python list with integers from 1 to 1 000 000 oneMillion = [num for num in range(1, int(1e6)+1)] # Time the execution of addition operation on the list start_time = time.time() list = [i + 10 for i in oneMillion] end_time = time.time() # Print the execution time print('Execution time for list (seconds): ', end_time - start_time) # Create the array array = np.array(oneMillion) # Time the execution of addition operation on the array start_time = time.time() array = array + 10 end_time = time.time() # Print the execution time print('Execution time for array (seconds): ', end_time - start_time)
-
Challenge
Understanding the N-Dimensional Array Object
Understanding the N-Dimensional Array Object
To review the concepts covered in this step, please refer to the Understanding the Ndarray Object module of the Getting Started with NumPy course.
Understanding the n-dimensional array (ndarray) object is important because it is the fundamental object underlying all of NumPy. This step will cover the creation of simple ndarray objects and their attributes.
In this step, you will create an instance of ndarray using the
np.array
function. You will also explore the attributes of the ndarray object, such as shape, size, ndim, and dtype. Use thenp.array
function to create your ndarray and the 'dot' notation to access its attributes.
Task 2.1: Creating a Simple ndarray
Import numpy with the alias np, and then create a simple ndarray object using the numpy.array function. The array should contain integers and be in any shape you want. Display the array.
π Hint
Use the
np.array
function and pass in a list of integers from 1 to 5. For example,np.array([1, 2, 3, 4, 5])
. Or, create a list of lists and make the array 3x3. For example,np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]
π Solution
# Import numpy import numpy as np # Create an ndarray using np.array array = np.array([1, 2, 3, 4, 5]) # Print the array print(array)
Task 2.2: Exploring ndarray Attributes: Shape
Print the shape of the ndarray you created in the previous task.
π Hint
Use the
shape
attribute of the ndarray object to get its shape. For example,array.shape
.π Solution
# Print the shape of the array print(array.shape)
Task 2.3: Exploring ndarray Attributes: Size
Print the size of the ndarray you created in the first task.
π Hint
Use the
size
attribute of the ndarray object to get its size. For example,array.size
.π Solution
# Print the size of the array print(array.size)
Task 2.4: Exploring ndarray Attributes: ndim
Print the number of dimensions of the ndarray you created in the first task.
π Hint
Use the
ndim
attribute of the ndarray object to get its number of dimensions. For example,array.ndim
.π Solution
# Print the number of dimensions of the array print(array.ndim)
Task 2.5: Exploring ndarray Attributes: dtype
Print the data type of the elements in the ndarray you created in the first task.
π Hint
Use the
dtype
attribute of the ndarray object to get its data type. For example,array.dtype
.π Solution
# Print the data type of the elements in the array print(array.dtype)
-
Challenge
Creating Ndarrays
Creating Ndarrays
To review the concepts covered in this step, please refer to the Creating Ndarrays module of the Getting Started with NumPy course.
Creating ndarrays is important because it is the first step in working with NumPy. This step will cover different ways of creating ndarrays, such as from existing data or within a numerical range.
In this step, you will create ndarrays using different methods. You will use the
np.empty
,np.zeros
,np.ones
, andnp.full
routines to create ndarrays. You will also create an ndarray from existing data using thenp.asarray
routine and an ndarray within a numerical range using thenp.arange
andnp.linspace
routines. At each stage, we'll print the results.
Task 3.1: Creating an Empty Ndarray
Import numpy with the alias
np
, and create an empty ndarray of shape (3, 3) using thenp.empty
routine. Display the resulting array.π Hint
Use the
np.empty()
function with the shape of the ndarray you want to create. The shape should be a tuple, like this:(3, 3)
.π Solution
# Import numpy import numpy as np # Create an empty ndarray empty_array = np.empty((3, 3)) empty_array
Task 3.2: Creating a Zeros Ndarray
Create an ndarray of zeros with shape (3, 3) using the
np.zeros
routine. Display the resulting array.π Hint
Use the
np.zeros()
function with the shape of the ndarray you want to create. The shape should be a tuple, like this:(3, 3)
.π Solution
# Create a zeros ndarray zeros_array = np.zeros((3, 3)) zeros_array
Task 3.3: Creating a Ones Ndarray
Create an ndarray of ones with shape (3, 3) using the
np.ones
routine. Display the resulting array.π Hint
Use the
np.ones()
function with the shape of the ndarray you want to create. The shape should be a tuple, like this:(3, 3)
.π Solution
# Create a ones ndarray ones_array = np.ones((3, 3)) ones_array
Task 3.4: Creating a Full Ndarray
Create an ndarray filled with the number 7 and of shape (3, 3) using the
np.full
routine. Display the resulting array.π Hint
Use
np.full()
with the shape of the ndarray you want to create as the first argument, and the number you want to fill the ndarray with as the second argument. The shape should be a tuple, like this:(3, 3)
and the number should be an integer, like this:7
.π Solution
# Create a full ndarray full_array = np.full((3, 3), 7) full_array
Task 3.5: Creating an Ndarray from Existing Data
Create an ndarray from the list
[1, 2, 3, 4, 5]
using thenp.asarray
routine. Display the resulting array.π Hint
Use
np.asarray()
with the list you want to convert into an ndarray. The list should be like this:[1, 2, 3, 4, 5]
.π Solution
# Create an ndarray from existing data list_array = np.asarray([1, 2, 3, 4, 5]) list_array
Task 3.6: Creating an Ndarray within a Numerical Range
Create an ndarray of numbers from 0 to 10 with a step of 2 using the
np.arange
routine. Display the resulting array.π Hint
Use
np.arange(start, stop, step)
with the start, stop, and step values for the range you want to create. The start should be0
, the stop should be10
, and the step should be2
.π Solution
# Create an ndarray within a numerical range range_array = np.arange(0, 10, 2) range_array
Task 3.7: Creating an Ndarray of Evenly Spaced Values
Create an ndarray of 5 evenly spaced numbers between 0 and 1 using the
np.linspace
routine. Display the resulting array.π Hint
Use
np.linspace(start, stop, n_samples)
with the start, stop, and number of samples values for the range you want to create. The start should be0
, the stop should be1
, and the number of samples should be5
.π Solution
# Create an ndarray of evenly spaced values linspace_array = np.linspace(0, 1, 5) linspace_array
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the authorβs guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.