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

การหาค่าประมาณเชิงพยายามน้อยที่สุด (Least Squares Fit) ของอนุกรม Laguerre กับข้อมูลใน Python


บทนำ

ในการหาค่าประมาณเชิงพยายามน้อยที่สุด (Least squares fit) ของอนุกรม Laguerre กับข้อมูล สามารถใช้เมธอด laguerre.lagfit() จาก NumPy เมธอดนี้คืนค่าสัมประสิทธิ์ Laguerre เรียงจากต่ำไปสูง หาก y เป็น 2 มิติ สัมประสิทธิ์ของข้อมูลในคอลัมน์ k จะอยู่ในคอลัมน์ k

พารามิเตอร์สำคัญ

  • x: พิกัด x ของจุดข้อมูล M จุด
  • y: พิกัด y ของจุดข้อมูล
  • deg: องศาของพหุนามที่ใช้擬合
  • rcond: เงื่อนไขสัมพัทธ์ของการ fit
  • full: สลับการคืนค่า ข้อมูลวินิจฉัยเพิ่มเติม
  • w: น้ำหนักของข้อมูล

ขั้นตอนการทำงาน

1. นำเข้าไลบรารี

import numpy as np
from numpy.polynomial import laguerre as L

2. สร้างพิกัด x

x = np.linspace(-1, 1, 51)

3. สร้างพิกัด y พร้อม雜訊

y = x**3 - x + np.random.randn(len(x))

4. หา Least squares fit

c, stats = L.lagfit(x, y, 3, full=True)
print("\nResult...\n", c)
print("\nResult...\n", stats)

ตัวอย่างโค้ดสมบูรณ์

import numpy as np
from numpy.polynomial import laguerre as L

# พิกัด x
x = np.linspace(-1, 1, 51)
print("X Co-ordinate...\n", x)

# พิกัด y
y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n", y)

# หา Least squares fit
c, stats = L.lagfit(x, y, 3, full=True)
print("\nResult...\n", c)
print("\nResult...\n", stats)

ผลลัพธ์ตัวอย่าง (สุ่ม)

X Co-ordinate...   [-1. -0.96 -0.92 ... 0.96 1.]
Y Co-ordinate...   [ 2.60011413 0.59715605 ... 1.10442549]
Result...   [ 11.2805293 -36.35804353 36.47911284 -11.65554029]
Result...   [array([43.46828156]), 4, array([1.88377481, 0.66402594, 0.10220349, 0.00405509]), 1.1324274851176597e-14]