Trăn nội suy

Có một số phương tiện nội suy chung có sẵn trong SciPy, dành cho dữ liệu ở kích thước 1, 2 và cao hơn

  • Một lớp đại diện cho một nội suy [

    >>> x = np.linspace[0, 10, num=11, endpoint=True]
    >>> y = np.cos[-x**2/9.0]
    >>> f1 = interp1d[x, y, kind='nearest']
    >>> f2 = interp1d[x, y, kind='previous']
    >>> f3 = interp1d[x, y, kind='next']
    
    8] trong 1-D, cung cấp một số phương thức nội suy

  • Chức năng tiện lợi

    >>> x = np.linspace[0, 10, num=11, endpoint=True]
    >>> y = np.cos[-x**2/9.0]
    >>> f1 = interp1d[x, y, kind='nearest']
    >>> f2 = interp1d[x, y, kind='previous']
    >>> f3 = interp1d[x, y, kind='next']
    
    9 cung cấp giao diện đơn giản để nội suy theo N chiều [N = 1, 2, 3, 4, …]. Giao diện hướng đối tượng cho các thói quen cơ bản cũng có sẵn

  • >>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
    >>> import matplotlib.pyplot as plt
    >>> plt.plot[x, y, 'o']
    >>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
    >>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
    >>> plt.show[]
    
    0 cung cấp một số phương pháp nội suy trên lưới thông thường ở các kích thước [N] tùy ý,

  • Các hàm cho phép nội suy khối 1 và 2-D [được làm nhẵn], dựa trên thư viện FORTRAN FITPACK. Chúng đều là giao diện thủ tục và hướng đối tượng cho thư viện FITPACK

  • Nội suy sử dụng hàm cơ sở xuyên tâm

Nội suy 1-D [______08]#

Lớp

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
8 trong
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
6 là một phương pháp thuận tiện để tạo một hàm dựa trên các điểm dữ liệu cố định, có thể được đánh giá ở bất kỳ đâu trong miền được xác định bởi dữ liệu đã cho bằng phép nội suy tuyến tính. Một thể hiện của lớp này được tạo bằng cách chuyển các vectơ 1-D bao gồm dữ liệu. Thể hiện của lớp này định nghĩa một phương thức __call__ và do đó có thể được coi như một hàm nội suy giữa các giá trị dữ liệu đã biết để thu được các giá trị chưa biết [nó cũng có một chuỗi tài liệu để được trợ giúp]. Hành vi tại ranh giới có thể được chỉ định tại thời điểm khởi tạo. Ví dụ sau minh họa việc sử dụng nó, cho phép nội suy spline tuyến tính và bậc ba

>>> from scipy.interpolate import interp1d

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']

________số 8

Một tập hợp các phép nội suy khác trong

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
8 là gần nhất, trước đó và tiếp theo, trong đó chúng trả về điểm gần nhất, trước đó hoặc tiếp theo dọc theo trục x. Gần nhất và tiếp theo có thể được coi là trường hợp đặc biệt của bộ lọc nội suy nhân quả. Ví dụ sau minh họa việc sử dụng chúng, sử dụng cùng dữ liệu như trong ví dụ trước

>>> from scipy.interpolate import interp1d

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']

>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]

Nội suy dữ liệu đa biến [_______09]#

Giả sử bạn có dữ liệu đa chiều, chẳng hạn, đối với một hàm cơ bản f[x, y], bạn chỉ biết các giá trị tại các điểm [x[i], y[i]] không tạo thành lưới thông thường

Giả sử chúng ta muốn nội suy hàm 2-D

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
4

trên lưới trong [0, 1]x[0, 1]

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
5

nhưng chúng tôi chỉ biết giá trị của nó tại 1000 điểm dữ liệu

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
6

Điều này có thể được thực hiện với

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
9 – bên dưới, chúng tôi thử tất cả các phương pháp nội suy

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
8

