[python] NumPy에서 배열의 일부를 무작위로 추출하는 방법은 무엇인가요?
import numpy as np

# 1) np.random.choice를 사용하여 일부 요소 추출하기
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
sample = np.random.choice(arr, size=5, replace=False)
print(sample)

# 2) np.random.shuffle을 사용하여 배열 섞은 뒤 일부 요소 추출하기
np.random.shuffle(arr)
sample = arr[:5]
print(sample)

# 3) np.random.randint를 사용하여 일부 요소 추출하기
arr = np.arange(10)
sample_indices = np.random.randint(0, len(arr), size=5)
sample = arr[sample_indices]
print(sample)

위의 예시 코드에서는 1) np.random.choice 함수를 사용하여 배열에서 무작위로 5개의 요소를 추출한 것, 2) np.random.shuffle 함수를 사용하여 배열을 섞은 뒤 앞에서부터 5개의 요소를 추출한 것, 3) np.random.randint 함수를 사용하여 배열 인덱스 중 5개를 무작위로 선택한 뒤 해당 인덱스에 해당하는 요소를 추출한 것을 보여줍니다.

NumPy는 다양한 확률적 함수와 메서드를 제공하여 배열 처리를 더욱 유연하고 편리하게 할 수 있습니다. 더 자세한 내용은 NumPy 공식 문서를 참고하시기 바랍니다.[^1^]