Numpy 随机选择偏向第一个元素

问题描述 投票:0回答:1

所以我有三个日期列表

(x1,x2,x3)
。每个列表等于前一个列表加上一个新元素:

x1=['2005-10-31', '2006-01-31', '2006-04-30', '2006-07-31', '2006-10-31', '2007-01-31', '2007-04-30', '2007-07-31', '2007-10-31', '2008-01-31', '2008-04-30', '2008-07-31', '2008-10-31', '2009-01-31', '2009-04-30', '2009-07-31', '2009-10-31', '2010-01-31', '2010-04-30', '2010-07-31', '2010-10-31', '2011-01-31', '2011-04-30', '2011-07-31', '2011-10-31', '2012-01-31', '2012-04-30', '2012-07-31', '2012-10-31', '2013-01-31', '2013-04-30', '2013-07-31', '2013-10-31', '2014-01-31', '2014-04-30', '2014-07-31', '2014-10-31', '2015-01-31', '2015-04-30', '2015-07-31', '2015-10-31', '2016-01-31']

x2=['2005-10-31', '2006-01-31', '2006-04-30', '2006-07-31', '2006-10-31', '2007-01-31', '2007-04-30', '2007-07-31', '2007-10-31', '2008-01-31', '2008-04-30', '2008-07-31', '2008-10-31', '2009-01-31', '2009-04-30', '2009-07-31', '2009-10-31', '2010-01-31', '2010-04-30', '2010-07-31', '2010-10-31', '2011-01-31', '2011-04-30', '2011-07-31', '2011-10-31', '2012-01-31', '2012-04-30', '2012-07-31', '2012-10-31', '2013-01-31', '2013-04-30', '2013-07-31', '2013-10-31', '2014-01-31', '2014-04-30', '2014-07-31', '2014-10-31', '2015-01-31', '2015-04-30', '2015-07-31', '2015-10-31', '2016-01-31', '2016-04-30']

x3=['2005-10-31', '2006-01-31', '2006-04-30', '2006-07-31', '2006-10-31', '2007-01-31', '2007-04-30', '2007-07-31', '2007-10-31', '2008-01-31', '2008-04-30', '2008-07-31', '2008-10-31', '2009-01-31', '2009-04-30', '2009-07-31', '2009-10-31', '2010-01-31', '2010-04-30', '2010-07-31', '2010-10-31', '2011-01-31', '2011-04-30', '2011-07-31', '2011-10-31', '2012-01-31', '2012-04-30', '2012-07-31', '2012-10-31', '2013-01-31', '2013-04-30', '2013-07-31', '2013-10-31', '2014-01-31', '2014-04-30', '2014-07-31', '2014-10-31', '2015-01-31', '2015-04-30', '2015-07-31', '2015-10-31', '2016-01-31', '2016-04-30','2016-07-31']

令我惊讶的是,从上述任何一个中随机抽取长度为

len(x1)
的样本都会返回相同的结果。

np.random.seed(0)
np.random.choice(x1,len(x1),replace=True)
array(['2005-10-31', '2006-07-31', '2006-07-31', '2015-07-31',
       '2008-01-31', '2010-07-31', '2011-01-31', '2014-10-31',
       '2011-07-31', '2007-04-30', '2011-10-31', '2011-10-31',
       '2008-10-31', '2006-01-31', '2015-04-30', '2015-07-31',
       '2011-07-31', '2011-10-31', '2010-01-31', '2015-01-31',
       '2012-01-31', '2009-01-31', '2007-10-31', '2008-01-31',
       '2010-10-31', '2009-10-31', '2007-01-31', '2009-07-31',
       '2005-10-31', '2010-04-30', '2014-07-31', '2011-10-31',
       '2013-01-31', '2010-07-31', '2010-07-31', '2009-04-30',
       '2015-07-31', '2013-10-31', '2006-01-31', '2008-01-31',
       '2013-10-31', '2013-07-31'], dtype='<U10')

以下唯一的区别是从

x2
中进行选择。尽管此列表多了一个元素,但结果完全相同:

np.random.seed(0)
np.random.choice(x2,len(x1),replace=True)
array(['2005-10-31', '2006-07-31', '2006-07-31', '2015-07-31',
       '2008-01-31', '2010-07-31', '2011-01-31', '2014-10-31',
       '2011-07-31', '2007-04-30', '2011-10-31', '2011-10-31',
       '2008-10-31', '2006-01-31', '2015-04-30', '2015-07-31',
       '2011-07-31', '2011-10-31', '2010-01-31', '2015-01-31',
       '2012-01-31', '2009-01-31', '2007-10-31', '2008-01-31',
       '2010-10-31', '2009-10-31', '2007-01-31', '2009-07-31',
       '2005-10-31', '2010-04-30', '2014-07-31', '2011-10-31',
       '2013-01-31', '2010-07-31', '2010-07-31', '2009-04-30',
       '2015-07-31', '2013-10-31', '2006-01-31', '2008-01-31',
       '2013-10-31', '2013-07-31'], dtype='<U10')