Người ta có thể thấy rằng kết quả chính xác được sao chép bằng tất cả các phương pháp ở một mức độ nào đó, nhưng đối với chức năng trơn tru này, nội suy bậc ba từng phần cho kết quả tốt nhất

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
0

Nội suy dữ liệu đa biến trên lưới thông thường [
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
0]#

Giả sử bạn có dữ liệu n chiều trên một lưới thông thường và bạn muốn nội suy nó. Trong trường hợp như vậy,

>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
0 có thể hữu ích. Ví dụ sau minh họa việc sử dụng nó và so sánh kết quả nội suy bằng từng phương pháp

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
1

Giả sử chúng ta muốn nội suy hàm 2-D này

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
2

Giả sử chúng ta chỉ biết một số dữ liệu trên lưới thông thường

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
3

Tạo điểm kiểm tra và giá trị thực để đánh giá

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
4

Chúng ta có thể tạo các điểm kiểm tra nội suy và nội suy bằng từng phương pháp

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
5

Như mong đợi, phép nội suy spline ở mức độ cao hơn gần với giá trị thực nhất, mặc dù tính toán tốn kém hơn so với tuyến tính hoặc gần nhất. Phép nội suy tuyến tính cũng phù hợp với phép nội suy tuyến tính

nội suy spline#

Nội suy spline trong 1-D. Thủ tục [nội suy. splXXX]#

