본문 바로가기

코딩으로 익히는 Python/모델링

[Python] 15. 이미지분류 : mnist, MLPClassifier

728x90
반응형
SMALL
import numpy as np 
import pandas as pd 
import seaborn as sns
import matplotlib.pyplot as plt
import mglearn
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import sklearn.metrics as m


from sklearn.datasets import fetch_openml

import warnings
warnings.simplefilter('ignore')
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')

 

x_data = mnist.data
y_data = mnist.target

x_data.shape # 이미지 픽셀값
[OUT] :

(70000, 784)

 

y_data
[OUT] :

array(['5', '0', '4', ..., '4', '5', '6'], dtype=object)

 

y_data.shape
[OUT] :

(70000,)

 

x_data[0].shape
[OUT] :

(784,)

 

# 3개만 시각화해보기
for i in range(3):
    plt.imshow(x_data[i].reshape(28,28)) # 원래 이미지가 (28,28)로 주어져있음
    plt.show()

 

x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, 
                    test_size=0.2, stratify=y_data)

 

model = MLPClassifier(verbose=1)
model.fit(x_train,y_train)
[OUT] :

Iteration 1, loss = 6.36916934
Iteration 2, loss = 0.90783269
Iteration 3, loss = 0.48415432
Iteration 4, loss = 0.34281886
Iteration 5, loss = 0.26263358
Iteration 6, loss = 0.21066357
Iteration 7, loss = 0.18172394
Iteration 8, loss = 0.15518834
Iteration 9, loss = 0.13670137
Iteration 10, loss = 0.13712496
# ...
# 중략
# ...
Iteration 84, loss = 0.03653212
Iteration 85, loss = 0.03835577
Training loss did not improve more than tol=0.000100 for 10 consecutive epochs. Stopping.
MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,
              beta_2=0.999, early_stopping=False, epsilon=1e-08,
              hidden_layer_sizes=(100,), learning_rate='constant',
              learning_rate_init=0.001, max_fun=15000, max_iter=200,
              momentum=0.9, n_iter_no_change=10, nesterovs_momentum=True,
              power_t=0.5, random_state=None, shuffle=True, solver='adam',
              tol=0.0001, validation_fraction=0.1, verbose=1, warm_start=False)

 

model.score(x_test,y_test)
[OUT] :

0.9646428571428571

 

y_pred = model.predict(x_test)

 

cm = m.confusion_matrix(y_test,y_pred)
plt.figure(figsize=(15,10))
sns.heatmap(cm,annot=True,cmap='Reds')
plt.show()

 

plt.imshow(x_test[0].reshape(28,28))
plt.show()

 

model.predict([x_test[0]])
[OUT] :

array(['4'], dtype='<U1')

연습문제

 

몇개나 틀렸는지 확인해보고, 맞은 확률 구하기


Solution

 

print(y_pred)
print(y_test)

cnt = 0
for i in range(len(y_test)):
    if y_test[i]!=y_pred[i]:
        cnt += 1
        
print(cnt)
print(1-cnt/len(y_test))
print(model.score(x_test,y_test))
[OUT] :

['4' '1' '3' ... '9' '7' '5']
['4' '1' '3' ... '9' '7' '5']
495
0.9646428571428571
0.9646428571428571

 

728x90
반응형
LIST