Hướng dẫn cubic spline extrapolation python - trăn ngoại suy khối spline

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

Nội phân Chính showShow

  • Nội suy 1-D (Interp1d)#
  • Nội suy dữ liệu đa biến (GridData)#
  • Nội suy dữ liệu đa biến trên lưới thông thường (thông thường
  • Nội suy spline#
  • Nội suy spline trong 1-d: thủ tục (nội suy.splxxx)#
  • Nội suy spline trong 1-D: hướng đối tượng (Univariatespline)#
  • Biểu diễn spline 2-D: Thủ tục (Bisplrep)#
  • Biểu diễn spline 2-D: hướng đối tượng (bivariatespline)#
  • Sử dụng các chức năng cơ sở xuyên tâm để làm mịn/nội suy#
  • Ví dụ 1-D#
  • Ví dụ 2-D#

  • 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')
    
    6) trong 1-D, cung cấp một số phương pháp nội suy.

  • Hàm thuận tiệ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')
    
    7 cung cấp một giao diện đơn giản để nội suy theo kích thước N (n = 1, 2, 3, 4, Hồi). Giao diện hướng đối tượng cho các thói quen cơ bản cũng có sẵ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')
    
    8 cung cấp một số phương pháp nội suy trên lưới thông thường theo kích thước tùy ý (n),

  • Các chức năng cho phép nội suy hình khối 1- và 2-D (được làm mịn), dựa trên thư viện Fortran FitPack. Chúng là cả hai giao diện theo thủ tục và hướng đối tượng cho thư viện FitPack.

  • Nội suy sử dụng các chức năng cơ sở xuyên tâm.

Nội suy 1-D (Interp1d)#

Nội suy dữ liệu đa biến (GridData)#

>>> 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')
>>> 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()

