Interp1d trong python là gì?

>>> x = [-180, -170, -185, 185, -10, -5, 0, 365] >>> xp = [190, -190, 350, -350] >>> fp = [5 . mảng interp[x, xp, fp, period=360][[7. 5 , 5. , số 8. 75, 6. 25, 3. , 3. 25, 3. 5 , 3. 75]]
def py_sampleProfile[cường độ, Rmin, dR, nxy, dxy, udat, vdat, dRA=0. , dDec=0. , PA=0, inc=0. ]. """ Triển khai Python của sampleProfile. """ inc_cos = np. cos[inc] nrad = len[cường độ] gridrad = np. linspace[Rmin, Rmin + dR * [nrad - 1], nrad] ncol, nrow = nxy, nxy # tạo lưới mắt lưới x = [np. không gian trống [0. 5, -0. 5+1. /float[ncol], ncol]] * dxy * ncol y = [np. không gian trống [0. 5, -0. 5+1. /float[nrow], nrow]] * dxy * nrow # ta thu nhỏ trục x, vì PA là góc Đông Bắc của # mặt phẳng đĩa [trực giao với trục động lượng góc] # PA=0 là a . lưới lưới[x / inc_cos, y] x_meshgrid = np. sqrt[x_axis ** 2. + trục y ** 2. ] # chuyển sang Jansky sr_to_px = dxy**2. cường độ *= sr_to_px f = interp1d[lưới, cường độ, loại='tuyến tính', fill_value=0. , giới hạn_error=Sai, giả sử_sorted=True] intensmap = f[x_meshgrid] intensmap[nrow//2, ncol//2] = central_pixel[cường độ, Rmin, dR, dxy] vis = py_sampleImage[intensmap, dxy, udat, vdat,
---------------------------------------------------------------------------
ValueError                                Traceback [most recent call last]
 in []
----> 1 f[-1.]

~/miniconda3/envs/dev/lib/python3.6/site-packages/scipy/interpolate/polyint.py in __call__[self, x]
     77         """
     78         x, x_shape = self._prepare_x[x]
---> 79         y = self._evaluate[x]
     80         return self._finish_y[y, x_shape]
     81 

~/miniconda3/envs/dev/lib/python3.6/site-packages/scipy/interpolate/interpolate.py in _evaluate[self, x_new]
    608         y_new = self._call[self, x_new]
    609         if not self._extrapolate:
--> 610             below_bounds, above_bounds = self._check_bounds[x_new]
    611             if len[y_new] > 0:
    612                 # Note fill_value must be broadcast up to the proper size

~/miniconda3/envs/dev/lib/python3.6/site-packages/scipy/interpolate/interpolate.py in _check_bounds[self, x_new]
    637         # !! Could provide more information about which values are out of bounds
    638         if self.bounds_error and below_bounds.any[]:
--> 639             raise ValueError["A value in x_new is below the interpolation "
    640                              "range."]
    641         if self.bounds_error and above_bounds.any[]:

ValueError: A value in x_new is below the interpolation range.

Lớp scipy

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1 trong Python dùng để nội suy hàm một chiều. Hàm một chiều lấy một giá trị đầu vào làm tham số và trả về một giá trị đầu ra được phân tích duy nhất

Thông thường, chúng tôi có một loạt các điểm dữ liệu ở các vị trí riêng biệt. Bây giờ, chúng tôi đang cố gắng tính gần đúng hàm có thể tìm giá trị y cho bất kỳ giá trị x đã cho nào giữa các điểm đã cho này

Cú pháp của
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1 để nội suy các điểm dữ liệu

scipy.interpolate.interp1d[x,
          y,
          kind]

Thông số

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
3Dạng mảng. Nó là tập hợp đầu vào của các giá trị được cung cấp cho chức năng.
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
4Dạng mảng. Đó là giá trị đầu vào được xác định dựa trên x.
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
5Đó là một tham số tùy chọn. Nó chỉ định loại nội suy. Theo mặc định, nó được đặt thành
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
6

Trở về

Nó trả về một chức năng

Mã ví dụ.
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
7 Giữa các điểm dữ liệu bằng cách sử dụng
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]

đầu ra

Ở đây, chúng tôi cố gắng nội suy hoặc tạo một hàm xấp xỉ mối quan hệ giữa

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1 và
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
2. Trong đoạn mã trên,
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1 và
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
2 được lấy ngẫu nhiên. Sau đó, các giá trị được chuyển dưới dạng đối số vào hàm
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
5, hàm này xác định hàm nội suy. Bây giờ chúng ta có thể tìm bất kỳ
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
2 nào cho bất kỳ
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1 nào trong phạm vi của
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
8

Cuối cùng, để hình dung hàm nội suy trông như thế nào, chúng ta lấy các điểm

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
9 giữa
scipy.interpolate.interp1d[x,
          y,
          kind]
20 và
scipy.interpolate.interp1d[x,
          y,
          kind]
21 và vẽ đường cong của hàm được biểu thị bằng đường cong màu đen trong hình

Vì chúng tôi chưa đặt loại đường cong mà chúng tôi muốn nội suy, nên theo mặc định, phương thức

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
5 hiển thị cho chúng tôi một đường thẳng tuyến tính giữa các điểm

Mã ví dụ. Đặt Tham số
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
5 trong Phương thức
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
1

scipy.interpolate.interp1d[x,
          y,
          kind]
2

đầu ra

Biểu đồ trên cho thấy các hàm nội suy xấp xỉ bằng cách sử dụng các kỹ thuật

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
6 và
scipy.interpolate.interp1d[x,
          y,
          kind]
26. Dòng
scipy.interpolate.interp1d[x,
          y,
          kind]
27 trong biểu đồ biểu thị đường được nội suy bằng phương pháp
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
6 và dòng
scipy.interpolate.interp1d[x,
          y,
          kind]
29 trong biểu đồ biểu thị đường được nội suy bằng phương pháp
scipy.interpolate.interp1d[x,
          y,
          kind]
26

Do đó, để tóm tắt, lớp

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

x_value = np.array[[0,1,2,4]]
y_value = np.array[[2,3,12,147]]

function = scipy.interpolate.interp1d[x_value, y_value]
x_new = np.linspace[0, 4, 10]

plt.scatter[x_value, y_value, color = 'blue']
plt.plot[x_new, function[x_new], color = 'black']
plt.xlabel["X-Values"]
plt.ylabel["Y-Values"]
plt.title["1d Interpolation using scipy interp1d method"]
plt.show[]
5 được sử dụng để tính toán một hàm bằng cách sử dụng các điểm dữ liệu được cung cấp và có thể được tính toán mọi lúc, mọi nơi được chỉ định trong miền đã cho bằng phép nội suy tuyến tính

Nội suy trong Python là gì?

Nội suy là một kỹ thuật trong Python được sử dụng để ước tính các điểm dữ liệu chưa biết giữa hai điểm dữ liệu đã biết. Nội suy chủ yếu được sử dụng để gán các giá trị bị thiếu trong khung dữ liệu hoặc chuỗi trong khi xử lý trước dữ liệu

Là nội suy tuyến tính interp1d?

Nội suy tuyến tính một chiều để tăng đơn điệu các điểm mẫu. Trả về phép nội suy tuyến tính từng phần một chiều cho một hàm với các điểm dữ liệu riêng biệt đã cho [xp, fp], được đánh giá tại x. Tọa độ x để đánh giá các giá trị được nội suy.

SciPy nội suy trong Python là gì?

Thẻ scipy. nội suy là một mô-đun trong Python SciPy bao gồm các lớp, hàm spline và các lớp nội suy đơn biến và đa biến . Nội suy được thực hiện theo nhiều cách, một số trong số đó là. Nội suy 1-D. nội suy Spline.

Phạm vi nội suy là gì?

Trong lĩnh vực toán học của phân tích số, phép nội suy là một loại ước tính, một phương pháp xây dựng [tìm kiếm] các điểm dữ liệu mới dựa trên phạm vi của một tập hợp dữ liệu đã biết . .

Chủ Đề