我会尽力具体说明我所说的内容。首先给大家看一下代码:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)
x, y = np.random.random((2, 1000))
xbins = np.linspace(0, 1, 10)
ybins = np.linspace(0, 1, 10)
counts, _, _ = np.histogram2d(x, y, bins=(xbins, ybins))
print counts
如果我有另一个数组,您将从这段代码中获得一个二维数组
z = np.random.random((2, 1000))
那么如何从这三个数组中得到一个三维分布数组。我尝试过:
zbins = np.linspace(0, 1, 10)
counts, _,_,_ = np.histogramdd(x, y, z, bins=(xbins, ybins, zbins))
但是没有用。 而且,真正的数据文件太大,无法使用循环语句,这将花费我几个小时来运行它,而且我不容易检查。 感谢您考虑这个问题!
最佳答案
我根据您最后的评论编写了以下代码
import numpy as np
data = np.random.random((1000, 3))
nbins = 10
H, [bx, by, bz]=np.histogramdd(data, bins=(nbins,nbins,nbins),
range=((0,1),(0,1),(0,1)))
而H
是每个网格中点数的汇总。在您之前的代码中,histogramdd
未正确使用。输入数据是第一个参数,在您的情况下应该是 N x 3 数组。
您可以看到document of histogramdd here .
关于python - 如何在python2.7上从三个数组中获取三维分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46108114/