Python 散射数据-缩放x轴以完成范围并插值剩余的y数据点

Python 散射数据-缩放x轴以完成范围并插值剩余的y数据点,python,Python,我有XY散点数据,随机X为整数。我希望输出数据具有偶数X分布,并对缺失的y进行插值 我将创建一个范围为x的数组并填充它。然后运行循环以填充新数组中的Y,然后对剩余的Y进行插值 但对我来说,这似乎效率太低了。有更好的方法吗? 我对python相当陌生,不知道像numpy这样的模块是否内置了高效的解决方案 澄清: 排序后,我有一组这样的分散数据 [[1 , 0.1], [3 , 0.2], [5 , 0.4]] 需要这样的数组吗 [[1 , 0.1], [2 , 0.15],

我有XY散点数据,随机X为整数。我希望输出数据具有偶数X分布,并对缺失的y进行插值

我将创建一个范围为x的数组并填充它。然后运行循环以填充新数组中的Y,然后对剩余的Y进行插值

但对我来说,这似乎效率太低了。有更好的方法吗? 我对python相当陌生,不知道像numpy这样的模块是否内置了高效的解决方案

澄清:
排序后,我有一组这样的分散数据

[[1 , 0.1],  
 [3 , 0.2],  
 [5 , 0.4]]
需要这样的数组吗

[[1 , 0.1],  
 [2 , 0.15],  
 [3 , 0.2],
 [4 , 0.3],  
 [5 , 0.4]]
你可以用

举个简单的例子:

import numpy as np

x = np.array([0,1,2,4])
y = np.array([0.1,0.2,0.3,0.6])

# do the linear interpolation
new_x = np.arange(0,5,1)
new_y = np.interp(new_x, x, y)
它提供了一个新的:

[0.1,0.2,0.3,0.45,0.6]
你可以用

举个简单的例子:

import numpy as np

x = np.array([0,1,2,4])
y = np.array([0.1,0.2,0.3,0.6])

# do the linear interpolation
new_x = np.arange(0,5,1)
new_y = np.interp(new_x, x, y)
它提供了一个新的:

[0.1,0.2,0.3,0.45,0.6]
这正是你要找的

它采用表格形式的函数,即(x,y)集合,并计算任何新x的线性插值

考虑以下生成所需结果的代码:

import numpy as np

a = np.array([[1 , 0.1],
 [3 , 0.2],  
 [5 , 0.4]])

# a requested range - we fill in missing integer values.
new_x = range(1,6)

# perform interpolation. Origina array is sliced by columns.
new_y = np.interp(new_x, a[:, 0], a[:, 1])

# target array is zipped together from interpolated x a y

np.array(list(zip(new_x, new_y))).tolist()
[[1.0, 0.1],
 [2.0, 0.15000000000000002],
 [3.0, 0.2],
 [4.0, 0.30000000000000004],
 [5.0, 0.4]]
这正是你要找的

它采用表格形式的函数,即(x,y)集合,并计算任何新x的线性插值

考虑以下生成所需结果的代码:

import numpy as np

a = np.array([[1 , 0.1],
 [3 , 0.2],  
 [5 , 0.4]])

# a requested range - we fill in missing integer values.
new_x = range(1,6)

# perform interpolation. Origina array is sliced by columns.
new_y = np.interp(new_x, a[:, 0], a[:, 1])

# target array is zipped together from interpolated x a y

np.array(list(zip(new_x, new_y))).tolist()
[[1.0, 0.1],
 [2.0, 0.15000000000000002],
 [3.0, 0.2],
 [4.0, 0.30000000000000004],
 [5.0, 0.4]]