티스토리 뷰

카테고리 없음

MNIST 데이터

코딩계란 2024. 3. 4. 11:54
728x90

개요

  • 손으로 쓴 숫자들로 이루어진 대형 데이터베이스
  • 0 부터 9까지의 숫자 이미지 70,000 로 이루어진 데이터로 레이블이 존재
  • sklearn 을 이용해서 다운로드 받으면 data 속성에 피처가 존재하고 target 속성으로 레이블을 제공

 

MNIST 데이터 가져오기

from sklearn.datasets import fetch_openml

mnist = fetch_openml('mnist_784', version=1, as_frame=False)

X, y = mnist['data'], mnist['target']

print(X.shape)
print(y.shape)
#실행 결과
(70000, 784)
(70000,)

 

 

이미지 1개 출력 

#이미지 데이터 1개 가져오기
some_digit = X[0]

#이미지를 다시 2차원으로 변경
some_digit_image = some_digit.reshape(28, 28)

#출력
import matplotlib.pyplot as plt
import matplotlib as mpl

plt.imshow(some_digit_image, cmap=mpl.cm.binary)
plt.axis('off')
plt.show()

 

 

훈련데이터와 테스트 데이터 분리 

#레이블의 자료형을 정수로 변경
print(y.dtype)
y = y.astype(np.uint8)
print(y.dtype)

#레이블의 자료형을 정수로 변경
print(y.dtype)
y = y.astype(np.uint8)
print(y.dtype)
#결과
object
uint8
uint8
uint8
728x90