0

確率値が格納された配列があります。一部の値は 0 です。各ビンに同じ数の要素があるようにヒストグラムをプロットする必要があります。matplotlibs hist 関数を使用してみましたが、ビンの数を決定できます。これをプロットするにはどうすればよいですか?(通常のプロットとヒストの作業ですが、必要なものではありません)

私は10000のエントリを持っています。値が 0 より大きく、0.0005 から 0.2 の間にあるのは 200 個だけです。この分布は、0.2 の要素が 1 つしかないのに対し、2000 付近の値は 0.0005 であるため、均一ではありません。したがって、ビンの幅が等しくなく、要素の数が同じでなければならないため、プロットが問題でした

4

1 に答える 1

2

このタスクは私にはあまり意味がありませんが、次のコードは、私がやるべきこととして理解したものです。

また、コードの最後の行は、あなたが本当にやりたかったことだと思います。視覚化を改善するために異なるビン幅を使用します (ただし、各ビン内で同量のサンプルの分布をターゲットにしないでください)。method='blocks' で astroml の histを使用しました( astropy もこれをサポートしています)

コード

# Python 3 -> beware the // operator!

import numpy as np
import matplotlib.pyplot as plt
from astroML import plotting as amlp

N_VALUES = 1000
N_BINS = 100

# Create fake data
prob_array = np.random.randn(N_VALUES)
prob_array /= np.max(np.abs(prob_array),axis=0)  # scale a bit

# Sort array
prob_array = np.sort(prob_array)

# Calculate bin-borders,
bin_borders = [np.amin(prob_array)] + [prob_array[(N_VALUES // N_BINS) * i] for i in range(1, N_BINS)] + [np.amax(prob_array)]

print('SAMPLES: ', prob_array)
print('BIN-BORDERS: ', bin_borders)

# Plot hist
counts, x, y = plt.hist(prob_array, bins=bin_borders)
plt.xlim(bin_borders[0], bin_borders[-1] + 1e-2)
print('COUNTS: ', counts)
plt.show()


# And this is, what i think, what you really want

fig, (ax1, ax2) = plt.subplots(2)
left_blob = np.random.randn(N_VALUES/10) + 3
right_blob = np.random.randn(N_VALUES) + 110
both = np.hstack((left_blob, right_blob))  # data is hard to visualize with equal bin-widths

ax1.hist(both)
amlp.hist(both, bins='blocks', ax=ax2)
plt.show()

出力

ここに画像の説明を入力 ここに画像の説明を入力

于 2016-08-31T20:23:38.570 に答える