LoginSignup
0
1

More than 3 years have passed since last update.

numpyでヒストグラムの最大地点を取得する関数

Last updated at Posted at 2020-05-03

meanでもmedianでもなくhmaxを取得したい時がある。

image.png

関数

def hmax(x, bins=20):
    import numpy as np
    hist, bins = np.histogram(x, density=True, bins=20)
    delta = (bins[1]-bins[0])/2
    xr = np.linspace(np.min(bins)+delta,np.max(bins)-delta,len(bins)-1)
    return xr[np.argmax(hist)]

プロット

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline
plt.rcParams['font.size']=15

x = np.random.beta(2,5,100000)
df_plot = pd.DataFrame(fit.extract()['mu'])
c = sns.color_palette('husl',3)
sns.distplot(x,color='gray')
plt.axvline(x=np.mean(x),label='mean',color=c[0])
plt.axvline(x=np.median(x),label='median',color=c[1])
plt.axvline(hmax(x),label='hmax',color=c[2])
plt.legend()
plt.show()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1