x3
中进行选择也是如此,其中有两个附加元素:

np.random.seed(0)
np.random.choice(x3,len(x1),replace=True)
array(['2005-10-31', '2006-07-31', '2006-07-31', '2015-07-31',
       '2008-01-31', '2010-07-31', '2011-01-31', '2014-10-31',
       '2011-07-31', '2007-04-30', '2011-10-31', '2011-10-31',
       '2008-10-31', '2006-01-31', '2015-04-30', '2015-07-31',
       '2011-07-31', '2011-10-31', '2010-01-31', '2015-01-31',
       '2012-01-31', '2009-01-31', '2007-10-31', '2008-01-31',
       '2010-10-31', '2009-10-31', '2007-01-31', '2009-07-31',
       '2005-10-31', '2010-04-30', '2014-07-31', '2011-10-31',
       '2013-01-31', '2010-07-31', '2010-07-31', '2009-04-30',
       '2015-07-31', '2013-10-31', '2006-01-31', '2008-01-31',
       '2013-10-31', '2013-07-31'], dtype='<U10')

这是否意味着当进行放回采样并且样本大小

(len(x1))
小于输入大小(
len(x2)
len(x3)
)时,算法会偏向于选择第一个
len(x1)
元素?

更新

所以我用不同的种子(1)尝试了类似的方法,似乎问题部分存在,因为前 k=19 个元素惊人地相同:

np.random.seed(1)
np.random.choice(x1,len(x1),replace=True)
array(['2015-01-31', '2008-10-31', '2007-10-31', '2008-01-31',
       '2008-07-31', '2007-01-31', '2009-07-31', '2005-10-31',
       '2009-10-31', '2006-01-31', '2008-10-31', '2007-07-31',
       '2007-04-30', '2012-01-31', '2010-10-31', '2015-01-31',
       '2010-04-30', '2010-10-31', '2008-07-31', '2012-10-31',
       '2013-01-31', '2009-04-30', '2006-10-31', '2011-07-31',
       '2011-07-31', '2016-01-31', '2013-04-30', '2013-10-31',
       '2011-04-30', '2009-01-31', '2016-01-31', '2008-01-31',
       '2007-07-31', '2011-04-30', '2006-01-31', '2005-10-31',
       '2010-01-31', '2007-10-31', '2011-10-31', '2009-01-31',
       '2007-10-31', '2013-04-30'], dtype='<U10')

np.random.seed(1)
np.random.choice(x2,len(x1),replace=True)
array(['2015-01-31', '2008-10-31', '2007-10-31', '2008-01-31',
       '2008-07-31', '2007-01-31', '2009-07-31', '2005-10-31',
       '2009-10-31', '2006-01-31', '2008-10-31', '2007-07-31',
       '2007-04-30', '2012-01-31', '2010-10-31', '2015-01-31',
       '2010-04-30', '2010-10-31', '2008-07-31', '2016-04-30',
       '2012-10-31', '2013-01-31', '2009-04-30', '2006-10-31',
       '2011-07-31', '2011-07-31', '2016-01-31', '2013-04-30',
       '2013-10-31', '2011-04-30', '2009-01-31', '2016-01-31',
       '2008-01-31', '2007-07-31', '2011-04-30', '2006-01-31',
       '2005-10-31', '2010-01-31', '2007-10-31', '2011-10-31',
       '2009-01-31', '2016-04-30'], dtype='<U10')
numpy random
1个回答
0
投票

在计算机科学中,随机数生成器实际上是伪随机数生成器。他们在统计意义上产生随机数,但它们并不是真正随机的。为了使它们的行为可重复,您可以设置一个起始值 - 种子,然后生成器将始终生成相同的数字序列。 (从统计意义上来说,这仍然是 radnom。)

在每个示例中,您都使用

np.random.seed(0)
“重置”随机数生成器,并且另外在每个示例中您请求生成
len(x1)
数字,因此它始终生成相同的数字序列。

仅在第一个示例之前删除

np.random.seed(0)
或放置
np.random.seed(0)
,它将“随机”工作。

© www.soinside.com 2019 - 2024. All rights reserved.