Python Review
English translation
Learning objectives
Combine missing-value handling and array conversion into a small function so the same data preparation can be reused across analyses.
Measurement data with missing values
Starting conditions
The 3-feature data are rows=[[1,2,3],[2,None,4],[3,4,5],[4,5,6]]. Keep rows containing None in a separate list.
Procedure
- Record the original row count, complete row count, and positions of rows with missing values.
- Convert only complete rows to a NumPy array, and calculate each column's mean and maximum.
- Combine the processing into a prepare(rows) function.
Results to retain
- A table tracking original row selection and a table of statistics by feature.
import numpy as np
import matplotlib.pyplot as plt
rows=[[1,2,3],[2,None,4],[3,4,5],[4,5,6]]
complete=[row for row in rows if None not in row]
X=np.asarray(complete,dtype=float)
print(X.shape,X.mean(axis=0))
Record results and self-check
Original row:
Complete:
Selection results:
Reason:
Points to check
- Did you keep the original input and processed results in separate variables?
- Did you check the shape after the row count changed?
Applying the same calculation to different inputs
Starting conditions
Use rows2=[[10,20,30],[20,30,40], [None,40,50]] and an empty list.
Procedure
- Process both inputs using the same prepare function.
- Specify the return rule for empty input and write an assert.
- Compare the results of a row-by-row loop with NumPy aggregation.
Results to retain
- A comparison table for 3 input types and 2 asserts.
rows2=[[10,20,30],[20,30,40],[None,40,50]]
# TODO: Apply the same prepare function.
Record results and self-check
Input:
Original count:
Valid count:
Column means:
Edge case handling:
Points to check
- Did you avoid reporting the mean of an empty array as a valid value?
- Did you avoid rewriting the function for each input?
Summarizing results with plots
Starting conditions
Use the first feature of complete rows as x and the third feature as y.
Procedure
- Use different symbols for the original data and rows2.
- Label each point with its original row number so you can trace its source.
- Briefly summarize how data selection affected the plot.
Results to retain
- A scatter plot and a table tracing the selected rows.
# TODO: Add a scatter plot of the first and third features and the row indices.
Record results and self-check
Materials:
Selected original rows:
x range:
y range:
Points to check
- Did you retain the original row numbers after removing missing values?
- Did you state which feature each axis represents?