หากต้องการหาการเข้าสมดุลแบบ least-squares ของอนุกรมเชบิเชฟ (Chebyshev series) ต่อข้อมูล ให้ใช้ chebyshev.chebfit() ใน Python NumPy
เมธอดคืนค่าสัมประสิทธิ์เชบิเชฟ เรียงจากอันดับต่ำไปสูง หาก y เป็น 2 มิติ สัมประสิทธิ์สำหรับข้อมูลในคอลัมน์ k ของ y จะอยู่ในคอลัมน์ k
พารามิเตอร์สำคัญ
- x: พิกัด x ของจุดข้อมูล M จุด (x[i], y[i])
- y: พิกัด y ของจุดตัวอย่าง ชุดข้อมูลหลายชุดที่มีพิกัด x เหมือนกันสามารถ fit พร้อมกันได้โดยให้ y เป็นอาร์เรย์ 2 มิติ
- deg: อันดับของพหุนามที่ fit หากเป็น integer เดียว จะรวมทุกพจน์จนถึงอันดับ deg
- rcond: เลข شرطสัมพัทธ์ (relative condition number) ค่า singular values ที่เล็กกว่า rcond เทียบกับค่าสูงสุดจะถูกข้าม (default: len(x)*eps)
- full: สลับรูปแบบการคืนค่า False (default) = คืนแค่สัมประสิทธิ์, True = คืนข้อมูล診斷จาก SVD ด้วย
- w: น้ำหนัก (weights) สำหรับ residual หากไม่ใช่ None จะน้ำหนัก w[i] ให้กับ residual ที่ x[i]
ขั้นตอนการใช้งาน
1. นำเข้าไลบรารีที่จำเป็น
import numpy as np
from numpy.polynomial import chebyshev as C
2. สร้างพิกัด x
x = np.linspace(-1, 1, 51)
3. แสดงพิกัด x
print("X Co-ordinate...\n", x)
4. สร้างพิกัด y (พร้อม noise แบบสุ่ม)
y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n", y)
5. หา least-squares fit ด้วย chebyshev.chebfit() (deg=3, full=True)
c, stats = C.chebfit(x, y, 3, full=True)
print("\nResult (coefficients)...\n", c)
print("\nResult (stats)...\n", stats)
ตัวอย่างโค้ดเต็ม
import numpy as np
from numpy.polynomial import chebyshev as C
# พิกัด x
x = np.linspace(-1, 1, 51)
print("X Co-ordinate...\n", x)
# พิกัด y: x³ - x + noise
y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n", y)
# Least-squares fit อนุกรมเชบิเชฟอันดับ 3
c, stats = C.chebfit(x, y, 3, full=True)
print("\nResult (coefficients)...\n", c)
print("\nResult (stats)...\n", stats)
ผลลัพธ์ที่ได้ (ตัวอย่าง - จะเปลี่ยนแปลงไปตาม random seed)
X Co-ordinate...
[-1. -0.96 -0.92 ... 0.96 1.]
Y Co-ordinate...
[ 0.04578661 -0.41009751 ... 0.46429998]
Result (coefficients)...
[-0.12730537 -0.08699379 -0.4211565 0.32959334]
Result (stats)...
[array([43.34485511]), 4, array([1.20144978, 1.19227163, 0.76058422, 0.74600162]), 1.1324274851176597e-14]
คำอธิบายเพิ่มเติม
ในตัวอย่าง พยายาม fit ฟังก์ชัน y = x³ - x ที่มี noise ด้วยอนุกรมเชบิเชฟอันดับ 3
สัมประสิทธิ์ที่ได้: [-0.127, -0.087, -0.421, 0.330] แทน:
C(x) = -0.127·T₀ - 0.087·T₁ - 0.421·T₂ + 0.330·T₃
สถิติ (stats) เมื่อ full=True:
- Singular values: ค่าเอกค่าของเมทริกซ์ออกแบบ
- Rank: อันดับของเมทริกซ์
- rcond: ค่าเกณฑ์สำหรับการตัดค่า singular values เล็ก
- Residuals: ผลรวมกำลังสองของ residuals
ข้อดีของการ fit ด้วยเชบิเชฟ:
- เสถียรทางตัวเลขดีกว่าพหุนามมาตรฐาน (Vandermonde matrix)
- กระจายข้อผิดพลาดอย่างสม่ำเสมอ (minimax property)
- เหมาะสำหรับ interpolation และ approximation
ใช้ chebyshev.chebfit() หา least-squares fit อนุกรมเชบิเชฟอันดับ 3 ต่อข้อมูล y=x³-x+noise ได้สัมประสิทธิ์และสถิติ SVD