Tag Archives: sklearn.utils.validation.check_random_state(seed)

sklearn.utils.validation.check_random_state(seed)

Parameters:

————————————————————————————————————

Seed: if seed is none, random state singleton (NP. Random) is returned

If seed is int, a new random state instance is returned, which is generated by the new seed

If seed is a random state instance, it returns itself directly

Process

————————————————————————————————————

defcheck_random_state(seed):

"""Turnseedintoanp.random.RandomStateinstance
IfseedisNone,returntheRandomStatesingletonusedbynp.random.
Ifseedisanint,returnanewRandomStateinstanceseededwithseed.
IfseedisalreadyaRandomStateinstance,returnit.
OtherwiseraiseValueError.
"""
#Returns np.random.mtrand._rand if seed is None or np.random.
ifseedisNoneorseedisnp.random:
returnnp.random.mtrand._rand

#seed for numbers.Intergral or np.integer instances.
ifisinstance(seed,(numbers.Integral,np.integer)):
returnnp.random.RandomState(seed)

#Returns itself if it is np.random.RandomState.


ifisinstance(seed,np.random.RandomState):
returnseed


# Error is reported if it is not the above case.


raiseValueError('%rcannotbeusedtoseedanumpy.random.RandomState'
'instance'%seed)