Course materials · Introduction to Machine Learning

NumPy

English translation

Learning objectives

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

Aggregation along Each 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

import numpy as np
import matplotlib.pyplot as plt
X=np.array([[2,4,6],[1,3,5],[0,2,4],[3,5,7]])
print(X.mean(axis=0),X.mean(axis=1))

Record results and self-check

Operation:

Expected shape:

Actual shape:

Results:

Points to check

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

X2=X.astype(float).copy();X2[:,-1]*=100

Record results and self-check

Dataset and column:

Means before and after the change:

Standard deviations before and after the change:

Standardization results:

Points to check

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

# Use the preceding activity's data and functions to check the specified new examples.

Record results and self-check

Constant column position and value:

Mean and standard deviation:

Rule for a denominator of 0:

Transformed column and finite-value check:

Points to check