cannot reshape array of size 785 into shape (28,28) 에러

Hyuntae Jung·2022년 3월 28일
0

cannot reshape array of size 785 into shape (28,28)

https://www.youtube.com/watch?v=N3oMKS1AfVI
을 학습하는 과정에서 에러가 발생함.

split the training and testing data into X (image) and Y (label) arrays


train_data = np.array(train_df, dtype='float32')
test_data = np.array(test_df, dtype='float32')

x_train = train_data[:, 1:] / 255  # 1 column 제외 (label) , 2^8 pixel
y_train = train_data[:, 0]  # label

x_test = test_data[:, 1:] / 255
y_test = test_data[:, 0]

split the training data into train and validate arrays (will be used later)

x_train, x_validate, y_train, y_validate = train_test_split(
    x_train, y_train, test_size=0.2, random_state=12345,
)

lets see what the images look like

image = x_train[50, :].reshape((28, 28))

plt.imshow(image)
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\Public\Documents\ESTsoft\CreatorTemp/ipykernel_1724/3578654178.py in <module>
      1 # lets see what the images look like
      2 
----> 3 image = x_train[50, :].reshape((28, 28))
      4 
      5 plt.imshow(image)

ValueError: cannot reshape array of size 785 into shape (28,28)

위와 같은 에러 발생

Why???

의류 이미지는 가로 28 * 세로 28 = 784개의 픽셀로 이루어짐

ValueError: cannot reshape array of size 785 into shape (28,28)

이유는 28*28 = 754 이지만, 불러온 데이터셋은 달라서 발생함

하지만 내가 설정한 것은

image = x_train[50, :].reshape((28, 28))

이므로 데이터가 시작되는 시점을 잘 선택해야함

위와 같이 3번째부터 시작하는 것을 알 수 있다.

해결

x_train을 정의할때

x_train = train_data[:, 1:] / 255 

으로 했으므로

image = x_train[50, 1:].reshape((28, 28))

plt.imshow(image)
plt.show()

이와 같이 수정하면 정상적으로 코드가 실행된다.

0개의 댓글