Python 按间隔平均数组
我有一个二维浮点数数组,如下所示(数据): 如您所见,这些值不是唯一的,单个x值可以有多个y值 我想通过取x轴上给定间隔内这些点的y值的平均值来生成一条平均曲线 我可以在x轴上设置20个间隔,并在每个间隔上迭代搜索x值所在的y值。以下是MWE:Python 按间隔平均数组,python,arrays,numpy,scipy,average,Python,Arrays,Numpy,Scipy,Average,我有一个二维浮点数数组,如下所示(数据): 如您所见,这些值不是唯一的,单个x值可以有多个y值 我想通过取x轴上给定间隔内这些点的y值的平均值来生成一条平均曲线 我可以在x轴上设置20个间隔,并在每个间隔上迭代搜索x值所在的y值。以下是MWE: import numpy as np # Define array with the data. a = [[lots of floats][lots of floats]] # Define intervals in x. interv_x =
import numpy as np
# Define array with the data.
a = [[lots of floats][lots of floats]]
# Define intervals in x.
interv_x = np.linspace(14., 24., 20)
interv_y = []
for i, x in enumerate(interv_x):
if i < len(interv_x) - 1:
y_lst = []
# Search for points inside this interval.
for j, y_val in enumerate(a[1]):
if x <= a[0][j] < interv_x[i + 1]:
y_lst.append(y_val)
# Average the y values of all these points.
interv_y.append(np.average(y_lst))
# Plot results.
plt.scatter(a[0], a[1], c='b', s=10)
plt.scatter(interv_x[:-1], interv_y, c='r', s=50)
plt.show()
将numpy导入为np
#用数据定义数组。
a=[[大量浮动][大量浮动]]
#以x为单位定义间隔。
interv_x=np.linspace(14,24,20)
interv_y=[]
对于枚举中的i,x(interv_x):
如果i 如果你想画一条平滑的曲线?