0
0
NumPydata~10 mins

np.savetxt() and np.loadtxt() for text in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.savetxt() and np.loadtxt() for text
Create or have a numpy array
Use np.savetxt() to write array to text file
File saved on disk as plain text
Use np.loadtxt() to read text file back into numpy array
Array loaded in memory, same shape and values
This flow shows saving a numpy array to a text file and then loading it back as an array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
np.savetxt('data.txt', arr, fmt='%d')
loaded = np.loadtxt('data.txt', dtype=int)
Save a 2x2 integer array to 'data.txt' and load it back into 'loaded'.
Execution Table
StepActionInput/ConditionResult/Output
1Create arrayarr = [[1, 2], [3, 4]]arr is a 2x2 numpy array with integers
2Call np.savetxtfilename='data.txt', arr, fmt='%d'File 'data.txt' created with text: 1 2 3 4
3Call np.loadtxtfilename='data.txt', dtype=intloaded is a 2x2 numpy array with values [[1, 2], [3, 4]]
4Check equalitynp.array_equal(arr, loaded)True
💡 Finished saving and loading array, data matches original
Variable Tracker
VariableStartAfter Step 1After Step 3Final
arrundefined[[1 2] [3 4]][[1 2] [3 4]][[1 2] [3 4]]
loadedundefinedundefined[[1 2] [3 4]][[1 2] [3 4]]
Key Moments - 3 Insights
Why do we need to specify fmt='%d' in np.savetxt?
Without fmt='%d', np.savetxt saves floats by default, which can add decimals. Specifying '%d' saves integers exactly as in the execution_table step 2.
What happens if the file 'data.txt' does not exist when calling np.loadtxt?
np.loadtxt will raise a FileNotFoundError because it cannot find the file to read, as shown in execution_table step 3 where the file must exist.
Does np.loadtxt always return the same shape array as saved?
Yes, if the file format matches and data is consistent, np.loadtxt returns the same shape, as seen in execution_table step 3 and variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of 'data.txt' after step 2?
A"1 2\n3 4"
B"[1 2]\n[3 4]"
C"1,2\n3,4"
D"1.0 2.0\n3.0 4.0"
💡 Hint
Check the Result/Output column in execution_table row 2 for the exact file content.
At which step does the variable 'loaded' get its value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Action column in execution_table where np.loadtxt is called.
If we remove fmt='%d' from np.savetxt, how would the file content change?
AIt would save the array as binary
BIt would save integers as floats with decimals
CIt would not save anything
DIt would save the array with commas
💡 Hint
Refer to key_moments question about fmt='%d' and execution_table step 2.
Concept Snapshot
np.savetxt(filename, array, fmt) saves a numpy array to a text file.
np.loadtxt(filename, dtype) loads the text file back as a numpy array.
Use fmt='%d' to save integers without decimals.
The saved file is plain text with rows and columns.
Loaded array matches the original shape and values.
Full Transcript
This lesson shows how to save a numpy array to a text file using np.savetxt and then load it back using np.loadtxt. First, we create a 2x2 integer array. Then, np.savetxt writes this array to a file named 'data.txt' with integer formatting to avoid decimals. Next, np.loadtxt reads the file and recreates the array in memory. The loaded array matches the original exactly in shape and values. Key points include specifying the format to control how data is saved and ensuring the file exists before loading. This process is useful for saving data in a human-readable text format and loading it later for analysis.