获得几条曲线的平均曲线,其中 x 值不相同 [英] Getting a mean curve of several curves with x-values not being the same

查看:48
本文介绍了获得几条曲线的平均曲线,其中 x 值不相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个包含许多 x 和 y 值的数据集.值少得多的示例如下所示:

data_set1:

x1 y1--------- ---------0 1000.0100523 65.10770.0201047 64.05190.030157 63.03410.0402094 62.13090.0502617 61.36490.060314 60.86140.0703664 60.35550.0804187 59.76350.0904711 59.1787

data_set2:

x2 y2--------- ---------0 1000.01 66.1190.02 64.45930.03 63.13770.04 62.03860.05 61.09430.06 60.28110.07 59.56030.08 58.8908

所以在这里我有(对于这个例子)两个包含 10 个 x 和 y 值的数据集.y 值总是不同的,但在某些情况下 x 值会相同,有时它们会不同 - 就像在这种情况下一样.不是很多,但仍然是不同的.将这两个数据集绘制成一个图形会产生两条不同的曲线,我现在想制作两者的平均曲线.如果 x 值相同,我只会取 y 值的平均值并将它们与 x 值作图,但如上所述,它们有时不同,有时相同.有没有什么方法可以推断,或者类似的东西,这样我就可以平均这些值(再次,对于许多数据集)而不必只是猜测"或说它们几乎相同,所以只需平均就可以了y 值".外推似乎是一种可行的方法,但我从未在 python 中使用过它,也许还有更好的方法来做到这一点?

解决方案

如果你在每个数据集中有相同数量的点(你的例子没有,但你在你的帖子中声明你有),你可以只需从每个集合中获取各自 x 值的平均值,以及各自 y 值的平均值.如果您没有相同数量的值,您可以按照

I have several datasets containing many x- and y-values. An example with a lot fewer values would look something like this:

data_set1:

x1          y1        
---------   ---------   
0           100
0.0100523   65.1077
0.0201047   64.0519
0.030157    63.0341
0.0402094   62.1309
0.0502617   61.3649
0.060314    60.8614
0.0703664   60.3555
0.0804187   59.7635
0.0904711   59.1787

data_set2:

x2          y2        
---------   ---------   
0           100
0.01        66.119
0.02        64.4593
0.03        63.1377
0.04        62.0386
0.05        61.0943
0.06        60.2811
0.07        59.5603
0.08        58.8908

So here I have (for this example) two data sets containing 10 x- and y-values. The y-values are always different, but in some cases the x-values will be the same, and sometimes they will be different - as in this case. Not by a lot, but still, they are different. Plotting these two data sets into a graph yields two different curves, and I would now like to make a mean curve of both. If the x-values were the same I would just take the mean of the y-values and plot them against the x-values, but as stated, they are sometimes different, and sometimes the same. Is there some way to extrapolate, or something like that, so that I could average the values (again, for many data sets) without "just guessing" or saying "they are pretty much the same, so it will be okay just to average the y-values". Extrapolation seems like a plausible way of doing this, but I have never played with it in python, and maybe there are even better ways to do this ?

解决方案

If you have the same number of points in each dataset (the example you have doesn't, but you state in your post that you do), you could just get the mean of the respective x values from each set, and the mean of the respective y values. If you do not have the same number of values, you could follow the answers in this post

For example given your data, but with 9 points each:

>>> x1
array([0.       , 0.0100523, 0.0201047, 0.030157 , 0.0402094, 0.0502617,
       0.060314 , 0.0703664, 0.0804187])
>>> y1
array([100.    ,  65.1077,  64.0519,  63.0341,  62.1309,  61.3649,
        60.8614,  60.3555,  59.7635])
>>> x2
array([0.  , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08])
>>> y2
array([100.    ,  66.119 ,  64.4593,  63.1377,  62.0386,  61.0943,
        60.2811,  59.5603,  58.8908])

You can do:

import numpy as np

mean_x = np.mean((x1,x2), axis=0)
mean_y = np.mean((y1,y2), axis=0)

And when to show visually, you can plot. Here, the black line is your mean line, and the blue and orange lines are your original datasets:

import matplotlib.pyplot as plt
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.plot(mean_x,mean_y, color='black')
plt.show()

这篇关于获得几条曲线的平均曲线,其中 x 值不相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