Module 12a — ทบทวน (Review)

N-dimensional Array using NumPy (I) — แบบฝึกทบทวน

ทบทวน Module 12a 📝

  • Multiple Choice — เลือกคำตอบที่ถูก แล้วกดถัดไปเพื่อดูเฉลย
  • ทำนายผล · หา Bug
  • เติมคำในช่องว่าง — ลองพิมพ์ใน Live Code แล้วรัน (มี NumPy จริง)

. . .

Note

ครอบคลุม: สร้าง array · .shape/.dtype · element-wise · broadcasting · np.arange · สถิติ + axis ทบทวนเนื้อหาเต็มได้ที่ Module 12a 👈

Multiple Choice 🔘

เลือกคำตอบที่ถูกต้อง — เฉลยจะปรากฏเมื่อกดถัดไป

คำถาม #1

รูปแบบ import ที่นิยมสำหรับ NumPy คือข้อใด?

A. import numpy as np
B. import np
C. from numpy import all

Tip

ตอบ: A — ตั้ง alias np แล้วเรียก np.array(), np.arange() ได้กระชับ

คำถาม #2

ทุก item ใน NumPy array เดียวกันต้องเป็นอย่างไร?

A. ต่างชนิดกันได้
B. ชนิดเดียวกันทั้งหมด
C. เป็นตัวเลขเท่านั้น

Tip

ตอบ: B — array บังคับให้ทุก item ชนิดเดียวกัน (ต่างจาก List) — ถ้าต่างชนิด NumPy จะแปลงให้อัตโนมัติ

คำถาม #3: ชวนคิด

.shape เขียนโดยไม่มี () ต่อท้าย เพราะอะไร?

A. เป็น property (บอกสถานะ) ไม่ใช่ function
B. เป็น keyword
C. เขียนผิด

Important

ตอบ: A.shape, .dtype เป็น property (ไม่มี ()) · ส่วน .array(), .append() เป็น function (มี ())

คำถาม #4

np.array([1,2,3]) + np.array([10,20,30]) ให้ผลใด?

A. [11, 22, 33]
B. [1,2,3,10,20,30]
C. error

Tip

ตอบ: A — operator ทำงานแบบ element-wise (item ตำแหน่งเดียวกัน) ต่างจาก List ที่ + = ต่อกัน

คำถาม #5

เมื่อ array มี shape ไม่เท่ากัน NumPy ใช้กลไกใดกระจายการคำนวณ?

A. Broadcasting
B. Slicing
C. Casting

Tip

ตอบ: ABroadcasting กระจายการคำนวณได้เมื่อ shape สอดคล้องตามกฎ (เช่น array ⊕ ตัวเลขเดี่ยว)

คำถาม #6: ชวนคิด

ทำไมต้องใช้ np.arange(1, 11, 0.5) แทน range(1, 11, 0.5)?

A. np.arange สั้นกว่า
B. range() รับ step เป็น float ไม่ได้ (error)
C. เหมือนกันทุกอย่าง

Important

ตอบ: Brange() สร้างได้เฉพาะจำนวนเต็ม · np.arange() สร้างเลขทศนิยมได้ (ผลเป็น NumPy array)

คำถาม #7

np.sum(x) โดยไม่กำหนด axis ให้ผลเป็นอะไร?

A. ผลรวมแต่ละ column
B. ผลรวมแต่ละ row
C. ค่าเดียว (ยุบทั้ง array)

Tip

ตอบ: C — ไม่ระบุ axis → ยุบทั้ง array เหลือค่าเดียว · axis=0 = แต่ละ column · axis=1 = แต่ละ row

ทำนายผล 🔮

โค้ดจะพิมพ์อะไร? — ทายก่อน แล้วกดถัดไปดูเฉลย

ทำนายผล #8

import numpy as np
x = np.array([1, 2, 3, 4])
print(x ** 2)

Tip

ผลลัพธ์: [ 1 4 9 16] — ยกกำลังสองแบบ element-wise (broadcasting กับตัวเลขเดี่ยว 2)

ทำนายผล #9: ชวนคิด

import numpy as np
x = np.array([[1, 5, 3, 1],
              [0, 1, 2, 9]])
print(np.sum(x, axis=0))

Important

ผลลัพธ์: [1 6 5 10]axis=0 ยุบแถวเหลือผลรวมแต่ละ column: 1+0, 5+1, 3+2, 1+9

ทำนายผล #10: ชวนคิด

import numpy as np
a = np.array([1, 2])
b = np.array([1, 2.0])
print(a.dtype, b.dtype)

Tip

ผลลัพธ์: int64 float64b มี 2.0 (float) → NumPy แปลงทุก item เป็น float ให้เป็นชนิดเดียวกัน

หา Bug 🐞

โค้ดผิดตรงไหน — ทำไม

หา Bug #11

x = range(1, 11, 0.5)
print(list(x))

ผิดตรงไหน?

Important

ผิด: range() รับ step เป็น float ไม่ได้ → TypeError แก้: ใช้ np.arange(1, 11, 0.5)

หา Bug #12: ชวนคิด

import numpy as np
x1 = np.array([[1,2],[3,4],[5,6]])   # (3,2)
x2 = np.array([[1,2,3],[4,5,6]])     # (2,3)
print(x1 - x2)

