상세 컨텐츠

본문 제목

Artificial Neural Network(ANN)-Neural Network Model (다중분류)

인공지능/딥러닝

by 2^7 2022. 6. 13. 14:56

본문

Neural Network Model (다중분류)

1. 탐색적 데이터 분석

1-1. 빈도분석

import seaborn as sns

DF = sns.load_dataset('iris')
DF.species.value_counts()

virginica 50

setosa 50

versicolor 50

Name: species, dtype: int64


1-2. 분포 시각화

import matplotlib.pyplot as plt
import seaborn as sns

sns.pairplot(hue = 'species', data = DF)
plt.show()


2.Data Preprocessing

2-1. Data Set

X = DF[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
y = DF['species']

2-2.Train & Test Split

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    test_size = 0.3,
                                                    random_state = 2045)

print('Train Data : ', X_train.shape, y_train.shape)
print('Test Data : ', X_test.shape, y_test.shape)

Train Data : (105, 4) (105,)

Test Data : (45, 4) (45,)

 


3. Modeling

3-1. Train_Data로 모델 생성

  • hidden_layer_sizes : 은닉층 노드의 개수
  • activation : 활성화 함수
  • solver : 최적화 기법
  • max_iter : 학습 반복 횟수
from sklearn.neural_network import MLPClassifier

Model_NN = MLPClassifier(hidden_layer_sizes = (5),
                         activation = 'logistic',
                         solver ='adam', 
                         max_iter = 5000,
                         random_state = 2045)

Model_NN.fit(X_train, y_train)


3-2. Test_Data에 Model 적용

y_hat = Model_NN.predict(X_test)

3-3.Confusion Matrix

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test, y_hat)


3-4. Accuracy

from sklearn.metrics import accuracy_score

print('%.8f' % accuracy_score(y_test, y_hat))

1.00000000


3-5. Classification Report

from sklearn.metrics import classification_report

print(classification_report(y_test, y_hat, 
                            target_names = ['setosa', 'versicolor', 'virginica'],
                            digits = 5))

728x90

관련글 더보기