0
0
NumPydata~10 mins

np.savetxt() and np.loadtxt() for text in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save the array 'data' to a text file named 'output.txt'.

NumPy
import numpy as np

data = np.array([1, 2, 3, 4, 5])
np.[1]('output.txt', data)
Drag options to blanks, or click blank then click option'
Aloadtxt
Bsave
Cload
Dsavetxt
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.loadtxt() instead of np.savetxt() to save data.
Using np.save() which saves in binary format, not text.
2fill in blank
medium

Complete the code to load data from the text file 'output.txt' into a numpy array.

NumPy
import numpy as np

loaded_data = np.[1]('output.txt')
print(loaded_data)
Drag options to blanks, or click blank then click option'
Aloadtxt
Bsavetxt
Csave
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.savetxt() instead of np.loadtxt() to read data.
Using np.load() which reads binary files, not text.
3fill in blank
hard

Fix the error in the code to save a 2D array 'arr' to 'matrix.txt' with comma separators.

NumPy
import numpy as np

arr = np.array([[1, 2], [3, 4]])
np.savetxt('matrix.txt', arr, delimiter=[1])
Drag options to blanks, or click blank then click option'
A' '
B','
C';'
D':'
Attempts:
3 left
💡 Hint
Common Mistakes
Using space ' ' as delimiter when commas are needed.
Using semicolon or colon which changes the file format.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

NumPy
words = ['apple', 'bat', 'car', 'door']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<' instead of '>' which filters shorter words.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 3.

NumPy
words = ['apple', 'bat', 'car', 'door']
lengths = { [1]: [2] for word in words if len(word) [3] 3}
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Using '<' instead of '>' in the condition.
Using the word itself instead of its length as the value.