简体   繁体   中英

Python numpy random numbers probability

Python 3.6.1 :: Anaconda custom (64-bit)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mtptlb

print (np.__version__)
1.12.1
print (mtptlb.__version__)
2.0.2

%matplotlib inline
a=np.random.uniform(1,100,1000000)
b=range(1,101)
plt.hist(a)

在此处输入图片说明

Why does the Y axis show 100000 ? np.random.uniform(1,100, 1000000 ) has the value 1000000, so shouldn't it show 1000000 on y axis?

By default matplotlib.pyplot.hist uses 10 bins. So all your 1 million values are distributed into 10 bins. For a perfect uniform distribution you would expect that you have 100k occurrences (1 million divided by 10) in each bin.

You can change the number of bins, ie

a=np.random.uniform(1, 100, 1000000)
plt.hist(a, bins=100)

在此处输入图片说明

Here it's divided into 100 bins and because it's a uniform distribution all bins are roughly at 10000.

Or just one bin if you want a count of 1 000 000:

a=np.random.uniform(1, 100, 1000000)
plt.hist(a, bins=1)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM