NumPy

English translation

Practice calculating aggregates by sample and by feature without confusing rows and columns.

Before practice

01 What do axis=0 and axis=1 aggregate?

Description

Aggregate a 2-dimensional array by reducing values along the row and column directions, respectively.

02 What happens if you standardize a constant column?

Description

The standard deviation is 0, so a separate division rule is needed.

Practice

Preview practice notebookDownload practice notebook

1. Aggregation by Axis

Starting conditions

X=[246135024357]X=\begin{bmatrix}2&4&6\\1&3&5\\0&2&4\\3&5&7\end{bmatrix}, where rows are samples and columns are features.

Procedure

  1. Predict the means for axis=0 and axis=1, then check with NumPy.
  2. Select the first two rows, the last column, and elements that are 4 or greater, respectively.

Results to retain

Items to record and points to check
Operations
Expected shape
Actual shape
Result

Points to check

  • Did you avoid swapping the meanings of rows and features?

2. Features with Different Scales

Starting conditions

Create data with only the last column multiplied by 100, and compare column-wise means and standard deviations before and after standardization.

Procedure

  1. Preserve X and multiply only the last column of a copy, X2, by 100. Record each column's mean and standard deviation before and after the change.
  2. Subtract each column's mean from that column of X2, and divide by its standard deviation. Use ddof=0, matching NumPy's default.
  3. Record each column's means and standard deviations before and after Standardization side by side. Use both statistics to explain whether the column multiplied by 100 is now on the same scale as the other columns.

Results to retain

Items to record and points to check
Data and column
Means before and after the change
Standard deviations before and after the change
Standardization results

Points to check

  • Did you use each column's own mean and standard deviation for standardization?

3. Checking Other Inputs

Starting conditions

Add a constant feature column and handle the case where the Standardization denominator is 0.

Procedure

  1. Append one column with the same value in every row to X, and record its value and column position. For example, append a column filled with 1.
  2. Calculate each column's mean and standard deviation, and identify columns whose standard deviation is 0. Add a rule to the code for dividing those columns. For example, set only those denominators to 1.
  3. Record the transformed constant column and np.isfinite(...).all() for the entire result. Explain what changes if you divide by the constant column's unmodified standard deviation.

Results to retain

Items to record and points to check
Constant column position and value
Mean and standard deviation
Rule for handling a denominator of 0
Transformed column and finite-value check

Points to check

  • Did you record how columns with a standard deviation of 0 were handled and their actual outputs?