我正在嘗試進行多種排列。從代碼:
# 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))
我能怎么做?
uj5u.com熱心網友回復:
要從某個集合中獲取 20 個排列,請初始化itertools.permutations
迭代器,然后用于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.permutation
以相同的方式使用很多:
[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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479486.html
上一篇:從csv檔案中選擇某些列