Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Saving and Loading Data with np.save() and np.load()
📖 Scenario: You are working on a data science project where you need to save your numerical data to a file and load it later for analysis. This helps you avoid recalculating or reloading data from scratch every time.
🎯 Goal: You will create a numpy array, save it to a binary file using np.save(), then load it back using np.load() and print the loaded data.
📋 What You'll Learn
Create a numpy array with specific values
Save the numpy array to a binary file using np.save()
Load the saved numpy array from the file using np.load()
Print the loaded numpy array to verify it matches the original
💡 Why This Matters
🌍 Real World
Saving and loading numpy arrays is useful when working with large datasets or intermediate results in data science projects. It helps save time by storing data in a fast, binary format.
💼 Career
Data scientists and analysts often save processed data to files for reuse, sharing, or backup. Knowing how to use <code>np.save()</code> and <code>np.load()</code> is a basic but important skill in data handling.
Progress0 / 4 steps
1
Create a numpy array
Import numpy as np and create a numpy array called data with the values [10, 20, 30, 40, 50].
NumPy
Hint
Use np.array() to create the array with the exact values given.
2
Save the numpy array to a binary file
Use np.save() to save the numpy array data to a file named 'datafile.npy'.
NumPy
Hint
Call np.save() with the filename and the array variable.
3
Load the numpy array from the binary file
Use np.load() to load the numpy array from the file 'datafile.npy' into a variable called loaded_data.
NumPy
Hint
Use np.load() with the filename and assign it to loaded_data.
4
Print the loaded numpy array
Print the variable loaded_data to display the loaded numpy array.
NumPy
Hint
Use print(loaded_data) to show the array.
Practice
(1/5)
1. What does the np.save() function do in NumPy?
easy
A. Saves a NumPy array to a binary file on disk
B. Loads a NumPy array from a binary file
C. Converts a NumPy array to a list
D. Prints the contents of a NumPy array
Solution
Step 1: Understand the purpose of np.save()
The np.save() function is designed to save a NumPy array to a file in binary format, preserving its data type and shape.
Step 2: Differentiate from np.load()
np.load() is used to load arrays from files, not save them. Other options do not relate to saving files.
Final Answer:
Saves a NumPy array to a binary file on disk -> Option A
Quick Check:
np.save() saves array [OK]
Hint: np.save() writes array to file, np.load() reads it back [OK]
Common Mistakes:
Confusing np.save() with np.load()
Thinking np.save() converts array to list
Assuming np.save() prints array
2. Which of the following is the correct syntax to save a NumPy array arr to a file named data.npy?
easy
A. np.savefile('data.npy', arr)
B. np.save(arr, 'data.npy')
C. np.load('data.npy', arr)
D. np.save('data.npy', arr)
Solution
Step 1: Recall np.save() parameter order
The first argument is the filename (string), the second is the array to save.
Step 2: Check other options for correctness
np.save(arr, 'data.npy') reverses parameters, np.load('data.npy', arr) uses np.load() which loads, not saves, np.savefile('data.npy', arr) uses a non-existent function.
Final Answer:
np.save('data.npy', arr) -> Option D
Quick Check:
Filename first, array second in np.save() [OK]
Hint: np.save(filename, array) always filename first [OK]
C. np.save() is missing the array argument to save
D. The filename should have .txt extension
Solution
Step 1: Check np.save() usage
The np.save() function requires two arguments: filename and array. Here, the array argument is missing.
Step 2: Verify other options
np.load() should be called before np.save() is incorrect because loading happens after saving. The filename should have .txt extension is wrong because .npy is the correct extension. np.save() cannot save integer arrays is false; np.save() can save integer arrays.
Final Answer:
np.save() is missing the array argument to save -> Option C
Quick Check:
np.save() needs filename and array [OK]
Hint: np.save() always needs array argument after filename [OK]
Common Mistakes:
Forgetting to pass the array to np.save()
Thinking .txt is needed instead of .npy
Confusing order of np.save() and np.load()
5. You have saved multiple arrays separately using np.save() as arr1.npy and arr2.npy. How can you load both arrays and combine them into a single 2D array where each original array is a row?
hard
A. Use np.load('arr1.npy', 'arr2.npy') directly
B. Load each with np.load() and use np.vstack([arr1, arr2])
C. Save both arrays in one file using np.save() and then load
D. Load arrays and use np.concatenate(arr1, arr2, axis=1)
Solution
Step 1: Load arrays separately
Since arrays are saved in separate files, load each using np.load() individually.
Step 2: Combine arrays as rows
Use np.vstack([arr1, arr2]) to stack arrays vertically, making each array a row in the new 2D array.
Step 3: Check other options
Use np.load('arr1.npy', 'arr2.npy') directly is invalid syntax, Save both arrays in one file using np.save() and then load is incorrect because np.save() saves one array per file, Load arrays and use np.concatenate(arr1, arr2, axis=1) concatenates along columns which may not work if shapes differ.
Final Answer:
Load each with np.load() and use np.vstack([arr1, arr2]) -> Option B
Quick Check:
Load separately, stack with vstack [OK]
Hint: Load arrays separately, stack rows with np.vstack() [OK]
Common Mistakes:
Trying to load multiple files in one np.load() call
Using np.concatenate with wrong axis
Assuming np.save() can save multiple arrays in one file