IT TIP

셔플 대 영구 numpy

itqueen 2020. 11. 27. 21:52
반응형

셔플 대 영구 numpy


numpy.random.shuffle(x)의 차이점은 무엇입니까 numpy.random.permutation(x)?

문서 페이지를 읽었지만 배열의 요소를 무작위로 섞고 싶을 때 둘 사이에 차이점이 있는지 이해할 수 없었습니다.

더 정확하게 말하면 배열이 있다고 가정 x=[1,4,2,8]합니다.

나는 X의 임의의 순열을 생성 할 경우의 차이 무엇 shuffle(x)permutation(x)?


np.random.permutation다음과 두 가지 차이점이 있습니다 np.random.shuffle.

  • 배열을 전달하면 배열의 셔플 된 복사본반환합니다 . np.random.shuffle배열을 제자리에 섞습니다.
  • 정수를 전달하면 셔플 된 범위를 반환합니다. np.random.shuffle(np.arange(n))

x가 정수이면 np.arange (x)를 임의로 변경합니다. x가 배열이면 요소를 무작위로 복사하고 섞습니다.

소스 코드는이를 이해하는 데 도움이 될 수 있습니다.

3280        def permutation(self, object x):
...
3307            if isinstance(x, (int, np.integer)):
3308                arr = np.arange(x)
3309            else:
3310                arr = np.array(x)
3311            self.shuffle(arr)
3312            return arr

@ecatmur가 말한 것에 추가하면 np.random.permutation특히 분류를 위해 순서가 지정된 쌍을 섞을 필요가있을 때 유용합니다.

from np.random import permutation
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target

# Data is currently unshuffled; we should shuffle 
# each X[i] with its corresponding y[i]
perm = permutation(len(X))
X = X[perm]
y = y[perm]

참고 URL : https://stackoverflow.com/questions/15474159/shuffle-vs-permute-numpy

반응형