Uys*_*des 2 python arrays numpy scipy
我希望通过 numpy 创建一个数组,该数组根据给定数组中的值从一个区间到另一个区间生成一个等距的值。
我明白有:
np.linspace(min, max, num_elements)
Run Code Online (Sandbox Code Playgroud)
但我正在寻找的是想象你有一组价值观:
arr = np.array([1, 2, 4, 6, 7, 8, 12, 10])
Run Code Online (Sandbox Code Playgroud)
当我做:
#some numpy function
arr = np.somefunction(arr, 16)
>>>arr
>>> array([1, 1.12, 2, 2.5, 4, 4.5, etc...)]
# new array with 16 elements including all the numbers from previous
# array with generated numbers to 'evenly space them out'
Run Code Online (Sandbox Code Playgroud)
因此,我正在寻找与 linspace() 相同的功能,但采用数组中的所有元素并创建另一个包含所需元素但与数组中的设置值间隔均匀的数组。我希望我能清楚地说明这一点..
我试图用这个设置实际做的是获取现有的 x,y 数据并扩展数据以在某种意义上拥有更多的“控制点”,这样我就可以从长远来看进行计算。
先感谢您。
xp = np.arange(len(arr)) # X coordinates of arr
targets = np.arange(0, len(arr)-0.5, 0.5) # X coordinates desired
np.interp(targets, xp, arr)
Run Code Online (Sandbox Code Playgroud)
上面对 8 个数据点以 0.5 的间距进行简单的线性插值,总共 15 个点(因为栅栏贴):
array([ 1. , 1.5, 2. , 3. , 4. , 5. , 6. , 6.5, 7. ,
7.5, 8. , 10. , 12. , 11. , 10. ])
Run Code Online (Sandbox Code Playgroud)
您可以使用一些其他选项numpy.interp来调整行为。targets如果需要,您还可以以不同的方式生成。