Course materials · Machine Learning Lab

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.

Practice

Preview practice notebookDownload practice notebook

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

  1. Record the original row count, complete row count, and positions of rows with missing values.
  2. Convert only complete rows to a NumPy array, and calculate each column's mean and maximum.
  3. Combine the processing into a prepare(rows) function.

Results to retain

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

  1. Process both inputs using the same prepare function.
  2. Specify the return rule for empty input and write an assert.
  3. Compare the results of a row-by-row loop with NumPy aggregation.

Results to retain

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

  1. Use different symbols for the original data and rows2.
  2. Label each point with its original row number so you can trace its source.
  3. Briefly summarize how data selection affected the plot.

Results to retain

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?