본문 바로가기

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

[Python] 19. MLP : pima-indians 예제

728x90
반응형
SMALL
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.model_selection import validation_curve
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
import sklearn.metrics as metrics
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier, AdaBoostClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.neural_network import MLPClassifier

import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams["font.family"]="Malgun Gothic"
matplotlib.rcParams["axes.unicode_minus"]= False

import warnings
warnings.simplefilter('ignore')

 

데이터 불러오기 (pd.read_csv)

python 파일 경로에 만든 후 data5폴더에 다음의 'pima-indians-diabetes.data.csv' 파일 넣어놓기

pima-indians-diabetes.data.csv
0.02MB


df = pd.read_csv('data5/pima-indians-diabetes.data.csv')
df.head(3)

 

x_data = df[['Glucose', 'BMI', 'Age', 'Pregnancies', 'DiabetesPedigreeFunction', 'Insulin']]
y_data = df['Outcome']

 

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

 

m_pip = make_pipeline( StandardScaler(),
                    MLPClassifier( early_stopping=True,verbose=1 ) )
m_pip.fit( x_train, y_train)
[OUT] :

Iteration 1, loss = 0.59713529
Validation score: 0.655172
Iteration 2, loss = 0.58055930
Validation score: 0.655172
Iteration 3, loss = 0.56509129
Validation score: 0.672414
Iteration 4, loss = 0.55120574
Validation score: 0.655172
Iteration 5, loss = 0.53954608
Validation score: 0.655172
Iteration 6, loss = 0.52907695
Validation score: 0.655172
Iteration 7, loss = 0.51972209
Validation score: 0.655172
Iteration 8, loss = 0.51195284
Validation score: 0.655172
Iteration 9, loss = 0.50450803
Validation score: 0.655172
Iteration 10, loss = 0.49833271
Validation score: 0.655172
Iteration 11, loss = 0.49305098
Validation score: 0.655172
Iteration 12, loss = 0.48813886
Validation score: 0.655172
Iteration 13, loss = 0.48434923
Validation score: 0.655172
Iteration 14, loss = 0.48021801
Validation score: 0.655172
Validation score did not improve more than tol=0.000100 for 10 consecutive epochs. Stopping.
Pipeline(memory=None,
         steps=[('standardscaler',
                 StandardScaler(copy=True, with_mean=True, with_std=True)),
                ('mlpclassifier',
                 MLPClassifier(activation='relu', alpha=0.0001,
                               batch_size='auto', beta_1=0.9, beta_2=0.999,
                               early_stopping=True, 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))],
         verbose=False)

 

m_pip.named_steps['mlpclassifier'].loss_curve_
[OUT] :

[0.5971352911437267,
 0.5805592979414727,
 0.5650912925087086,
 0.5512057365194039,
 0.539546081635644,
 0.529076952618531,
 0.5197220933016252,
 0.5119528442356747,
 0.5045080254043071,
 0.49833270529782847,
 0.49305097722986374,
 0.48813885512602495,
 0.4843492278342033,
 0.48021800726459146]

 

m_pip.named_steps['mlpclassifier'].validation_scores_
[OUT] :

[0.6551724137931034,
 0.6551724137931034,
 0.6724137931034483,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034,
 0.6551724137931034]

 

m_mlp = MLPClassifier( early_stopping=True,verbose=1 )
m_mlp.fit( x_train, y_train)
[OUT] :

Iteration 1, loss = 3.98015101
Validation score: 0.517241
Iteration 2, loss = 2.25713251
Validation score: 0.689655
Iteration 3, loss = 1.31454145
Validation score: 0.620690
Iteration 4, loss = 1.47540483
Validation score: 0.637931
Iteration 5, loss = 1.62656677
Validation score: 0.637931
Iteration 6, loss = 1.49750096
Validation score: 0.724138
Iteration 7, loss = 1.21639955
Validation score: 0.706897
Iteration 8, loss = 1.08005393
Validation score: 0.637931
Iteration 9, loss = 1.11488639
Validation score: 0.655172
Iteration 10, loss = 1.10376751
Validation score: 0.672414
Iteration 11, loss = 0.94993128
Validation score: 0.586207
Iteration 12, loss = 0.93377286
Validation score: 0.689655
Iteration 13, loss = 0.91425011
Validation score: 0.724138
Iteration 14, loss = 0.88024901
Validation score: 0.655172
Iteration 15, loss = 0.80254153
Validation score: 0.655172
Iteration 16, loss = 0.79586528
Validation score: 0.672414
Iteration 17, loss = 0.76555140
Validation score: 0.672414
Validation score 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=True, 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)

 

m_mlp.loss_curve_ # 학습간 cost 값.
[OUT] :

[3.9801510132675086,
 2.2571325116214713,
 1.3145414510187858,
 1.4754048284427326,
 1.6265667660007088,
 1.4975009598962057,
 1.2163995464832993,
 1.0800539290118272,
 1.1148863880131452,
 1.1037675132478248,
 0.9499312766724436,
 0.9337728642736134,
 0.9142501089366765,
 0.8802490133405271,
 0.8025415251850463,
 0.7958652779161396,
 0.7655513989338698]

 

m_mlp.validation_scores_
[OUT] :

[0.5172413793103449,
 0.6896551724137931,
 0.6206896551724138,
 0.6379310344827587,
 0.6379310344827587,
 0.7241379310344828,
 0.7068965517241379,
 0.6379310344827587,
 0.6551724137931034,
 0.6724137931034483,
 0.5862068965517241,
 0.6896551724137931,
 0.7241379310344828,
 0.6551724137931034,
 0.6551724137931034,
 0.6724137931034483,
 0.6724137931034483]

 

plt.plot( m_mlp.loss_curve_ )
plt.plot( m_mlp.validation_scores_)
plt.show()


728x90
반응형
LIST