2

2 つのリストがあるとします。

    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]回数 (頻度) を示します。また、x1[0] = 1 および x1[8] = 1 であり、合計頻度は 4 (= x2[0] + x2[8]) であることに注意してください。x1[i]i

これを効率的にヒストグラムに変換するにはどうすればよいですか? 簡単な方法を以下に示しますが、これはおそらく非効率的 (3 番目のオブジェクトを作成してループする) であり、巨大なデータを持っているため、私を傷つけるでしょう。

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()
4

3 に答える 3

1

ビニングに問題があるようです。2 のカウントは 4 になるはずです。ではない ?ここにコードがあります。ここでは、追加の配列を 1 つ作成していますが、これは 1 回だけ動的に操作されます。それが役に立てば幸い。

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()

出力は次のとおりです。

ここに画像の説明を入力

于 2013-09-25T10:30:13.577 に答える
-1

1 つの方法はx3 = np.repeat(x1,x2)、x3 を使用してヒストグラムを作成することです。

于 2013-09-27T09:26:21.220 に答える