Nội suy dữ liệu đa biến trên lưới thông thường (thông thường

>>> 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 (GridData)#

Nội suy dữ liệu đa biến trên lưới thông thường (thông thường

Nội suy spline#

>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2

Nội suy spline trong 1-d: thủ tục (nội suy.splxxx)#

>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]

Nội suy spline trong 1-D: hướng đối tượng (Univariatespline)#

>>> rng = np.random.default_rng()
>>> points = rng.random((1000, 2))
>>> values = func(points[:,0], points[:,1])

Biểu diễn spline 2-D: Thủ tục (Bisplrep)#

>>> from scipy.interpolate import griddata
>>> grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
>>> grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
>>> grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')

Biểu diễn spline 2-D: hướng đối tượng (bivariatespline)#

>>> 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 (thông thường

Nội suy 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')
1

Nội suy spline trong 1-d: thủ tục (nội suy.splxxx)#

>>> 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

Nội suy spline trong 1-D: hướng đối tượng (Univariatespline)#

>>> 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

Biểu diễn spline 2-D: Thủ tục (Bisplrep)#

>>> 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

Biểu diễn spline 2-D: hướng đối tượng (bivariatespline)#

>>> 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

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

Nội suy spline#

Nội suy spline trong 1-d: thủ tục (nội suy.splxxx)#

Nội suy spline trong 1-D: hướng đối tượng (Univariatespline)#\(x\) and \(y\) components of the curve. The normal output is a 3-tuple, \(\left(t,c,k\right)\) , containing the knot-points, \(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

>>> 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()
5 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 các mảng \ (n \)-đại diện cho đường cong trong không gian N-d. Độ dài của mỗi mảng là số lượng các đ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ó khoảng cách bằng nhau giữa \ (0 \) và \ (1 \). Đầu ra mặc định bao gồm hai đối tượng: 3-tuple, \ (\ left (t, c, k \ right) \), chứa biểu diễn spline và biến tham số \ (u.\(N\)-arrays representing the curve in N-D space. The length of each array is the number of curve points, and each array provides one component of the N-D data point. The parameter variable is given with the keyword argument, u,, which defaults to an equally-spaced monotonic sequence between \(0\) and \(1\) . The default output consists of two objects: a 3-tuple, \(\left(t,c,k\right)\) , containing the spline representation and the parameter variable \(u.\)

Đối số từ khóa, s, được sử dụng để chỉ định lượng làm mịn để thực hiện trong spline Fit. Giá trị mặc định của \ (s \) là \ (s = m- \ sqrt {2m} \) trong đó \ (m \) là số lượng điểm dữ liệu phù hợp. Do đó, nếu không có sự làm mịn nào được mong muốn, giá trị của \ (\ mathbf {s} = 0 \) nên được chuyển đến các thói quen.\(s\) is \(s=m-\sqrt{2m}\) where \(m\) is the number of data-points being fit. Therefore, if no smoothing is desired a value of \(\mathbf{s}=0\) should be passed to the routines.

Khi biểu diễn spline của dữ liệu đã được xác định, các chức năng có sẵn để đánh giá spline (

>>> 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()
6) và các dẫn xuất của 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()
6,
>>> 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()
8) tại bất kỳ điểm nào và tích phân của spline giữa bất kỳ hai điểm nào (
>>> 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()
9). Ngoài ra, đối với các spline khối (\ (k = 3 \)) với 8 nút thắt trở lên, rễ của spline có thể được ước tính (
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
0). Các chức năng này được thể hiện trong ví dụ sau.\(k=3\) ) with 8 or more knots, the roots of the spline can be estimated (
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
0). These functions are demonstrated in the example that follows.

>>> 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

Cubic-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

Phái sinh 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 hợp 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

>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
0 có thể không tìm thấy một giải pháp rõ ràng ở rìa của khoảng thời gian gần đúng, \ (x = 0 \). Nếu chúng ta xác định spline trên một khoảng thời gian lớn hơn một chút, chúng ta sẽ khôi phục cả hai gốc \ (x = 0 \) và \ (x = 2 \ pi \):\(x = 0\). If we define the spline on a slightly larger interval, we recover both roots \(x = 0\) and \(x = 2\pi\):

>>> 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 (Univariatespline)#

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

>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2 và được tạo bằng các thành phần \ (x \) và \ (y \) của đường cong được cung cấp dưới dạng đối số cho hàm tạo. Lớp xác định
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
3, cho phép đối tượng được gọi với các giá trị trục x, tại đó nên đánh giá spline, trả về các giá trị Y được nội suy. Điều này được hiển thị trong ví dụ dưới đây cho lớp con
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
4. Các phương pháp
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
5,
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
6 và
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
7 cũng có sẵn trên các đối tượng
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2, cho phép các tích phân, dẫn xuất và rễ xác định được tính toán cho spline.\(x\) and \(y\) components of the curve provided as arguments to the constructor. The class defines
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
3, 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
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
4. The
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
5,
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
6, and
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
7 methods are also available on
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2 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 không của tham số làm mịn, với cùng nghĩa với từ khóa S của hàm

>>> 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()
4 được mô tả ở trên. Điều này dẫn đến một 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 spline nội suy, mà là một spline làm mịn. Nếu điều này không mong muốn, lớp
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
4 có sẵn. Đó là một lớp con của
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2 luôn đi qua tất cả các điểm (tương đương với việc buộc tham số làm mịn lên 0). Lớp này được thể hiện trong ví dụ dưới đây.

Lớp

>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
2 là lớp con khác của
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2. Nó cho phép người dùng chỉ định số và vị trí của các nút bên trong một cách rõ ràng với tham số t. Điều này cho phép tạo ra các 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à mịn ở các miền khác, hoặc thay đổi đặc tính 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')
6

Nội suyNivariatespline

>>> 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 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 (Bisplrep)#

Đối với (mịn) phù hợp với bề mặt 2 chiều, hàm

>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
4 có sẵn. Hàm này mất theo yêu cầu đầu vào các mảng 1-d x, y và z, biểu thị các điểm trên bề mặt \ (z = f \ trái (x, y \ reight). \) Đầu ra mặc định là danh sách \ (\ bên trái [TX, TY, C, KX, KY \ RIGHT] \) có các mục tương ứng, các thành phần của các vị trí nút, các hệ số của spline và thứ tự của spline trong mỗi tọa độ. Thật thuận tiện khi giữ danh sách này trong một đối tượng duy nhất, TCK, để nó có thể được chuyển dễ dàng cho hàm
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
5. Từ khóa, s, có thể được sử dụng để thay đổi lượng làm mịn được thực hiện trên dữ liệu trong khi xác định spline thích hợp. Giá trị mặc định là \ (s = m- \ sqrt {2m} \), trong đó \ (m \) là số lượng điểm dữ liệu trong các vectơ x, y và z. Kết quả là, nếu không mong muốn làm mịn, thì \ (s = 0 \) nên được truyền đến
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
4.1-D arrays x, y, and z, which represent points on the surface \(z=f\left(x,y\right).\) The default output is a list \(\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
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
5. 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}\), where \(m\) is the number of data points in the x, y, and z vectors. As a result, if no smoothing is desired, then \(s=0\) should be passed to
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
4.

