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.3Create a string variable called
filename with the value 'temps.txt'Use
np.savetxt() to save temperatures to the file filenameUse
np.loadtxt() to load the data from filename into a variable called loaded_tempsPrint 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