擅长:python、mysql、java
<p>使用scipy.ndimage可能有更好的方法,但下面是使用scipy.interpolate.interp1d执行此操作的方法:</p>
<pre><code>import numpy as np
import scipy.interpolate as spi
# A B C Y
my_arr = np.array([ [.20, .54, .26], # 0
[.22, .54, .24], # 1
[.19, .56, .25], # 2
[.19, .58, .23], # 3
[.17, .62, .21] ])
print(my_arr)
Y=np.arange(len(my_arr))
interp_funcs=[spi.interp1d(Y,my_arr[:,col]) for col in range(3)]
y=np.array([2.5,0.2,1.5,4.0,8.7])
y[y < 0] = 0
y[y > 4] = 4
print(np.vstack(f(y) for f in interp_funcs))
# [[ 0.19 0.204 0.205 0.17 0.17 ]
# [ 0.57 0.54 0.55 0.62 0.62 ]
# [ 0.24 0.256 0.245 0.21 0.21 ]]
</code></pre>