获取数据帧的相同随机事件

2024-10-08 19:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始用python。我只是想验证一下,假设我将一些.root文件传递给一个方法,该方法首先将其转换为dataframe,然后它们洗牌其中的事件,并以.h5格式给出输出

def loadDF(self, rndm = 12345):

    df = rpd.read_root(self.file_name,self.tree_name)


    rndseed = rndm
    np.random.seed(rndseed)
    index = list(df.index)
    np.random.shuffle(index)

    self.df = df

    hdf = pd.HDFStore(self.tree_name + '.h5')
    hdf.put('hdf', self.df)
    self.hdf = hdf
    hdf.close()

现在假设我想从.root文件中得到相同的无序数据帧。我想要的就是现在。h5不应该被创造出来。那么下面的方法正确吗

def loadDF(self, rndm = 12345):

    ddf = rpd.read_root(self.file_name,self.tree_name)

    rndseed = rndm
    np.random.seed(rndseed)
    index = list(ddf.index)
    np.random.shuffle(index)

    self.ddf = ddf

简言之,通过“np.random.seed(same\ u number)”我们可以回溯相同的随机化数据吗


Tags: 方法nameselftreedfindexnprandom
1条回答
网友
1楼 · 发布于 2024-10-08 19:27:44

random.seeddocs依次引用random.RandomState类的documentation,这里我们有了关于你问题的确定词

Compatibility Guarantee A fixed seed and a fixed series of calls to ‘RandomState’ methods using the same parameters will always produce the same results up to roundoff error except when the values were incorrect. Incorrect values will be fixed and the NumPy version in which the fix was made will be noted in the relevant docstring. Extension of existing parameter ranges and the addition of new parameters is allowed as long the previous behavior remains unchanged.

因此,只要不需要修复RandomState类或其中一个方法中的bug,就可以保证结果是相同的

在我看来,关于你的问题

In short, by "np.random.seed(same_number)" could we retrace the same randomized data?

不是肯定的,但是肯定的

相关问题 更多 >

    热门问题