Hello Dataquest community
In the NumPy Boolean Masks Practice Problems series there are a few problems where we are instructed to create a 1-dimensional array with the help of a boolean mask.
In the Data Engineer Path > STEP 5 of 6 Handling Large Data Sets In Python, COURSE 1 / 5 Numpy for Data Engineers, Datasets and Boolean Indexing, the 9.Boolean Masks in Higher Dimensions screen (https://app.dataquest.io/m/509/datasets-and-boolean-indexing/9/boolean-masks-in-higher-dimensions) very clearly states
However, the result of applying a boolean mask is always a 1-dimensional array, even when applied to 2-dimensional arrays.
However, in 6 practice problems I found out that the resulting nd array is actually 2 dimensional.
So I’m currently quite confused
Here are the practice problems concerned followed by my code if you could please advise me on what I am missing to explain this very-strange-to-me results.
#Create a 1-dimensional array named rows_larger_30 that contains the rows of the provided ndarray x whose sum is larger than 30. rows_larger_30 = x[x.sum(axis=1) > 30] print(rows_larger_30.ndim) #Output 2
#Create a ndarray named cols_larger_30 that contains the columns of the provided ndarray x whose sum is larger than 30. cols_larger_30 = x[:,x.sum(axis=0) > 30] print(cols_larger_30.ndim) #Output 2
#Create a 1-dimensional array named rows_without_zeros that contains all rows of x that do not contain zeros. mask = np.count_nonzero(x, axis=1) == x.shape rows_without_zeros = x[mask] print(rows_without_zeros.ndim) #Output 2
#Create a 1-dimensional array named rows_with_zeros that contains all rows of x that contain at least one zero. mask = np.count_nonzero(x, axis=1) < x.shape rows_with_zeros = x[mask] print(rows_with_zeros.ndim) #Output 2
#Create a 1-dimensional array named cols_without_zeros that contains all columns of x that do not contain zeros. mask = np.count_nonzero(x, axis=0) == x.shape cols_without_zeros = x[:,mask] print(cols_without_zeros) #Output 2
#Create a 1-dimensional array named cols_with_zeros that contains all columns of x that contain at least one zero. mask = np.count_nonzero(x, axis = 0) < x.shape cols_with_zeros = x[:,mask] print(cols_with_zeros.ndim)
The dataquest solutions (when different from mine) also output a 2 dimensional arrays.
These solutions are all currently accepted when submitted.
I don’t understand since to me, these are clearly boolean masks that filter depending on the condition a 2d array and should output a 1d array. I don’t get what’s different from the previous practice problems at the beginning of the series which do output a 1d array from a 2d array filtered by a boolean mask.
Thank you in advance for your explanations