T3 23/11 09. 41. 11 EST 2010
Thanks Josef, that got me pointed in the right direction. My solution: x = np.array[[1.0,1.0,1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,2.0,2.0,1.0,1.0,1.0,6.0]] xtmp = x.copy[] xtmp[1:] = x[1:] - x[:-1] x_idx = np.nonzero[xtmp][0] x_vals = x[x_idx] np.interp[np.arange[len[x]],x_idx, x_vals] array[[ 1. , 1.33333333, 1.66666667, 2. , 2.5 , 3. , 3.33333333, 3.66666667, 4. , 3. , 2. , 1.5 , 1. , 2.66666667, 4.33333333, 6. ]] - d >>> 11/22/2010 4:04 PM >>> On Mon, Nov 22, 2010 at 4:53 PM, Dharhas Pothina wrote: > Hi, > > A while back I asked a question about interpolating between duplicate point for a monotonic array. I was able to get that working with the code below. > > x = np.array[[1.0,1.0,1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,5.0,5.0,5.0,5.0,6.0,6.0]] > xUnique, xUniqueIndices = np.unique[x, return_index=True] > idx = np.argsort[xUniqueIndices] > np.interp[np.arange[len[x]], xUniqueIndices[idx], xUnique[idx]] > > gives : > > np.array[[ 1. , 1.33333333, 1.66666667, 2. , 2.5 , > 3. , 3.33333333, 3.66666667, 4. , 4.5 , > 5. , 5.25 , 5.5 , 5.75 , 6. , 6. ]] > > now I need to do something similar for a non-monotonic array: i.e > > x = np.array[[ 1., 1., 1., 2., 2., 3., 3., 3., 4., 4., 2., 2., 1., 1., 1., 6.]] > > newx = np.array[[1., 1.33, 1.67, 2., 2.5, 3., 3.33, 3.67, 4., 3., 2., 3., 4., 5., 6.]] > > I've tried a few things but nothing has worked yet. as a corrolary question: is there a way to identify the indices on the array x that are the start of adjacent duplicate elements i.e a version np.unique that only uniques adjacent values. just the last question [np.diff[x] != 0] np.nonzero[np.diff[x]] or some variation on it Josef > > - dharhas > > _______________________________________________ > SciPy-User mailing list > SciPy-User at scipy.org > //mail.scipy.org/mailman/listinfo/scipy-user > _______________________________________________ SciPy-User mailing list SciPy-User at scipy.org //mail.scipy.org/mailman/listinfo/scipy-userThông tin thêm về danh sách gửi thư của Người dùng SciPy
Hàm NumPy interp[] trong Python còn được gọi là phép nội suy trả về phép nội suy tuyến tính từng phần một chiều cho một hàm có các điểm dữ liệu rời rạc đã cho [xp, fp]. Đó là phép nội suy tuyến tính một chiều cho các điểm mẫu tăng đơn điệu
Trong bài viết này, tôi sẽ giải thích cách sử dụng hàm
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
3 và cách lấy các giá trị nội suy của mảng NumPy1. Ví dụ nhanh về hàm nội suy Python NumPy
Nếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách sử dụng hàm nội suy NumPy interp[] trong Python
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
2. Cú pháp của NumPy interp[]
Sau đây là cú pháp của hàm
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
3
# Syntax of numpy.interp[]
numpy.interp[x, xp, fp, left=None, right=None, period=None]
2. 1 Các tham số của interp[]
Hàm
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
3 cho phép các tham số sau
6 – giống như mảng. Tọa độ x để đánh giá các giá trị được nội suy# Below are the quick examples # Example 1: Use numpy.interp[] function x = 5.8 xp = [3, 6, 8] fp = [2, 5, 7] arr2 = np.interp[x, xp, fp] # Example 2: Get the interpolate values on graphical representation arr = np.linspace[0, 2*np.pi, 12] arr1 = np.sin[arr] xvals = np.linspace[0, 2*np.pi, 60] yinterp = np.interp[xvals, arr, arr1] plt.plot[arr, arr1, 'o'] plt.plot[xvals, yinterp, '-x'] plt.show[] # Example 3: Get interpolation with periodic x-coordinates x = [-120, -150, -135, 195, -30, -9, 5, 345] xp = [180, -170, 250, -460] fp = [8, 14, 5, 9] arr2 = np.interp[x, xp, fp, period=360] # Example 4: Use interp[] function to numpy complex interpolation x = [2.7, 6.0] xp = [3, 5, 7] fp = [2.0j, 0, 3+6j] arr2 = np.interp[x, xp, fp]
0– Tọa độ x của các điểm dữ liệu, phải tăng nếu khoảng thời gian đối số không được chỉ định. Mặt khác, xp được sắp xếp bên trong sau khi chuẩn hóa các ranh giới định kỳ với chu kỳ xp = xp %# Syntax of numpy.interp[] numpy.interp[x, xp, fp, left=None, right=None, period=None]
1 – Tọa độ y của các điểm dữ liệu, cùng độ dài với xp# Syntax of numpy.interp[] numpy.interp[x, xp, fp, left=None, right=None, period=None]
2 – Tùy chọn float hoặc complex tương ứng với fp. bên trái là giá trị trả về cho x < xp[0], theo mặc định là các giá trị tùy chọn và fp[0]# Syntax of numpy.interp[] numpy.interp[x, xp, fp, left=None, right=None, period=None]
3 – Tùy chọn float hoặc complex tương ứng với fp. bên phải là giá trị trả về cho x > xp[-1], theo mặc định là giá trị tùy chọn và fp[-1]# Syntax of numpy.interp[] numpy.interp[x, xp, fp, left=None, right=None, period=None]
4– Không có hoặc thả nổi, tùy chọn. khoảng thời gian là khoảng thời gian cho tọa độ x. Nếu nó được đưa ra, trái và phải sẽ bị bỏ qua nếu khoảng thời gian được chỉ định. Đây cũng là tùy chọn# Syntax of numpy.interp[] numpy.interp[x, xp, fp, left=None, right=None, period=None]
2. 2 Giá trị trả về của interp[]
Nó trả về các giá trị nội suy của kiểu float, cùng hình dạng với x. Tăng ValueError nếu xp và fp có độ dài khác nhau. Nếu xp hoặc fp không phải là chuỗi 1-D Nếu dấu chấm == 0
3. sử dụng numpy. hàm interp[]
Hãy sử dụng NumPy interp[] [nội suy] của mảng bằng cách sử dụng tọa độ x và y
________số 84. Nhận các giá trị nội suy trên biểu diễn đồ họa
Chúng ta cũng có thể sử dụng hàm NumPy interp[] để biểu diễn đồ họa bằng thư viện MatLab. Vẽ một hàm nội suy cho hàm sin
import numpy as np
import matplotlib.pyplot as plt
# Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
Sản lượng dưới sản lượng
Bạn có thể xem đồ thị Parabolic của phương pháp
# Syntax of numpy.interp[]
numpy.interp[x, xp, fp, left=None, right=None, period=None]
5 trong Numpy5. Nhận nội suy với tọa độ x định kỳ
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
16. Sử dụng hàm interp[] để nội suy phức hợp NumPy
Chúng ta cũng có thể lấy giá trị Nội suy của các phần tử phức tạp của một mảng. Ví dụ: trước khi đoạn trích sử dụng loại phức tạp
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
2Sự kết luận
Trong bài viết này, tôi đã giải thích cách sử dụng hàm
# Below are the quick examples
# Example 1: Use numpy.interp[] function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp[x, xp, fp]
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace[0, 2*np.pi, 12]
arr1 = np.sin[arr]
xvals = np.linspace[0, 2*np.pi, 60]
yinterp = np.interp[xvals, arr, arr1]
plt.plot[arr, arr1, 'o']
plt.plot[xvals, yinterp, '-x']
plt.show[]
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp[x, xp, fp, period=360]
# Example 4: Use interp[] function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp[x, xp, fp]
3 để lấy các giá trị nội suy, cùng hình dạng với x với các ví dụ