Course materials · Machine Learning Lab

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

  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

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

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

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

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

# 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