본문 바로가기
Python Library/NumPy

[NumPy] numpy.random.choice

by goatlab 2022. 7. 30.
728x90
반응형
SMALL

numpy.random.choice

 

주어진 1차원 배열에서 무작위 샘플을 생성한다.

 

numpy.random.choice(a, size=None, replace=True, p=None)
a : 1-D array-like 또는 int ndarray인 경우 해당 요소에서 임의의 샘플이 생성된다. int인 경우 임의의 샘플이 np.arange(a)이 생성된다.
size : int 또는 tuple of ints (optional) 출력 모양. 예를 들어, 주어진 모양이 (m, n, k)이면 샘플 m * n * k이 그려진다. 기본값은 None이며 이 경우 단일 값이 반환된다.
replace : boolean (optional) 샘플의 중복여부, 기본값은 True이며 a 값에서 여러 번 선택할 수 있음을 의미한다.
p : 1-D array-like (optional) a의 각 항목과 관련된 확률이다. 지정하지 않은 경우 샘플은 a의 모든 항목에 대해 균일한 분포를 가정한다.
import tensorflow as tf
import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

(x_train, t_train), (x_test, t_test) = mnist.load_data()

print('')
print('x_train.shape = ', x_train.shape, ', t_train.shape = ', t_train.shape)
print('x_test.shape = ', x_test.shape, ', t_test.shape = ', t_test.shape)

random_index_list = np.random.choice(len(x_test), 5, False)

print(random_index_list)

random_x_test_list = []

for index in random_index_list:

    random_x_test_list.append(x_test[index])

random_x_test_array = np.array(random_x_test_list)

print(random_x_test_array.shape)

728x90
반응형
LIST

'Python Library > NumPy' 카테고리의 다른 글

[NumPy] np.load 배열 확인  (0) 2022.05.30
[NumPy] numpy array 전체 출력  (0) 2021.12.31
[NumPy] numpy 원소 재배열  (0) 2021.12.30
[NumPy] numpy 원소 제거 및 추가  (0) 2021.12.29
[NumPy] asarray()  (0) 2021.12.27