Python Review
English translation
Combine missing-value handling and array conversion into a small function so the same data preparation can be reused across analyses.
Before practice
01 What changes if you write separate code to handle missing values for each dataset?
Description
Different rules may otherwise be applied to values with the same meaning, so use a shared function to keep the criteria consistent.
02 Will the same function work when the sample count changes?
Description
Check that no operation depends on a fixed length and that the handling rules for empty input and missing values remain consistent.
1. 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.
Items to record and points to 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?
2. 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.
Items to record and points to 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?
3. 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.
Items to record and points to 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?