How to check variable distribution in python?

In uni testing, I need to verify that the distribution of array values ​​is uniform. For instance:

in array = [1, 0, 1, 0, 1, 1, 0, 0] there is a uniform distribution of values. Since there are four "1" and four "0"

For longer array lengths, the distribution is more β€œuniform”

How to prove that the array that is testing has a uniform distribution?

Note: an array is created using random.randint(min,max,len), fromnumpy.random

+4
source share
1 answer

- . scipy.stats.kstest http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kstest.html#scipy.stats.kstest.

In [12]:

import scipy.stats as ss
import numpy as np
In [14]:

A=np.random.randint(0,10,100)
In [16]:

ss.kstest(A, ss.randint.cdf, args=(0,10))
#args is a tuple containing the extra parameter required by ss.randint.cdf, in this case, lower bound and upper bound
Out[16]:
(0.12, 0.10331653831438881)
#This a tuple of two values; KS test statistic, either D, D+ or D-. and p-value

P 0,1033, , A . P , , , (: ), , . KS , A . p 0,1033 , . P 0,05 0,01, null. p 0,05, , A .

scipy.stats.chisquare():

In [17]:

import scipy.stats as ss
import numpy as np
In [18]:

A=np.random.randint(0, 10, 100)
In [19]:

FRQ=(A==np.arange(10)[...,np.newaxis]).sum(axis=1)*1./A.size #generate the expect frequecy table.
In [20]:

ss.chisquare(FRQ) #If not specified, the default expected frequency is uniform across categories.
Out[20]:
(0.084000000000000019, 0.99999998822800984)

- chisquare, - P.

+5

Source: https://habr.com/ru/post/1531665/


All Articles