[英]calculating mean of several numpy masked arrays (masked_all)
first of all I'm new to python and programming but you guys already helped me a lot, so thanks a lot! 首先,我是python和编程的新手,但是你们已经为我提供了很多帮助,非常感谢! But I've come to a problem I haven't found an answer so far:
但是我遇到了一个问题,到目前为止我还没有找到答案:
I have the data of several plates where the data represents the pressure on each plate at a large number of different spots. 我有几个板的数据,其中的数据表示在许多不同点上每个板上的压力。 The thing is, these plates aren't perfectly round because of the sensors measuring the pressure and sometimes these sensors even produce an error so I don't have any data at a spot within the plate.
事实是,由于传感器测量压力,这些板并不是完美的圆形,有时这些传感器甚至会产生错误,因此我在板内的某个位置上没有任何数据。
When I just have to plot one plate, I'll do it like that: 当我只需要绘制一个盘子时,我会像这样:
import numpy.ma as ma
matrix=ma.masked_all((160,65),float)
for x in range(len(plate.X)):
matrix[(plate.Y[x],plate.X[x])]=data.index(plate.measurementname[x])
image.pcolormesh(matrix,min,max)
This works fine. 这很好。 Now that I have several plates I'd like to plot the mean pressure on each spot.
现在,我有几个板块,我想在每个点上绘制平均压力。 Because I don't know any mean function, I thought of adding all plates together and divide by the number of plates...I tried following:
因为我不知道任何均值函数,所以我考虑将所有板块相加并除以板块数...我尝试了以下操作:
import numpy.ma as ma
meanmatrix=ma.masked_all((160,65),float)
for plate in plateslist:
matrix=ma.masked_all((160,65),float)
for x in range(len(plate.X)):
matrix[(plate.Y[x],plate.X[x])]=data.index(plate.measurementname[x])
meanmatrix+=matrix
meanmatrix=meanmatrix/len(plateslist)
image.pcolormesh(meanmatrix,min,max)
This works pretty good but there's one problem I can't solve. 这个效果很好,但是有一个我无法解决的问题。 As I said sometimes some plates didn't get all data, therefore there's a "hole" at some spots in the plot.
正如我说过的,有些板块有时无法获取所有数据,因此图中某些点存在“空洞”。 Now my meanmatrix has a whole where ever one of the plates had a whole even if all others had data at that spot.
现在,我的均值矩阵具有一个整数,即使其中所有其他板块都具有该点的数据,其中一个板块也具有一个整体。
How can I make sure I won't get these holes or is there even a smoother way of getting my "meanmatrix"?? 我如何确保我不会碰到这些孔,或者是否有更平滑的方式获取“ meanmatrix”? (I hope my question is clear enough...)
(我希望我的问题足够清楚...)
Edit: 编辑:
The problem is not that I don't get the mean of the data, this actually works (well I don't like how I did it but it works), the problem is that I get these "holes" I described before. 问题不是我没有得到数据的平均值,这实际上是可行的(好吧,我不喜欢我是如何做到的,但是它是可行的),问题是我遇到了我之前描述的“漏洞”。 That's what bothers me.
那就是让我困扰的事情。
EDIT: Sorry, I misinterpreted the question. 编辑:对不起,我误解了这个问题。 Try this:
尝试这个:
allplates = ma.masked_all((160, 65, numplates))
# fill in allplates
meanplate = allplates.mean(axis=2)
This will compute the mean over the last dimension of the array, ie, average the plates together. 这将计算阵列最后一个维度的平均值,即将板平均在一起。 Missing values are ignored.
缺少的值将被忽略。
Earlier answer: You can take the mean of a masked array, and it will ignore the missing values: 较早的答案:您可以取一个掩码数组的平均值,它将忽略缺失的值:
>>> X = ma.masked_all((160, 65))
>>> X.mean()
masked
>>> X[0, 0] = 1
>>> X.mean()
1.0
Try to avoid using matrix
as a variable name, though, because it also refers to a NumPy data structure. 但是,请尝试避免将
matrix
用作变量名,因为它也引用NumPy数据结构。
Ok I got an answer: 好吧,我得到了答案:
import numpy.ma as ma
allplats=ma.masked_all((160,65),float)
for plate in plateslist:
for x in range(len(plate.X)):
allplates[(plate.Y[x],plate.X[x])]+=data.index(plate.measurementname[x])
allplates=allplates/len(plateslist)
image.pcolormesh(meanmatrix,min,max)
This actually works! 这实际上有效! So i guess there was a mistake when adding two masked_all arrays...("Stupid is as stupid does") If someone has a better approach to get the mean of all plates at each single spot, it would be nice to read it.
所以我猜想在添加两个masked_all数组时会出错...(“愚蠢就像愚蠢一样”)如果有人有更好的方法来获取每个点的所有板的均值,那么阅读它会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.