SciPyScientific Python เป็นไลบรารี Python สำหรับคำนวณทางวิทยาศาสตร์และเทคนิคบนข้อมูลขนาดใหญ่ สร้างบน NumPy (ต้องติดตั้ง NumPy ก่อน) เป็น Open Source ใช้งานฟรี ได้มาตรฐานในวงการ Data Science และ Machine Learning
SciPy ให้ฟังก์ชันสำหรับจัดการอาร์เรย์ NumPy ความเร็วสูง เข้าใจง่าย มีโมดูลย่อยมากมาย เช่น:
- Integration (อินทิกรัล)
- Interpolation (การหาค่าระหว่าง)
- Optimization (การหาค่าสูงสุด/ต่ำสุด)
- Linear Algebra (พีชคณิตเชิงเส้น)
- Signal Processing (ประมวลผลสัญญาณ)
- Statistics (สถิติ)
- FFT (Fast Fourier Transform)
วิธีติดตั้ง
# Windows
pip install scipy
# macOS (MacPorts)
sudo port install py35-scipy py35-numpy
# Linux (Debian/Ubuntu)
sudo apt-get install python3-scipy python3-numpy
ตัวอย่าง: การหาอินทิกรัล
import scipy.integrate
my_fun = lambda x: 11.345 * x
i = scipy.integrate.quad(my_fun, 0, 3.1)
print("The integrated values are : ")
print(i)
ผลลัพธ์
The integrated values are : (54.512725, 6.052128243005939e-13)
ผลลัพธ์เป็น Tuple: (ค่าอินทิกรัล, ประมาณการข้อผิดพลาด) ในที่นี้คือ ~54.5127
คำอธิบาย
นำเข้าโมดูล
scipy.integrateกำหนดฟังก์ชันด้วย lambda: f(x) = 11.345x
ใช้
quad()หาอินทิกรัลจำกัดจาก 0 ถึง 3.1แสดงผลค่าอินทิกรัลและค่าข้อผิดพลาด
โมดูลย่อยสำคัญของ SciPy:
| โมดูล | หน้าที่ |
|---|---|
| scipy.optimize | Optimization, Root finding |
| scipy.integrate | Integration, ODE solvers |
| scipy.linalg | Linear Algebra |
| scipy.fft | Fast Fourier Transform |
| scipy.signal | Signal Processing |
| scipy.stats | Statistical Functions |
| scipy.interpolate | Interpolation |
| scipy.sparse | Sparse Matrices |