打印多个随机排列

发布于 2025-01-30 11:53:08 字数 660 浏览 3 评论 0原文

我正在尝试进行多个排列。从代码中:

# generate random Gaussian values
from numpy.random import seed
from numpy.random import randn
# seed random number generator
seed(1)
# generate some Gaussian values
values = randn(100)
print(values)

但是现在我想生成,例如,(值)20个排列。 使用代码:

import numpy as np
import random
from itertools import permutations
result = np.random.permutation(values)
print(result)

我只能观察到一个排列(或“手动”获得其他置换)。我希望我有很多排列(20或更多),因此自动计算每个置换量(来自值)的Durbin-Watson统计量。

from statsmodels.stats.stattools import durbin_watson
sm.stats.durbin_watson(np.random.permutation(values))

我该怎么办?

I am trying to do multiple permutations. From code:

# generate random Gaussian values
from numpy.random import seed
from numpy.random import randn
# seed random number generator
seed(1)
# generate some Gaussian values
values = randn(100)
print(values)

But now I would like to generate, for example, 20 permutations (of values).
With code:

import numpy as np
import random
from itertools import permutations
result = np.random.permutation(values)
print(result)

I can only observe one permutation (or "manually" get others). I wish I had many permutations (20 or more) and so automatically calculate the Durbin-Watson statistic for each permutation (from values).

from statsmodels.stats.stattools import durbin_watson
sm.stats.durbin_watson(np.random.permutation(values))

How can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2025-02-06 11:53:08

要从某些集合中获取20个排列,请Intial itertools.permutations iterator,然后使用next()进行第一个20:

import numpy as np
import itertools as it

x = np.random.random(100)  # 100 random floats
p = it.permutations(x)  # an iterator which produces permutations (DON'T TRY TO CALCULATE ALL OF THEM)
first_twenty_permutations = [next(p) for _ in range(20)]

当然,这些不会是随机的排列(即它们以有组织的方式计算,尝试it.permutations(“ abcdef”),您会明白我的意思)。如果需要随机排列,则可以以相同的方式使用np.random.permunt.

[np.random.permutation(x) for _ in range(20)]

然后计算Durbin Watson统计信息:

permutations = np.array([np.random.permutation(x) for _ in range(20)])
np.apply_along_axis(durbin_watson, axis=1, arr=permutations)

To get 20 permutations out of some collection, intialize the itertools.permutations iterator and then use next() to take the first twenty:

import numpy as np
import itertools as it

x = np.random.random(100)  # 100 random floats
p = it.permutations(x)  # an iterator which produces permutations (DON'T TRY TO CALCULATE ALL OF THEM)
first_twenty_permutations = [next(p) for _ in range(20)]

Of course, these won't be random permutations (i.e., they are calculated in an organized manner, try with it.permutations("abcdef") and you'll see what I mean). If you need random permutations, you can use np.random.permutation much in the same way:

[np.random.permutation(x) for _ in range(20)]

To then calculate the Durbin Watson statistic:

permutations = np.array([np.random.permutation(x) for _ in range(20)])
np.apply_along_axis(durbin_watson, axis=1, arr=permutations)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文