簡體   English   中英

帶有單獨列表的直方圖表示頻率

[英]Histogram with separate list denoting frequency

假設我有兩個列表:

    x1 = [1,2,3,4,5,6,7,8,1,10]
    x2 = [2,4,2,1,1,1,1,1,2,1]

此處,列表的每個索引i是一個時間點, x2[i]表示在時間i觀察到的次數(頻率)比x1[i] 還要注意x1 [0] = 1和x1 [8] = 1,總頻率為4(= x2 [0] + x2 [8])。

如何有效地將其轉換為直方圖? 下面是簡單的方法,但這可能效率低下(創建第三個對象並循環),並且由於我擁有巨大的數據,這會傷害我。

import numpy as np
import matplotlib.pyplot as plt

x3 = []
for i in range(10):
    for j in range(x2[i]):
        x3.append(i)

hist, bins = np.histogram(x1,bins = 10)
width = 0.7*(bins[1]-bins[0])
center = (bins[:-1]+bins[1:])/2
plt.bar(center, hist, align = 'center', width = width)
plt.show()

最好的方法是在np.histogram (doc)上使用weights kwarg,這還將處理x1任意bin大小和非整數值

vals, bins = np.histogram(x1, bins=10, weights=x2)

如果您只需要基於整數值進行累加,則可以一次創建直方圖:

new_array = np.zeros(x2.shape)  # or use a list, but I like numpy and you have it
for ind, w in izip(x1, x2):
     # -1 because your events seem to start at 1, not 0
     new_array[ind-1] += w

如果您真的想對列表執行此操作,則可以使用列表理解

[_x for val, w in zip(x1, x2) for _x in [val]*w]

哪個返回

[1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 6, 7, 8, 1, 1, 10]

附帶說明一下,值得了解如何手動有效地計算直方圖:

from __future__ import division
from itertools import izip

num_new_bins = 5
new_min = 0
new_max = 10
re_binned = np.zeros(num_new_bins)
for v, w in izip(x1, x2):
    # figure out what new bin the value should go into
    ind = int(num_new_bins * (v - new_min) / new_max)
    # make sure the value really falls into the new range
    if ind < 0 or ind >= num_new_bins:
        # over flow
        pass
    # add the weighting to the proper bin
    re_binned[ind] += w

看來您的分檔有問題。2的計數應為4。 是不是 這是代碼。 在這里,我們額外創建了一個數組,但它只能運行一次,而且可以動態運行。 希望能幫助到你。

import numpy as np
import matplotlib.pyplot as plt

x1 = [1,2,3,4,5,6,7,8,1,10]
x2 = [2,4,2,1,1,1,1,1,2,1]

#your method
x3 = []
for i in range(10):
    for j in range(x2[i]):
        x3.append(i)
plt.subplot(1,2,1)
hist, bins = np.histogram(x1,bins = 10)
width = 0.7*(bins[1]-bins[0])
center = (bins[:-1]+bins[1:])/2
plt.bar(center, hist, align = 'center', width = width)
plt.title("Posted Method")
#plt.show()

#New Method
new_array=np.zeros(len(x1))
for count,p in enumerate(x1):
    new_array[p-1]+=x2[count]
plt.subplot(1,2,2)  
hist, bins = np.histogram(x1,bins = 10)
width = 0.7*(bins[1]-bins[0])
center = (bins[:-1]+bins[1:])/2
plt.bar(center, new_array, align = 'center', width = width)
plt.title("New Method")
plt.show()

這是輸出:

在此處輸入圖片說明

一種方法是使用x3 = np.repeat(x1,x2)並使用x3制作直方圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM