0
0
NumPydata~15 mins

Monitoring memory usage in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Monitoring memory usage
📖 Scenario: You are working on a data science project that uses numpy arrays. You want to monitor how much memory your arrays use to optimize your program's performance.
🎯 Goal: Build a simple Python script that creates a numpy array, sets a threshold for memory usage, checks the array's memory usage, and prints a message if the usage exceeds the threshold.
📋 What You'll Learn
Create a numpy array with specific values
Define a memory usage threshold variable
Calculate the memory usage of the array in bytes
Compare the memory usage with the threshold
Print a message showing the memory usage and if it exceeds the threshold
💡 Why This Matters
🌍 Real World
Monitoring memory usage helps keep programs efficient and prevents crashes due to running out of memory.
💼 Career
DevOps engineers and data scientists often monitor memory to optimize applications and infrastructure.
Progress0 / 4 steps
1
Create a numpy array
Import numpy as np and create a numpy array called data with these exact values: [10, 20, 30, 40, 50].
NumPy
Need a hint?

Use np.array() to create the array with the given list of numbers.

2
Set memory usage threshold
Create a variable called memory_threshold and set it to 100 to represent 100 bytes.
NumPy
Need a hint?

Just assign the number 100 to the variable memory_threshold.

3
Calculate memory usage of the array
Create a variable called memory_used and set it to the number of bytes used by the data array using the nbytes attribute.
NumPy
Need a hint?

Use data.nbytes to get the memory size in bytes.

4
Print memory usage and check threshold
Write a print statement that shows the text "Memory used:" followed by the value of memory_used. Then write an if statement that prints "Memory usage exceeds threshold!" if memory_used is greater than memory_threshold.
NumPy
Need a hint?

Use print("Memory used:", memory_used) and an if statement to compare memory_used and memory_threshold.