Challenge - 5 Problems
np.genfromtxt() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.genfromtxt() with missing values
What is the output of the following code snippet that uses
np.genfromtxt() to read data with missing values?NumPy
import numpy as np from io import StringIO data = """ 1,2,3 4,,6 7,8, """ arr = np.genfromtxt(StringIO(data), delimiter=',', filling_values=-1) print(arr)
Attempts:
2 left
💡 Hint
Look at how
filling_values replaces missing entries.✗ Incorrect
The
filling_values=-1 argument replaces missing data with -1. So missing entries become -1 instead of NaN or errors.🧠 Conceptual
intermediate1:30remaining
Understanding the role of filling_values in np.genfromtxt()
What does the
filling_values parameter do in np.genfromtxt() when reading a file with missing data?Attempts:
2 left
💡 Hint
Think about how missing data can be handled automatically.
✗ Incorrect
The
filling_values parameter tells np.genfromtxt() what value to use instead of missing entries, so the array remains numeric and complete.❓ data_output
advanced2:30remaining
Resulting array shape and content with missing data
Given this CSV data with missing values, what is the shape and content of the numpy array after loading with
np.genfromtxt() using delimiter=',' and default parameters?NumPy
import numpy as np from io import StringIO data = """ 10,20,30 40,,60 ,80,90 """ arr = np.genfromtxt(StringIO(data), delimiter=',') print(arr) print(arr.shape)
Attempts:
2 left
💡 Hint
By default, missing values become NaN in float arrays.
✗ Incorrect
Without specifying
filling_values, missing entries become nan and the array is float type with shape (3,3).🔧 Debug
advanced2:00remaining
Identify the error when loading CSV with missing data
What error will this code raise when trying to load CSV data with missing values using
np.genfromtxt() without specifying filling_values?NumPy
import numpy as np from io import StringIO data = """ 1,2,3 4,,6 7,8,9 """ arr = np.genfromtxt(StringIO(data), delimiter=',', dtype=int) print(arr)
Attempts:
2 left
💡 Hint
Missing values cannot be converted to int without filling.
✗ Incorrect
Missing entries are empty strings which cannot convert to int, causing a ValueError.
🚀 Application
expert3:00remaining
Handling mixed missing data types with np.genfromtxt()
You have a CSV file with numeric and string columns, some missing values in both. Which
np.genfromtxt() call correctly loads the data, replacing missing numeric values with -999 and missing strings with 'missing'?NumPy
import numpy as np from io import StringIO data = """ 1,apple,3.5 2,, ,banana,4.1 """
Attempts:
2 left
💡 Hint
Use a dictionary for filling_values to specify per-column replacements.
✗ Incorrect
Using a dictionary for filling_values allows specifying different fill values per column, matching their data types.