0
0
NumPydata~5 mins

np.savetxt() and np.loadtxt() for text in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.savetxt() do in NumPy?

np.savetxt() saves a NumPy array to a text file. It writes the array data in a readable text format, like CSV or space-separated values.

Click to reveal answer
beginner
How do you load data from a text file into a NumPy array?

You use np.loadtxt(). It reads numbers from a text file and returns them as a NumPy array.

Click to reveal answer
intermediate
What parameter in np.savetxt() controls the format of saved numbers?

The fmt parameter controls how numbers are saved, for example, fmt='%.2f' saves numbers with two decimal places.

Click to reveal answer
intermediate
Can np.loadtxt() read files with comments or headers?

Yes, you can skip lines with skiprows and ignore comments with the comments parameter.

Click to reveal answer
beginner
What happens if you save a 2D array with np.savetxt() and then load it back with np.loadtxt()?

You get the same 2D array back, as np.savetxt() writes rows line by line and np.loadtxt() reads them back into a 2D array.

Click to reveal answer
Which function saves a NumPy array to a text file?
Anp.savetxt()
Bnp.loadtxt()
Cnp.save()
Dnp.load()
How do you skip the first 3 lines when loading a text file with np.loadtxt()?
Askiprows=3
Bskip=3
Crows=3
Dignore=3
What does the fmt parameter in np.savetxt() control?
ADelimiter character
BData type
CFile name
DNumber format in the file
If a text file has lines starting with '#', how can np.loadtxt() ignore them?
Aignore='#'
Bskiprows='#'
Ccomments='#'
Dheader='#'
What type of data does np.loadtxt() return?
AList
BNumPy array
CDictionary
DString
Explain how to save a 2D NumPy array to a text file and then load it back.
Think about writing and reading files with NumPy functions.
You got /5 concepts.
    Describe how to handle text files with headers or comments when loading data using NumPy.
    Consider parameters that help ignore unwanted lines.
    You got /3 concepts.