Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

SciPy คืออะไร? วิธีติดตั้ง และประยุกต์ใช้งานใน Python

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.optimizeOptimization, Root finding
scipy.integrateIntegration, ODE solvers
scipy.linalgLinear Algebra
scipy.fftFast Fourier Transform
scipy.signalSignal Processing
scipy.statsStatistical Functions
scipy.interpolateInterpolation
scipy.sparseSparse Matrices