Bird
Raised Fist0
NumPydata~10 mins

np.save() and np.load() for binary in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - np.save() and np.load() for binary
Create numpy array
↓
Call np.save(filename, array)
↓
Array saved as binary file
↓
Call np.load(filename)
↓
Binary file read into numpy array
↓
Use loaded array in program
First, you save a numpy array to a binary file using np.save(). Later, you load it back with np.load() to get the same array.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
np.save('data.npy', arr)
loaded_arr = np.load('data.npy')
print(loaded_arr)
This code saves a numpy array to a file and then loads it back, printing the loaded array.
Execution Table
StepActionInput/ConditionResult/Output
1Create arrayarr = [1, 2, 3]arr is numpy array [1 2 3]
2Save arraynp.save('data.npy', arr)File 'data.npy' created with binary data
3Load arrayloaded_arr = np.load('data.npy')loaded_arr is numpy array [1 2 3]
4Print loaded arrayprint(loaded_arr)Output: [1 2 3]
5EndNo more codeExecution stops
💡 All steps completed, array saved and loaded successfully
Variable Tracker
VariableStartAfter Step 1After Step 3Final
arrundefined[1 2 3][1 2 3][1 2 3]
loaded_arrundefinedundefined[1 2 3][1 2 3]
Key Moments - 3 Insights
Why does np.save() create a file instead of returning data?
np.save() writes the array directly to a binary file on disk, so it does not return data but creates a file you can load later (see step 2 in execution_table).
What type is the object returned by np.load()?
np.load() returns a numpy array loaded from the binary file, exactly like the original array (see step 3 and 4 in execution_table).
Can you use np.load() without saving first?
No, np.load() needs a file created by np.save() or similar. If the file doesn't exist, it will cause an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'loaded_arr' after step 3?
Aundefined
B[1 2 3]
C[3 2 1]
DNone
💡 Hint
Check the 'Result/Output' column for step 3 in execution_table.
At which step is the binary file 'data.npy' created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the action 'Save array' in execution_table.
If you change the array before saving to [4,5,6], what will be printed at step 4?
A[4 5 6]
BError
C[1 2 3]
DEmpty array
💡 Hint
The saved file stores the array at save time, so loading will return the saved array (see variable_tracker).
Concept Snapshot
np.save(filename, array) saves a numpy array to a binary file.
np.load(filename) loads the array back from the file.
The saved file has .npy extension by convention.
Use these to store and retrieve arrays efficiently.
np.save does not return data, it writes to disk.
np.load returns the original numpy array.
Full Transcript
This visual execution shows how np.save() writes a numpy array to a binary file and np.load() reads it back. First, an array is created. Then np.save() writes it to 'data.npy'. Next, np.load() reads the file and returns the array. Finally, printing shows the loaded array matches the original. Variables 'arr' and 'loaded_arr' track the array before and after saving/loading. Key points include understanding that np.save creates a file and np.load returns a numpy array. The quiz checks understanding of variable values and file creation steps.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Saves a NumPy array to a binary file on disk -> Option A
  4. 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

  1. Step 1: Recall np.save() parameter order

    The first argument is the filename (string), the second is the array to save.
  2. 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.
  3. Final Answer:

    np.save('data.npy', arr) -> Option D
  4. Quick Check:

    Filename first, array second in np.save() [OK]
Hint: np.save(filename, array) always filename first [OK]
Common Mistakes:
  • Swapping filename and array arguments
  • Using np.load() instead of np.save() to save
  • Using wrong function name like np.savefile()
3. What will be the output of the following code?
import numpy as np
arr = np.array([1, 2, 3])
np.save('file.npy', arr)
loaded_arr = np.load('file.npy')
print(loaded_arr)
medium
A. [1 2 3]
B. ['1' '2' '3']
C. Error: file not found
D. [[1 2 3]]

Solution

  1. Step 1: Save and load the array

    The array [1, 2, 3] is saved to 'file.npy' and then loaded back exactly as it was.
  2. Step 2: Understand print output of loaded array

    Printing the loaded array shows the original array as [1 2 3] without quotes or extra brackets.
  3. Final Answer:

    [1 2 3] -> Option A
  4. Quick Check:

    np.load(np.save()) returns original array [OK]
Hint: np.load(np.save()) returns original array unchanged [OK]
Common Mistakes:
  • Expecting string elements instead of integers
  • Thinking np.load() returns nested arrays
  • Assuming file not found error without saving first
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([4, 5, 6])
np.save('mydata.npy')
loaded = np.load('mydata.npy')
print(loaded)
medium
A. np.save() cannot save integer arrays
B. np.load() should be called before np.save()
C. np.save() is missing the array argument to save
D. The filename should have .txt extension

Solution

  1. Step 1: Check np.save() usage

    The np.save() function requires two arguments: filename and array. Here, the array argument is missing.
  2. 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.
  3. Final Answer:

    np.save() is missing the array argument to save -> Option C
  4. 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

  1. Step 1: Load arrays separately

    Since arrays are saved in separate files, load each using np.load() individually.
  2. 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.
  3. 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.
  4. Final Answer:

    Load each with np.load() and use np.vstack([arr1, arr2]) -> Option B
  5. 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