Course materials · AI Applications

Read arrays and explain them with plots

English translation

AI Applications · Week 3

This week's goal · Connect table rows and columns to code shapes and present computed results as readable plots.

Run: Save a copy to your Drive in Colab, then run Code Cells from the top using ▶ or Shift+Enter. Double-click Text Cells marked ⟦Enter your response⟧ to edit them.

From Python to NumPy

Python lists and dictionaries group values; for and if process them through repetition and conditions. NumPy applies the same computation across an array. For scores=[72,85,90], both [v for v in scores if v>=80] and np.array(scores)[np.array(scores)>=80] select 85 and 90. The former checks conditions individually; the latter creates a Boolean Mask. Use dict to retrieve values by name, as in {"voltage":220,"current":1.5}.

1. From a single value to a collection

One score is a number; multiple scores can be grouped in a list. A NumPy array holds values of the same type in a fixed shape for numerical operations. shape=(4,5) means 20 values arranged in 4 rows and 5 columns. ndim is the number of axes, and size is the total number of elements.

From a single value to a collection

The list [1,2]*2 becomes the concatenated [1,2,1,2], whereas multiplying an array by 2 doubles each element. Do not transfer list operation semantics directly to arrays.

2. Selection and reshaping

With indexing starting at 0, data[1,2] selects the second row and third column. data[1:3,1:4] selects two rows and three columns, excluding the end indices. reshape preserves element count, so 20 elements cannot become 3×7.

Selection and reshaping

score>=80 is a bool array indicating whether each position meets the condition, rather than the values themselves. score[score>=80] retrieves the actual values at those positions. Print the condition and the selection result separately.

3. The same symbol performs different computations

A*B multiplies matching elements. A@B collects row-column inner products to perform Matrix Multiplication. The result of (m,n)@(n,p) is (m,p). Distinguish the maximum from its location: max returns values, while argmax returns indices.

The same symbol performs different computations

A square matrix does not always have an Inverse Matrix. For linear equations, generally use solve(A,b) rather than inv(A)@b. When examining the Inverse Matrix itself in this assignment, use allclose to check whether A@inv(A) is close to the identity matrix.

4. Plots provide the final explanation of a computation

First match x and y lengths and plot the line, then add a title, axis names, units, and Legend. Use subplots for metrics with different units. Find the maximum's position with argmax and mark it with annotate to make it easy to locate.

Plots provide the final explanation of a computation

Decreasing training loss and increasing accuracy are related but do not always move together. Loss responds to prediction confidence, while accuracy counts only correct predictions. Do not interpret the example curves as universal laws.

Check your understanding

  1. What values should go into the code, and what does each row and column of the resulting array represent? If calculating physical quantities, have you also checked the units?
  2. Can you identify which parts to rerun when you change one condition?
  3. Do the results match your expectations? If they differ, can you explain why using the output values or plots?

NumPy basics · Getting started with Matplotlib

Practice materials