Nội suy Spline yêu cầu hai bước cơ bản. [1] biểu diễn spline của đường cong được tính toán và [2] spline được đánh giá tại các điểm mong muốn. Để tìm biểu diễn spline, có hai cách khác nhau để biểu diễn một đường cong và lấy các hệ số spline [làm mịn]. trực tiếp và tham số. Phương pháp trực tiếp tìm biểu diễn spline của một đường cong trong mặt phẳng 2 chiều bằng cách sử dụng hàm

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
45. Hai đối số đầu tiên là những đối số duy nhất được yêu cầu và chúng cung cấp \[x\]\[y . Đầu ra bình thường là 3-tuple, components of the curve. The normal output is a 3-tuple, \[\left[t,c,k\right]\] , chứa các điểm nút, < . Thứ tự spline mặc định là khối, nhưng điều này có thể được thay đổi bằng từ khóa đầu vào, k. \[t\] , the coefficients \[c\] and the order \[k\] of the spline. The default spline order is cubic, but this can be changed with the input keyword, k.

Đối với các đường cong trong không gian N-D, hàm

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
46 cho phép xác định đường cong theo tham số. Đối với chức năng này, chỉ cần 1 đối số đầu vào. Đầu vào này là một danh sách \[N\] -mảng đại diện cho đường cong trong không gian N-D. Độ dài của mỗi mảng là số điểm đường cong và mỗi mảng cung cấp một thành phần của điểm dữ liệu N-D. Biến tham số được đưa ra với đối số từ khóa, u, mặc định là một chuỗi đơn điệu cách đều nhau giữa \[0\]\[1\] . The default output consists of two objects: a 3-tuple, \[\left[t,c,k\right]\] , chứa biểu diễn spline và biến tham số \[u.\]

Đối số từ khóa, s , được sử dụng để chỉ định mức độ làm mịn cần thực hiện trong quá trình khớp nối spline. Giá trị mặc định của \[s\]\[s=m-\sqrt{2m}\] . Do đó, nếu không muốn làm trơn thì giá trị where \[m\] is the number of data-points being fit. Therefore, if no smoothing is desired a value of \[\mathbf{s}=0\] sẽ được chuyển cho các quy trình.

Khi biểu diễn spline của dữ liệu đã được xác định, các hàm có sẵn để đánh giá spline [

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
47] và các dẫn xuất của nó [
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
47,
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
49] tại bất kỳ điểm nào và tích phân của spline giữa hai điểm bất kỳ [
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
50]. Ngoài ra, đối với các đường nối hình khối [ \[k=3\] ] có 8 nút trở lên, gốc của đường nối có thể được ước tính [
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
51]. Các chức năng này được thể hiện trong ví dụ sau.

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
6

khối-spline

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
7

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
8

Đạo hàm của spline

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
9

Tất cả các dẫn xuất của spline

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
0

tích phân của spline

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
1

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
2

Rễ của spline

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
3

Lưu ý rằng

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
51 có thể không tìm được nghiệm rõ ràng ở rìa của khoảng xấp xỉ, \[x = 0\] . Nếu chúng tôi xác định spline trên một khoảng lớn hơn một chút, chúng tôi sẽ phục hồi cả hai gốc \[x = 0\] . :

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
4

spline tham số

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
5

Nội suy spline trong 1-d. Hướng đối tượng [
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1]#

Các khả năng điều chỉnh spline được mô tả ở trên cũng có sẵn thông qua giao diện hướng đối tượng. Các spline 1-D là các đối tượng của lớp

>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1 và được tạo bằng \[x\] . Lớp định nghĩa
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
55, cho phép gọi đối tượng với các giá trị trục x, tại đó spline sẽ được đánh giá, trả về các giá trị y được nội suy. Điều này được thể hiện trong ví dụ dưới đây cho phân lớp
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
56. Các phương thức
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
57,
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
58 và
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
59 cũng có sẵn trên các đối tượng
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1, cho phép tính toán các tích phân, đạo hàm và nghiệm xác định cho spline.
components of the curve provided as arguments to the constructor. The class defines
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
55, allowing the object to be called with the x-axis values, at which the spline should be evaluated, returning the interpolated y-values. This is shown in the example below for the subclass
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
56. The
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
57,
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
58, and
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
59 methods are also available on
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1 objects, allowing definite integrals, derivatives, and roots to be computed for the spline.

Lớp UnivariateSpline cũng có thể được sử dụng để làm mịn dữ liệu bằng cách cung cấp giá trị khác 0 của tham số làm mịn s, với cùng ý nghĩa với từ khóa s của hàm

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
45 được mô tả ở trên. Điều này dẫn đến một đường spline có ít nút thắt hơn số lượng điểm dữ liệu và do đó không còn là một đường spline nội suy nữa mà là một đường spline làm mịn. Nếu điều này không được mong muốn, lớp
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
56 có sẵn. Nó là một phân lớp của
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1 luôn đi qua mọi điểm [tương đương với việc buộc tham số làm mịn bằng 0]. Lớp này được thể hiện trong ví dụ dưới đây

Lớp

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
64 là lớp con khác của lớp
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1. Nó cho phép người dùng chỉ định rõ ràng số lượng và vị trí của các nút bên trong với tham số t. Điều này cho phép tạo các đường spline tùy chỉnh với khoảng cách phi tuyến tính, để nội suy trong một số miền và làm mịn trong các miền khác hoặc thay đổi đặc tính của đường spline

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f = interp1d[x, y]
>>> f2 = interp1d[x, y, kind='cubic']
6

Nội suy Đơn biếnSpline

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
7

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
8

LSQUnivarateSpline với các nút thắt không đồng nhất

>>> xnew = np.linspace[0, 10, num=41, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o', xnew, f[xnew], '-', xnew, f2[xnew], '--']
>>> plt.legend[['data', 'linear', 'cubic'], loc='best']
>>> plt.show[]
9

>>> from scipy.interpolate import interp1d
0

Biểu diễn spline 2-D. Thủ tục [
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
2]#

Đối với khớp nối [mịn] với bề mặt 2 chiều, chức năng

>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
2 khả dụng. Hàm này nhận các đầu vào bắt buộc là các mảng 1-D x, y và z, đại diện cho các điểm trên bề mặt \[z=f\left[x,y\right]. \] Đầu ra mặc định là một danh sách \[\left[tx,ty,c,kx,ky\right]\] whose entries represent respectively, the components of the knot positions, the coefficients of the spline, and the order of the spline in each coordinate. It is convenient to hold this list in a single object, tck, so that it can be passed easily to the function
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
68. The keyword, s , can be used to change the amount of smoothing performed on the data while determining the appropriate spline. The default value is \[s=m-\sqrt{2m}\] , trong đó \[m\ . Kết quả là, nếu không muốn làm mịn, thì is the number of data points in the x, y, and z vectors. As a result, if no smoothing is desired, then \[s=0\] sẽ được chuyển đến
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
2.

Để đánh giá đường spline 2-D và các đạo hàm riêng của nó [theo thứ tự của đường spline], cần có hàm

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
68. Hàm này lấy hai đối số đầu tiên làm hai mảng 1-D có tích chéo chỉ định miền để đánh giá spline. Đối số thứ ba là danh sách tck được trả về từ
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
2. Nếu muốn, đối số thứ tư và thứ năm cung cấp thứ tự của đạo hàm riêng trong \[x\] . direction, respectively.

Điều quan trọng cần lưu ý là không nên sử dụng phép nội suy 2 chiều để tìm biểu diễn spline của hình ảnh. Thuật toán được sử dụng không phù hợp với số lượng lớn các điểm đầu vào. Hộp công cụ xử lý tín hiệu chứa các thuật toán phù hợp hơn để tìm biểu diễn spline của hình ảnh. Các lệnh nội suy 2-D được thiết kế để sử dụng khi nội suy một hàm 2-D như minh họa trong ví dụ sau. Ví dụ này sử dụng lệnh

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
82 trong NumPy, rất hữu ích để xác định “lưới lưới” theo nhiều chiều. [Xem thêm lệnh
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
83 nếu không cần full-mesh]. Số lượng đối số đầu ra và số lượng kích thước của mỗi đối số được xác định bởi số lượng đối tượng lập chỉ mục được truyền trong
>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
82

>>> from scipy.interpolate import interp1d
1

Xác định hàm trên lưới 20x20 thưa thớt

>>> from scipy.interpolate import interp1d
2

>>> from scipy.interpolate import interp1d
3

Hàm nội suy trên lưới 70x70 mới

>>> from scipy.interpolate import interp1d
4

>>> from scipy.interpolate import interp1d
5

Biểu diễn spline 2-D. Hướng đối tượng [
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
3]#

Lớp

>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
3 là lớp tương tự 2-D của lớp
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1. Nó và các lớp con của nó triển khai các hàm FITPACK được mô tả ở trên theo kiểu hướng đối tượng, cho phép khởi tạo các đối tượng có thể được gọi để tính giá trị spline bằng cách chuyển hai tọa độ làm hai đối số

Sử dụng các hàm cơ sở xuyên tâm để làm mịn/nội suy#

Các hàm cơ sở xuyên tâm có thể được sử dụng để làm mịn/nội suy dữ liệu rải rác trong N chiều, nhưng nên thận trọng khi sử dụng để ngoại suy bên ngoài phạm vi dữ liệu được quan sát

Ví dụ 1-D#

Ví dụ này so sánh việc sử dụng các lớp

>>> x = np.linspace[0, 10, num=11, endpoint=True]
>>> y = np.cos[-x**2/9.0]
>>> f1 = interp1d[x, y, kind='nearest']
>>> f2 = interp1d[x, y, kind='previous']
>>> f3 = interp1d[x, y, kind='next']
88 và
>>> xnew = np.linspace[0, 10, num=1001, endpoint=True]
>>> import matplotlib.pyplot as plt
>>> plt.plot[x, y, 'o']
>>> plt.plot[xnew, f1[xnew], '-', xnew, f2[xnew], '--', xnew, f3[xnew], ':']
>>> plt.legend[['data', 'nearest', 'previous', 'next'], loc='best']
>>> plt.show[]
1 từ scipy. mô-đun nội suy

Chủ Đề