0
0
NumPydata~15 mins

np.savetxt() and np.loadtxt() for text in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use loaded_temps = np.loadtxt(filename) to load, then print(loaded_temps).