All topics
NumPy and pandas
The data tools under every ML pipeline: NumPy arrays, shapes, axes and broadcasting, pandas DataFrames, missing values and grouping.
Sort:
5 questions
JuniorTheoryCommonWhat does this program print?
JuniorTheoryCommon
What does this program print?
(3,) (4, 3)
Open full question →Common mistakes
- ✗Expecting
(4,):axis=0collapses the rows, leaving one mean per column, somhas 3 values. - ✗Expecting a
ValueErrorfor subtracting a(3,)array from a(4, 3)one: broadcasting matches the last axis and repeatsmfor every row.
Follow-up questions
- →What does
X.mean(axis=1)return here, and why doesX - X.mean(axis=1)fail? - →How does
keepdims=Truechange the shape of the mean and the subtraction?
JuniorTheoryCommonWhat does this program print?
JuniorTheoryCommon
What does this program print?
11 2
Open full question →Common mistakes
- ✗Expecting the mask's sum to add the values:
a > 0holdsTrueandFalse, and its sum counts theTrueentries, so it is2. - ✗Expecting
a[a > 0]to return positions: a mask keeps the elements themselves,4and7, so their sum is11.
Follow-up questions
- →How would you get the mean of the negative values of
ain one line? - →What does
a[a > 0]return when no element is positive, and what is its sum?
JuniorTheoryCommonWhat does this program print?
JuniorTheoryCommon
What does this program print?
6 3
Open full question →Common mistakes
- ✗Expecting
3for the list:a * 2repeats a Python list, so it holds 6 elements; only a numpy array multiplies each element. - ✗Expecting
6for the array:b * 2multiplies each element of a numpy array, so the result[2, 4, 6]keeps length 3.
Follow-up questions
- →What do
a + aandb + bgive here, and why do they differ? - →What happens when you multiply the list
aby1.5, and the arraybby1.5?
JuniorTheoryCommonWhat does this program print?
JuniorTheoryCommon
What does this program print?
{'a': 110.0, 'b': 125.0}
Open full question →Common mistakes
- ✗Expecting
270.0for teama: that is the mean, which the outlier600drags up; the median is the middle of100, 110, 600, so it is110.0. - ✗Expecting
130.0for teamb: with an even number of values the median is the mean of the two central ones,(120 + 130) / 2, so125.0.
Follow-up questions
- →Which salary figure would you report for team
aas typical, and why? - →What would the median of team
abecome if600were replaced by6000?
JuniorTheoryCommonWhat does this program print?
JuniorTheoryCommon
What does this program print?
[1, 2]
Open full question →Common mistakes
- ✗Expecting
[0, 2, 1], a count per row:sum()adds down each column by default, so you get one count per column — 1 forage, 2 forcity. - ✗Expecting
Nonein a text column to go uncounted:isna()marks bothNaNandNoneas missing.
Follow-up questions
- →How would you count the missing values in each row instead?
- →How do you get the share of missing values per column rather than the count?