AI/케글(Kaggle)

Digit-recognizer

나굥 2021. 2. 15. 11:04
728x90

Digit-recognizer

- 손으로 쓴 한 자릿수의 이미지를 촬영하여 그 숫자가 무엇인지 결정하는 문제


  • train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
  • test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')

 

1. 데이터의 형태 살펴보기

  • train_data.shape / test_data.shape

 

2. 데이터의 라벨값을 CNN에 넣을수 없기 때문에 라벨값 분리

  • train_images = train_data.drop("label", axis=1)
  • train_labels = train_data["label"]

 

3. 신경망은 데이터를 Numpy Arrays의 형태로 입력하므로 데이터프레임 Numpy 어레이로 변환

  • train_images.to_numpy()
  • train_labels.to_numpy()
  • test_data.to_numpy()

 

4. 훈련을 시작하기 전에 데이터를 네트워크에 맞는 크기로 바꾸고 모든 값을 0과 1 사이로 스케일을 조정해야한다. 데이터 값은 숫자 배열의 값을 스케일링하기 위해 255로 나눕니다. 

  • train_images = train_images / 255
  • test_data=test_data / 255

 

5. 레이블을 범주형으로 인코딩

  • from keras.utils import to_categorical
  • train_labels = to_categorical(train_labels)

 

6. 신경망은 완전연결된 신경망 층인 Dense 층 2개가 연속되도록 한다. 마지막층은 10개의 확률점수가 들어있는 배열을 반환하는 소프트맥스 층이다.

  • from keras import models from keras
  • import layers network = models.Sequential()
  • network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
  • network.add(layers.Dense(10, activation='softmax'))

 

7. 손실함수, 옵티마이저,정확도를 고려

  • network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

 

8. 케라스에서는 fit 메서드를 호출하여 훈련 데이터에 모델을 학습시킨다

  • history=network.fit(train_images, train_labels,epochs=5,batch_size=128)

 

9. 모델을 fit 한 결과를 그래프로 보여주도록, x축을 epoch(에포크), y축을 accuracy 또는 loss 로 나타낸다.

  • # summarize history for accuracy
  • plt.plot(history.history['accuracy'])
  • plt.title('model accuracy')
  • plt.ylabel('accuracy')
  • plt.xlabel('epoch')
  • plt.legend(['train', 'test'], loc='upper left')
  • plt.show()
  • # summarize history for loss
  • plt.plot(history.history['loss'])
  • plt.title('model loss')
  • plt.ylabel('loss')
  • plt.xlabel('epoch')
  • plt.legend(['train', 'test'], loc='upper left')
  • plt.show()

 

케글상에 submit

정확도 97%

 

728x90