我想插入(x, y)
以下数据的多个用户输入:
| >=0 1 2 3 4 5 >=6
-------------------------------------------
>=09 <10 | 6.4 5.60 4.8 4.15 3.5 2.85 2.2
>=10 <11 | 5.3 4.50 3.7 3.05 2.4 1.75 1.1
>=11 <12 | 4.7 3.85 3.0 2.35 1.7 1.05 0.4
>=12 | 4.2 3.40 2.6 1.95 1.3 0.65 0.0
如果用户输入x = 2.5
and y = 9
,模型应该返回4.475
。另一方面,如果用户输入x = 2.5
and y = 9.5
,模型应该返回3.925
。
我使用map_coordinates
它,因为它提供了将坐标映射到 x,y 范围的能力
这是我到目前为止所做的:
import numpy as np
from scipy.ndimage import map_coordinates
# define array
z = np.array([[6.4, 5.60, 4.8, 4.15, 3.5, 2.85, 2.2],
[5.3, 4.50, 3.7, 3.05, 2.4, 1.75, 1.1],
[4.7, 3.85, 3.0, 2.35, 1.7, 1.05, 0.4],
[4.2, 3.40, 2.6, 1.95, 1.3, 0.65, 0.0]])
# function to interpolate
def twoD_interpolate(arr, xmin, xmax, ymin, ymax, x1, y1):
"""
interpolate in two dimensions with "hard edges"
"""
nx, ny = arr.shape
x1 = np.array([x1], dtype=np.float)
y1 = np.array([y1], dtype=np.float)
# if x1 is out of bounds set its value to its closest point, so that we're always
# interpolating within the range
x1[x1 > xmax] = xmax
x1[x1 < xmin] = xmin
# if y1 is out of bounds set its value to its closest point, so that we're always
# interpolating within the range
y1[y1 > ymax] = ymax
y1[y1 < ymin] = ymin
# convert x1 and y1 to indices so we can map over them
x1 = (nx - 1) * (x1 - xmin) / (xmax - xmin)
y1 = (ny - 2) * (y1 - ymin) / (ymax - ymin)
y1[y1 > 1] = 2.0
return map_coordinates(arr, [y1, x1])
# function to get the value
def test_val(x, y, arr):
return twoD_interpolate(arr, 0, 6, 9, 12, x, y)
测试代码
print test_val(4, 11, z) --> 3.00
print test_val(2, 10, z) --> 3.85
这些结果是不正确的,因为它们应该1.7
分别3.7
返回
关于我做错了什么的任何想法或想法?