Nội suy tuyến tính là kỹ thuật xác định giá trị hàm của bất kỳ điểm trung gian nào khi biết giá trị của hai điểm liền kề. Nội suy tuyến tính về cơ bản là ước tính của một giá trị chưa biết nằm trong hai giá trị đã biết. Nội suy tuyến tính được sử dụng trong các lĩnh vực khác nhau như thống kê, kinh tế, xác định giá, v.v. Nó được sử dụng để lấp đầy các khoảng trống trong dữ liệu thống kê vì mục đích liên tục của thông tin.
Bằng cách sử dụng công thức sau, chúng ta có thể nội suy tuyến tính điểm dữ liệu đã cho
Ở đây [x1, y1] là tọa độ của điểm dữ liệu đầu tiên. Và [x2,y2] là tọa độ của điểm dữ liệu thứ hai, trong đó x là điểm mà chúng tôi thực hiện phép nội suy và y là giá trị được nội suy
Vấn đề ví dụ
Hãy lấy một ví dụ để hiểu rõ hơn. Chúng tôi có các giá trị dữ liệu sau trong đó x biểu thị số và y là hàm của căn bậc hai của x. Nhiệm vụ của chúng ta là tìm căn bậc hai của 5. 5 [x]
x
1
2
3
4
5
6
y [ f[x] = √x ]
1
1. 4142
1. 7320
2
2. 2360
2. 4494
Chúng ta có thể sử dụng phương pháp Nội suy tuyến tính tại đây
1. Tìm hai số liền kề [x1, y1] ,[x2,y2] từ x. tôi. e. [5,2. 2360] và [6,2. 4494]
Trong đó x1 = 5, x2= 6, y1 = 2. 2360, y2 = 2. 4494 và chúng tôi nội suy tại điểm x = 5. 5
2. Sử dụng công thức y[x] = y1 + [x – x1] \frac{[y2 – y1] }{ [x2 – x1]}
3. Sau khi đặt các giá trị trong phương trình trên.
y = 2.3427
Tại x = 5. 5 giá trị của Y sẽ là 2. 3427. Vì vậy, bằng cách sử dụng phép nội suy tuyến tính, chúng ta có thể dễ dàng xác định giá trị của hàm giữa hai khoảng
Cách tiếp cận 1
Sử dụng công thức
Ví dụ. Giả sử chúng ta có một bộ dữ liệu về dân số của một thành phố và năm
X[Năm]
2016
2017
2018
2019
2021
Y[Dân số]
10001
12345
74851
12124
5700
Ở đây, X là năm và Y là dân số của bất kỳ thành phố nào. Nhiệm vụ của chúng tôi là tìm dân số của thành phố vào năm 2020
Chúng tôi chọn [x1, y1] ,[x2,y2] là x1=2019 , y1=12124, x2=2021, y2=5700, x = 2020, y = ?
Ở đây [x1, y1] và [x2, y2] là hai điểm kề nhau và x là năm mà chúng ta muốn dự đoán giá trị của dân số y
Python3
Population on year 2020 is 8912.02
Population on year 2020 is 8912.03
Population on year 2020 is 8912.04
Population on year 2020 is 8912.05
Population on year 2020 is 8912.06
Population on year 2020 is 8912.07
Population on year 2020 is 8912.08
Population on year 2020 is 8912.00
Population on year 2020 is 8912.01
Population on year 2020 is 8912.02
Population on year 2020 is 8912.03
Population on year 2020 is 8912.04
Population on year 2020 is 8912.05
Population on year 2020 is 8912.06
Population on year 2020 is 8912.07
Population on year 2020 is 8912.08
Population on year 2020 is 8912.09
Population on year 2020 is 8912.02
Population on year 2020 is 8912.03
Population on year 2020 is 8912.04
Population on year 2020 is 8912.03
Population on year 2020 is 8912.054
Population on year 2020 is 8912.055
Population on year 2020 is 8912.056
Population on year 2020 is 8912.05
Population on year 2020 is 8912.04
Population on year 2020 is 8912.05
Population on year 2020 is 8912.06
Population on year 2020 is 8912.09
Population on year 2020 is 8912.02
Population on year 2020 is 8912.03
Population on year 2020 is 8912.04
Population on year 2020 is 8912.05
Population on year 2020 is 8912.054
Population on year 2020 is 8912.027
Population on year 2020 is 8912.028
Population on year 2020 is 8912.05
Population on year 2020 is 8912.04
Population on year 2020 is 8912.03
Population on year 2020 is 8912.06
Population on year 2020 is 8912.09
Population on year 2020 is 8912.02
Population on year 2020 is 8912.03
Population on year 2020 is 8912.04
Population on year 2020 is 8912.03
Population on year 2020 is 8912.038
Population on year 2020 is 8912.08
Population on year 2020 is 8912.040
Population on year 2020 is 8912.00
Population on year 2020 is 8912.042
Population on year 2020 is 8912.043
Population on year 2020 is 8912.01____545
Population on year 2020 is 8912.046
Population on year 2020 is 8912.047
Population on year 2020 is 8912.048
Population on year 2020 is 8912.049
Population on year 2020 is 8912.050
Population on year 2020 is 8912.047
Population on year 2020 is 8912.052
Population on year 2020 is 8912.053
_______554____11____556
Population on year 2020 is 8912.057
Population on year 2020 is 8912.058
Population on year 2020 is 8912.059____560
Population on year 2020 is 8912.061
Population on year 2020 is 8912.062
Population on year 2020 is 8912.063
_______564____565
Đầu ra
Population on year 2020 is 8912.0
Cách tiếp cận 2
sử dụng scipy. nội suy. interp1d
Tương tự, chúng ta có thể đạt được phép nội suy tuyến tính bằng cách sử dụng hàm thư viện scipy có tên là nội suy. interp1d
cú pháp. scipy. nội suy. interp1d[x, y, kind=’linear’, axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False]
Sr. không.
Thông số
Sự miêu tả
1
x
Mảng 1-D của các giá trị thực
2
y
Một mảng N-D của các giá trị thực
3
Tốt bụng
i. e. loại nội suy mà bạn muốn nó có thể là 'tuyến tính', 'gần nhất', 'gần nhất', 'không', 'tuyến tính', 'bậc hai', 'khối', 'trước' hoặc 'tiếp theo'. 'không', 'tuyến tính', 'bậc hai' và 'khối', theo mặc định, nó là tuyến tính
4
trục
Chỉ định trục của y dọc theo mà chúng tôi nội suy
5
sao chép
Nó giữ các giá trị boolean nếu True, lớp tạo các bản sao bên trong của x và y
6
giới hạn_error
Nó chứa các giá trị boolean Nếu True, ValueError được nâng lên khi nội suy được thực hiện trên một giá trị nằm ngoài phạm vi của x
Ví dụ
Hãy có một tập dữ liệu ngẫu nhiên
X = [1,2,3,4,5], Y = [11,2. 2,3. 5,-88,1] và chúng tôi muốn tìm giá trị của Y tại điểm 2. 5
Python3
Population on year 2020 is 8912.066
Population on year 2020 is 8912.067
Population on year 2020 is 8912.068
Population on year 2020 is 8912.069____570
Population on year 2020 is 8912.071
Population on year 2020 is 8912.072
Population on year 2020 is 8912.01
Population on year 2020 is 8912.074
Population on year 2020 is 8912.05_______547____577
Population on year 2020 is 8912.047
Population on year 2020 is 8912.079
Population on year 2020 is 8912.047
Population on year 2020 is 8912.081
Population on year 2020 is 8912.047
Population on year 2020 is 8912.083
Population on year 2020 is 8912.06
Population on year 2020 is 8912.085
Population on year 2020 is 8912.086
Population on year 2020 is 8912.01
Population on year 2020 is 8912.074
Population on year 2020 is 8912.089
Population on year 2020 is 8912.047
Population on year 2020 is 8912.001
Population on year 2020 is 8912.047
Population on year 2020 is 8912.003
Population on year 2020 is 8912.047
Population on year 2020 is 8912.09
Population on year 2020 is 8912.006
Population on year 2020 is 8912.047
Population on year 2020 is 8912.05
Population on year 2020 is 8912.06
Population on year 2020 is 8912.010