เพื่อหา Least squares fit ของชุด Hermite_e กับข้อมูล ให้ใช้เมธอด hermite_e.hermfit() ใน Python numpy เมธอดคืนค่า係ของ Hermite_e เรียงจากต่ำไปสูง หาก y เป็น 2-D 係สำหรับข้อมูลในคอลัมน์ k ของ y จะอยู่ในคอลัมน์ k พารามิเตอร์ x คือพิกัด x ของจุดตัวอย่าง M จุด (x[i], y[i])
พารามิเตอร์ y คือพิกัด y ของจุดตัวอย่าง หลายชุดตัวอย่างที่มีพิกัด x เหมือนกันสามารถ fit แยกกันได้ด้วยการส่ง y เป็นอาร์เรย์ 2-D ที่มีชุดข้อมูลต่อคอลัมน์ พารามิเตอร์ deg คืออันดับของพหุนามการ fit หาก deg เป็น integer เดียว ทุกเทอมจนถึง deg จะรวมอยู่ใน fit พารามิเตอร์ rcond คือ relative condition number ของ fit ค่า singular ที่เล็กกว่า rcond เทียบกับค่าสูงสุดจะถูกเพิกเฉย ค่า default คือ len(x)*eps โดย eps คือ relative precision ของ float type ประมาณ 2e-16
พารามิเตอร์ full คือสวิตช์กำหนดลักษณะคืนค่า เมื่อ False (default) คืนแค่係 เมื่อ True คืนข้อมูล diagnostic จาก singular value decomposition ด้วย พารามิเตอร์ w คือน้ำหนัก หากไม่ใช่ None น้ำหนัก w[i] จะนำไปคูณกับ unsquared residual y[i] - y_hat[i] ที่ x[i] ค่า default คือ None
ขั้นตอน
ก่อนอื่น import ไลบรารีที่จำเป็น:
import numpy as np
from numpy.polynomial import hermite_e as H
พิกัด x:
x = np.linspace(-1, 1, 51)
แสดงพิกัด x:
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 ด้วย hermite_e.hermfit():
c, stats = H.hermefit(x, y, 3, full=True)
print("\nResult...\n", c)
print("\nResult...\n", stats)
ตัวอย่าง
import numpy as np
from numpy.polynomial import hermite_e as H
# The x-coordinate
x = np.linspace(-1, 1, 51)
# Display the x-coordinate
print("X Co-ordinate...\n", x)
# The y-coordinate
y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n", y)
# To get the Least squares fit of Hermite_e series to data
c, stats = H.hermefit(x, y, 3, full=True)
print("\nResult...\n", c)
print("\nResult...\n", stats)
ผลลัพธ์
X Co-ordinate... [-1. -0.96 -0.92 -0.88 -0.84 -0.8 -0.76 -0.72 -0.68 -0.64 -0.6 -0.56 -0.52 -0.48 -0.44 -0.4 -0.36 -0.32 -0.28 -0.24 -0.2 -0.16 -0.12 -0.08 -0.04 0. 0.04 0.08 0.12 0.16 0.2 0.24 0.28 0.32 0.36 0.4 0.44 0.48 0.52 0.56 0.6 0.64 0.68 0.72 0.76 0.8 0.84 0.88 0.92 0.96 1. ] Y Co-ordinate... [-0.54079609 -1.17586687 -0.81506394 0.8047718 -1.21403444 -1.09247646 -0.88942226 -0.62335081 0.83995142 0.29147171 2.45859847 -0.37545462 0.90161986 -0.7125131 -0.82978518 0.25422338 0.62073702 -1.43305948 0.96436296 0.03069738 -1.07349677 0.55233582 1.23286374 0.37330458 0.27239629 0.46859691 -0.1074476 1.19279741 0.15844038 -0.20424904 -1.41467693 -0.79396457 -2.38068246 -1.24121297 -0.7877071 -1.09171002 1.0806185 -0.94389035 -2.16201749 0.21671724 -1.15596405 0.57090598 -0.52496753 -0.20358065 -3.72121093 1.39868958 -0.02626711 -1.51582035 -0.12223608 -0.58368042 0.69138128] Result... [-0.54892802 4.71593168 -0.40858959 2.08689429] Result... [array([51.90771673]), 4, array([1.41192215, 1.37967947, 0.31061966, 0.08047256]), 1.1324274851176597e-14]