【问题标题】:histogram lists into one array直方图列表到一个数组中
【发布时间】:2015-11-13 16:03:44
【问题描述】:

我从一个 10 x 2000 的数组开始,然后运行一个循环来获取每行包含 100 个 bin 的直方图。该循环为我提供了 2000 个单独的 1 x 100 数组。 我需要能够将所有这些 2000 个直方图放入一个大数组中,以便我可以绘制它。我似乎无法弄清楚如何做到这一点。如果我尝试在循环之外执行此操作,它只会给我最后一个直方图。我需要全部2000个。 这是我的循环:

for i in np.arange(1999):
    temp_histogram, bin_edges= np.histogram([critters_intervals[i, :]], bins=np.arange(101))

任何帮助将不胜感激。

【问题讨论】:

    标签: python arrays


    【解决方案1】:

    以下将当前直方图附加到之前的直方图:

    # prepare result array
    histograms = np.array([])
    for i in np.arange(1999):
        temp_histogram, bin_edges= np.histogram([critters_intervals[i, :]], bins=np.arange(101))
        # append latest histogram to the previous ones
        np.concatenate((histograms, temp_histogram))
    

    如果您想对直方图求和/累加,可以执行以下操作:

    accumulated_histogram = np.zeros(100)
    for i in np.arange(1999):
        temp_histogram, bin_edges= np.histogram([critters_intervals[i, :]], bins=np.arange(101))
        # add latest histogram to the previous counts
        accumulated_histogram += temp_histogram
    

    编辑(回复评论):

    如果您想将所有直方图存储在一个二维数组中,您有以下选择:

    1.预分配一个二维数组并随后填充它:

    all_histograms = np.zeros((1999, 100))
    for i in np.arange(1999):
        all_histograms[:,i], bin_edges= np.histogram([critters_intervals[i, :]], bins=np.arange(101))
    

    2.生成直方图后将其堆叠

    # initialize array with first histogram
    all_histograms, all_bin_edges = np.histogram([critters_intervals[0, :]], bins=np.arange(101))
    for i in np.arange(1999):
        temp_histogram, bin_edges= np.histogram([critters_intervals[i, :]], bins=np.arange(101))
        all_histograms = np.vstack((all_histograms, temp_histogram))
    

    【讨论】:

    • 谢谢。但是当我使用你的第一个建议时,它并没有给我一个数组。它仍然导致几个单独的。我需要将所有单独的直方图作为一个数组的行。第一个直方图是第 1 行,第二个直方图是第 2 行,依此类推。
    • 您的问题并不清楚!给我一些时间来相应地更新我的答案......
    • 很抱歉。非常感谢。
    • 不要道歉,改进 ;) --> how to ask
    猜你喜欢
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2020-05-16
    • 2014-02-15
    • 2016-09-22
    • 2016-06-14
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多