NumPy
English translation
Learning objectives
Practice calculating aggregates by sample and by feature without confusing rows and columns.
Aggregation along Each Axis
Starting conditions
, where rows are samples and columns are features.
Procedure
- Predict the means for
axis=0andaxis=1, then check with NumPy. - Select the first two rows, the last column, and elements that are 4 or greater, respectively.
Results to retain
- A table with at least 5 rows of aggregation and selection results.
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
- Did you avoid swapping the meanings of rows and features?
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
- Preserve
Xand multiply only the last column of a copy,X2, by 100. Record each column's mean and standard deviation before and after the change. - Subtract each column's mean from that column of
X2, and divide by its standard deviation. Useddof=0, matching NumPy's default. - 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
- A table comparing the data and the mean and standard deviation of each column.
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
- Did you use each column's own mean and standard deviation for standardization?
Checking Other Inputs
Starting conditions
Add a constant feature column and handle the case where the Standardization denominator is 0.
Procedure
- 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. - 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.
- 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
- The rule for handling the constant column, transformed results, and finite-value check.
# 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
- Did you record how columns with a standard deviation of 0 were handled and their actual outputs?