Để đánh giá spline 2-D và các dẫn xuất một phần của nó (theo thứ tự của spline), hàm

>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
5 là bắt buộc. Hàm này lấy hai đối số đầu tiên hai mảng 1-D có sản phẩm chéo chỉ định tên miền để đánh giá spline. Đối số thứ ba là danh sách TCK được trả về từ
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
4. Nếu muốn, các đối số thứ tư và thứ năm cung cấp các đơn đặt hàng của đạo hàm từng phần trong hướng \ (x \) và \ (y \), tương ứng.two 1-D arrays whose cross-product specifies the domain over which to evaluate the spline. The third argument is the tck list returned from
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
4. If desired, the fourth and fifth arguments provide the orders of the partial derivative in the \(x\) and \(y\) 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-D để tìm biểu diễn spline của hình ảnh. Thuật toán được sử dụng không thể chấp nhận được 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 dự định để sử dụng khi nội suy hàm 2 chiều như được hiển thị trong ví dụ sau. Ví dụ này sử dụng lệnh

>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
9 trong Numpy, rất hữu ích cho việc xác định lưới lưới lưới trên nhiều chiều. (Xem thêm lệnh
>>> rng = np.random.default_rng()
>>> points = rng.random((1000, 2))
>>> values = func(points[:,0], points[:,1])
0 nếu không cần lưới đầy đủ). 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
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
9.

>>> from scipy.interpolate import interp1d
1

Xác định chức năng trên lưới 20x20 thưa thớt

>>> from scipy.interpolate import interp1d
2
>>> from scipy.interpolate import interp1d
3

Chức năng 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 (bivariatespline)#

Lớp

>>> rng = np.random.default_rng()
>>> points = rng.random((1000, 2))
>>> values = func(points[:,0], points[:,1])
2 là tương tự 2 chiều của lớp
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2. Nó và các lớp con của nó thực hiện các hàm FITPack được mô tả ở trên theo kiểu hướng đối tượng, cho phép các đối tượng được khởi tạo có thể được gọi để tính toán giá trị spline bằng cách chuyển trong hai tọa độ dưới dạng hai đối số.

Sử dụng các chức năng 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 phân tán theo kích thước N, nhưng nên được sử dụng thận trọ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

>>> rng = np.random.default_rng()
>>> points = rng.random((1000, 2))
>>> values = func(points[:,0], points[:,1])
4 và
>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
2 từ mô -đun scipy.Interpolate.

>>> from scipy.interpolate import interp1d
6
>>> from scipy.interpolate import interp1d
7
>>> from scipy.interpolate import interp1d
8
>>> from scipy.interpolate import interp1d
9
>>> 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')
0
>>> 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')
1

Ví dụ 2-D#

Ví dụ này cho thấy cách nội suy dữ liệu 2-D phân tá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')
2
>>> 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')
3
>>> 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
>>> 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