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.savetxt() and np.loadtxt()
📖 Scenario: Imagine you are working with temperature data collected from sensors every hour. You want to save this data to a text file so you can share it or use it later. Then, you want to load the data back into your program to analyze it.
🎯 Goal: You will create a small array of temperature data, save it to a text file using np.savetxt(), then load it back using np.loadtxt(). Finally, you will print the loaded data to check it matches the original.
📋 What You'll Learn
Create a numpy array called temperatures with the exact values: 22.5, 23.0, 21.8, 22.1, 23.3
Create a string variable called filename with the value 'temps.txt'
Use np.savetxt() to save temperatures to the file filename
Use np.loadtxt() to load the data from filename into a variable called loaded_temps
Print the variable loaded_temps to display the loaded data
💡 Why This Matters
🌍 Real World
Saving sensor or experiment data to text files is common in data science to share or archive data in a simple format.
💼 Career
Data scientists often need to save processed data and reload it later for analysis or reporting. Knowing how to use <code>np.savetxt()</code> and <code>np.loadtxt()</code> is a basic but important skill.
Progress0 / 4 steps
1
Create the temperature data array
Create a numpy array called temperatures with these exact values: 22.5, 23.0, 21.8, 22.1, 23.3
NumPy
Hint
Use np.array() and put the values inside a list.
2
Create the filename variable
Create a string variable called filename and set it to 'temps.txt'
NumPy
Hint
Just assign the string 'temps.txt' to the variable filename.
3
Save the array to a text file
Use np.savetxt() to save the temperatures array to the file named filename
NumPy
Hint
Call np.savetxt() with the filename and the array as arguments.
4
Load the data and print it
Use np.loadtxt() to load the data from the file filename into a variable called loaded_temps. Then print loaded_temps to display the loaded data.
NumPy
Hint
Use loaded_temps = np.loadtxt(filename) to load, then print(loaded_temps).
Practice
(1/5)
1. What is the main purpose of np.savetxt() in NumPy?
easy
A. To convert a NumPy array into a Python list
B. To load a NumPy array from a binary file
C. To save a NumPy array to a text file in a readable format
D. To display a NumPy array on the screen
Solution
Step 1: Understand the function purpose
np.savetxt() is designed to save arrays to text files, making the data readable and shareable.
Step 2: Compare with other options
Options B, C, and D describe different functions or actions unrelated to saving arrays as text files.
Final Answer:
To save a NumPy array to a text file in a readable format -> Option C
Quick Check:
np.savetxt() saves arrays to text files [OK]
Hint: Remember: savetxt saves arrays as readable text files [OK]
Common Mistakes:
Confusing savetxt with loadtxt
Thinking it saves to binary files
Assuming it converts arrays to lists
2. Which of the following is the correct syntax to save a 2D NumPy array arr to a file named data.txt using np.savetxt()?
easy
A. np.savetxt('data.txt', arr)
B. np.savetxt(arr, 'data.txt')
C. np.save('data.txt', arr)
D. np.loadtxt('data.txt', arr)
Solution
Step 1: Recall the correct parameter order for np.savetxt()
The first argument is the filename (string), the second is the array to save.
Step 2: Check each option
np.savetxt('data.txt', arr) matches the correct order. np.savetxt(arr, 'data.txt') reverses the order. np.save('data.txt', arr) uses np.save which saves binary files. np.loadtxt('data.txt', arr) uses np.loadtxt which reads files, not saves.
Final Answer:
np.savetxt('data.txt', arr) -> Option A
Quick Check:
Filename first, array second in np.savetxt() [OK]
Hint: Filename goes first, array second in np.savetxt() [OK]
A. Using '%d' format truncates floats to integers when saving
B. np.loadtxt cannot read float data
C. Missing delimiter argument causes error
D. Array must be 2D to use np.savetxt
Solution
Step 1: Check format string in np.savetxt()
The format '%d' saves numbers as integers, so 1.5, 2.5, 3.5 become 1, 2, 3 in the file.
Step 2: Loading with dtype=float
Loading back as float converts these integers to floats 1.0, 2.0, 3.0, losing original decimal parts.
Step 3: Identify the error
The error is the format string truncates data, causing loss of precision.
Final Answer:
Using '%d' format truncates floats to integers when saving -> Option A
Quick Check:
Format string controls saved data type [OK]
Hint: Use '%f' to save floats, not '%d' [OK]
Common Mistakes:
Assuming loadtxt can't read floats
Thinking delimiter is required for 1D arrays
Believing np.savetxt requires 2D arrays only
5. You have a 2D NumPy array with mixed integer and float values. You want to save it to a text file with comma separation and load it back preserving the exact values. Which code snippet correctly achieves this?
hard
A. np.savetxt('data.csv', arr, delimiter=',', fmt='%d')
loaded = np.loadtxt('data.csv', delimiter=',', dtype=float)
B. np.savetxt('data.csv', arr, delimiter=',', fmt='%.2f')
loaded = np.loadtxt('data.csv', delimiter=',')
C. np.savetxt('data.csv', arr, delimiter=';')
loaded = np.loadtxt('data.csv', delimiter=',')
D. np.savetxt('data.csv', arr)
loaded = np.loadtxt('data.csv', delimiter=',')
Solution
Step 1: Choose correct format for mixed data
Using fmt='%.2f' saves all numbers as floats with 2 decimals, preserving float and integer values.
Step 2: Match delimiter in save and load
Both saving and loading use delimiter=',' ensuring data is correctly split on commas.
Step 3: Check other options for errors
np.savetxt('data.csv', arr, delimiter=',', fmt='%d')
loaded = np.loadtxt('data.csv', delimiter=',', dtype=float) uses '%d' which truncates floats. np.savetxt('data.csv', arr, delimiter=';')
loaded = np.loadtxt('data.csv', delimiter=',') mismatches delimiters. np.savetxt('data.csv', arr)
loaded = np.loadtxt('data.csv', delimiter=',') saves without delimiter but loads with comma, causing errors.