ผิดตรงไหน?

Important

ผิด: shape (3,2) กับ (2,3) broadcast เข้ากันไม่ได้ → ValueError แก้: ให้ shape สอดคล้องกัน (เช่น transpose ตัวหนึ่ง หรือใช้ shape เท่ากัน)

เติมคำในช่องว่าง ✏️

เติมโค้ดในช่อง ____ แล้วกด Run (มี NumPy จริง)

เติมคำ #13

เติมโค้ดสร้าง array 2 มิติ แล้วดู .shape

import numpy as np

x = np.array([[1,2,3],[11,22,33],[10,20,30]])
print(x)
print(x.shape)
[[ 1  2  3]
 [11 22 33]
 [10 20 30]]
(3, 3)

เติมคำ #14

เติมโค้ดสร้าง x ตั้งแต่ 1 ถึง 10.5 เพิ่มทีละ 0.5 แล้วหา y = x^2 + 2x + 1

import numpy as np

x = np.arange(1, 11, 0.5)
y = x**2 + 2*x + 1
print(y)
[  4.     6.25   9.   ... 121.   132.25]

เติมคำ #15: ชวนคิด

เติม axis ให้ถูกต้อง — ผลรวมแต่ละ column และแต่ละ row

import numpy as np

x = np.array([[1,5,3,1],[0,1,2,9]])
print(f'Column Sum={np.sum(x, axis=0)}')
print(f'Row Sum={np.sum(x, axis=1)}')
Column Sum=[ 1  6  5 10]
Row Sum=[10 12]

เจาะจุดยาก: Broadcasting & axis 🎯

ฝึกเพิ่ม — สองจุดที่งงบ่อยสุดของ NumPy

ทำนายผล #16: ชวนคิด

import numpy as np
x = np.array([[1, 2, 3],
              [4, 5, 6]])
print(x + 10)

Tip

ผลลัพธ์:

[[11 12 13]
 [14 15 16]]

broadcasting — ตัวเลขเดี่ยว 10 ถูกกระจายบวกเข้าไปทุก item

ทำนายผล #17: ชวนคิด

import numpy as np
x = np.array([[1, 2, 3],
              [4, 5, 6]])
y = np.array([10, 20, 30])
print(x + y)

x shape (2,3) · y shape (3,) — ได้ผลอะไร?

Important

ผลลัพธ์:

[[11 22 33]
 [14 25 36]]

broadcastingy (1 row) ถูกกระจายไปบวกทุก row ของ x

ทำนายผล #18: ชวนคิด

import numpy as np
x = np.array([[1, 2],
              [3, 4],
              [5, 6]])
print(np.sum(x, axis=0))
print(np.sum(x, axis=1))

Tip

ผลลัพธ์: [ 9 12] แล้ว [ 3 7 11] axis=0 ยุบแถว → ผลแต่ละ column (1+3+5, 2+4+6) · axis=1 ยุบ column → ผลแต่ละ row

Engineering Application 🔧

ประมวลผลข้อมูลการวัดด้วย NumPy

Engineering #19: ค่าเฉลี่ยการวัด

import numpy as np
m = np.array([10.1, 9.8, 10.0, 10.2, 9.9])
print(np.mean(m))

Tip

ผลลัพธ์: 10.0 — วัดซ้ำ 5 ครั้ง แล้วหาค่าเฉลี่ยด้วย np.mean()

Engineering #20: ปรับเทียบ (calibration)

import numpy as np
raw = np.array([100, 200, 300])
calibrated = raw * 1.1
print(calibrated)

Important

ผลลัพธ์: [110. 220. 330.] — คูณ factor 1.1 กระจายเข้าไปทุก item (element-wise)

Engineering #21

เติม axis ให้หาค่าเฉลี่ยแต่ละ column ของตารางการวัด

import numpy as np
data = np.array([[1.0, 2.0],
                 [3.0, 4.0],
                 [5.0, 6.0]])
print(np.mean(data, axis=0))
[3. 4.]

Engineering #22: แรง = มวล × ความเร่ง

import numpy as np
mass = np.array([2, 4, 6])      # kg
acc = np.array([10, 5, 2])      # m/s^2
print(mass * acc)               # F = m*a

Tip

ผลลัพธ์: [20 20 12] — element-wise: หาแรงของแต่ละวัตถุพร้อมกัน \(F = ma\)

ท้าทาย 🔥

broadcasting หลายขั้นในบรรทัดเดียว

ท้าทาย #23: Normalize ต่อ column

ทำให้แต่ละ column มี mean 0, std 1: \((x - \text{mean}) / \text{std}\) — เติม axis

import numpy as np
x = np.array([[1, 2],
              [3, 4],
              [5, 6]])
xn = (x - x.mean(axis=0)) / x.std(axis=0)
print(xn)
[[-1.22474487 -1.22474487]
 [ 0.          0.        ]
 [ 1.22474487  1.22474487]]

Note

x.mean(axis=0) = [3. 4.], x.std(axis=0)[1.63 1.63] → broadcasting ลบ/หารทีละ column พร้อมกัน

จบการทบทวน 🎉

ทบทวนเนื้อหาเต็มได้ที่ Module 12a · ไปต่อ Module 12b