ฮาวทู numpy

จดไว้ก่อน เจ้า lib numpy เป็น Lib ที่ทำให้ตัว Python ทำการกับตัว Numerical เก่งขึ้น ทำงานกับพวกตัวเลขได้ดีขึ้น โดย NumPy ชื่อเต็ม Numerical Python เหมือนได้ฟังจากตัวงาน Global AI Bootcamp Bangkok 2025 เหมือน Lib นี้เป็นตัวเปิดให้ Python เข้ามาทำงานด้าน AI ได้ง่ายขึ้น และแบบเป็นฐานให้หลาย Lib เอาไปใช้งานต่อยอด อย่างตัว Pandas / PyTorch เป็นต้น

ลองมาดูการใช้งานกันครับ ผมลองเอามาเทียบๆกับ R ด้วยครับ

Get NumPy

  • install lib
pip install numpy
  • using lib ใน code
import numpy as np

Sample Usecase

- Create Array / Vector
  • Python
arr = np.array([1, 2, 3, 4, 5])
  • R
arr <- c(1, 2, 3, 4, 5)
- Resize Array / Vector
  • Python
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape((2, 3))
  • R
arr <- c(1, 2, 3, 4, 5, 6)
reshaped_arr <- matrix(arr, nrow=2, ncol=3)
- Create Matrix
  • Python
matrix = np.array([[1, 2, 3], [4, 5, 6]])
  • R
matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, ncol=3)
- Basic Operations
  • Python
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
sum_arr = arr1 + arr2
  • R
arr1 <- c(1, 2, 3)
arr2 <- c(4, 5, 6)
sum_arr <- arr1 + arr2
- Statistic Operations
  • Python
mean = np.mean(arr)
median = np.median(arr)
std_dev = np.std(arr)
  • R
mean <- mean(arr)
median <- median(arr)
std_dev <- sd(arr)

จริงๆมีอีกหลายตัว ดูเพิ่มเติมจาก doc > https://numpy.org/doc/stable//reference/routines.statistics.html

- Where
  • Python
import numpy as np

# Sample array
arr = np.array([1, 2, 3, 4, 5])

# Using np.where to find elements greater than 3
result = np.where(arr > 3, arr, -1)
print(result)  # Output: [-1 -1 -1  4  5]
  • R
# Sample vector
arr <- c(1, 2, 3, 4, 5)

# Using ifelse to find elements greater than 3
result <- ifelse(arr > 3, arr, -1)
print(result)  # Output: [1] -1 -1 -1  4  5
- Save Object
  • Python แบบทั่วไป
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Save to CSV
np.savetxt('array.csv', arr, delimiter=',')

# Reading a CSV file
data = np.loadtxt('array.csv', delimiter=',')
print(data)
  • NumPy มันมี format ของมันเองนะ .npy 
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Save NPY file
np.save('array.npy', arr)

# Reading an NPY file
data = np.load('array.npy')
print(data)
  • กลับมาที่ R Recap ก่อน มันมี RData นะ
example_object <- list(a = 1, b = 2, c = 3)
# Save the object to a file
save(example_object, file = "example_object.RData")

# Load the object from the file
load("example_object.RData")
print(example_object)

ในกรณีที่ Python ต้อง Read RData ใช้ตัว pyreadr ตัวอย่างตามนี้ //เคยเจอเคสคนทำเดิมออก Code R ยังเป็นตำนานที่ไม่มีคนแก้ต่อ 555

import pyreadr

# Path to the RDS file
file_path = 'your_file.rds'

# Read the RDS file as dictionary 
result = pyreadr.read_r(file_path)

# Accessing the data
for key in result.keys():
    print(f"Object name: {key}")
    print(result[key])

แถมเคสที่ R Read RData ต้องลง RcppCNPy

# Load NPY file
library(RcppCNPy)
arr_npy <- npyLoad('array.npy')

print(arr_npy)

จริงๆมีอีกหลายเคสนะ ลองดูได้จาก https://numpy.org/doc/stable/


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.