Skip to content

NumPy Demo, numpy_basic_demo.py

主要的函式介紹

np.array()
mean()
max()
min()
sum() 
以上功能可透過以下的 demo code 了解!

axis(軸向) 的關鍵觀念:不加 axis 是全班統計,加了 axis=0 或 1 是各科還是個人統計。

                  國文   英文   數學   程式
         Alice  [  85,   90,    78,   92 ] ──┐
          Bob   [  70,   80,    95,   88 ] ──┼──> axis=1 (橫向壓扁,算個人)
        Charlie [  60,   65,    70,   75 ] ──┘
                    │     │      │     │
                    ▼     ▼      ▼     ▼
                axis=0 (縱向壓扁,算各科)
  • 不寫 axis:整張紙揉成一團,算全校/全體單一數值。
  • axis=0(第一維度 / 列消失):
  • 白話:由上往下壓扁,把第 1 個人、第 2 個人、第 3 個人壓在一起。
  • 結果:剩下 4 個數字,代表各科目的統計值(形狀為 (4,))。
  • axis=1(第二維度 / 欄消失):
  • 白話:由左往右壓扁,把國、英、數、程擠在一起。
  • 結果:剩下 3 個數字,代表每位學生的統計值(形狀為 (3,))。
import numpy as np

# ==============================================================================
# Demo 設定:3 位學生參加 4 門科目的期中考成績單
# 橫列 (Row): 學生 (Alice, Bob, Charlie)
# 直行 (Col): 科目 (國文, 英文, 數學, 程式)
# ==============================================================================

# 1. np.array(): 將 Python List 轉換為高效能的連續記憶體二維陣列
scores = np.array([
    [85, 90, 78, 92],  # Alice
    [70, 80, 95, 88],  # Bob
    [60, 65, 70, 75]   # Charlie
])

print("=== 原始成績單 (shape: 3 列 x 4 欄) ===")
print(scores)
print("陣列維度 (ndim):", scores.ndim)
print("陣列形狀 (shape):", scores.shape)  # (3, 4) 代表 (3 位學生, 4 個科目)

print("\n" + "=" * 50)

# ==============================================================================
# 2. 全域統計 (不帶 axis 參數):把整個表格壓成一個數字
# ==============================================================================
print("【全域統計:整張考卷所有成績】")
print("全體總分 (sum) :", np.sum(scores))
print("全體平均 (mean):", round(np.mean(scores), 2))
print("全體最高 (max) :", np.max(scores))
print("全體最低 (min) :", np.min(scores))

print("\n" + "=" * 50)

# ==============================================================================
# 3. 沿著 axis=0 統計 (由上往下直向壓扁):求「各科目的統計」
# ==============================================================================
# 想像把 3 位學生的成績往下疊壓成一列,剩下 4 個科目的分數
print("【各科目統計 (axis=0, 縱向壓縮)】")
print("各科總分 (sum) :", np.sum(scores, axis=0))
print("各科平均 (mean):", np.mean(scores, axis=0))
print("各科最高 (max) :", np.max(scores, axis=0))
print("各科最低 (min) :", np.min(scores, axis=0))

print("\n" + "=" * 50)

# ==============================================================================
# 4. 沿著 axis=1 統計 (由左往右橫向壓扁):求「每位學生的個人統計」
# ==============================================================================
# 想像把 4 個科目的成績往右推壓成一行,剩下 3 位個人的分數
print("【每位學生統計 (axis=1, 橫向壓縮)】")
print("個人總分 (sum) :", np.sum(scores, axis=1))
print("個人平均 (mean):", np.round(np.mean(scores, axis=1), 2))
print("個人最高 (max) :", np.max(scores, axis=1))
print("個人最低 (min) :", np.min(scores, axis=1))

執行結果

=== 原始成績單 (shape: 3 列 x 4 欄) ===
[[85 90 78 92]
 [70 80 95 88]
 [60 65 70 75]]
陣列維度 (ndim): 2
陣列形狀 (shape): (3, 4)

==================================================
【全域統計:整張考卷所有成績】
全體總分 (sum) : 948
全體平均 (mean): 79.0
全體最高 (max) : 95
全體最低 (min) : 60

==================================================
【各科目統計 (axis=0, 縱向壓縮)】
各科總分 (sum) : [215 235 243 255]
各科平均 (mean): [71.66666667 78.33333333 81.         85.        ]
各科最高 (max) : [85 90 95 92]
各科最低 (min) : [60 65 70 75]

==================================================
【每位學生統計 (axis=1, 橫向壓縮)】
個人總分 (sum) : [345 333 270]
個人平均 (mean): [86.25 83.25 67.5 ]
個人最高 (max) : [92 95 75]
個人最低 (min) : [78 70 60]


Think more ?

「如果今天要做 AI 特徵標準化(Z-score),我們要算的是每個特徵(每一欄)的平均值,
我們應該下 np.mean(data, axis=0) 還是 axis=1?」

答案是 
0(消除樣本列,留下各特徵欄的平均) axia = "xu/6" <= 注音

Leave a comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *