728x90
반응형
In [1]:
import pandas as pd
from sklearn.preprocessing import StandardScaler
In [ ]:
"""<퍼셉트론(Perceptron)>
- 인공신경망의 종류
- 주로 이진분류 또는 다중분류에 사용되는 초기 인공신경망 모델
- 종속변수가 연속형인 회귀에서는 사용되지 않음(분류에서만 사용)
- 퍼셉트론에는 단층 퍼셉트론과 다층 퍼셉트론이 있음
- 주로 다층 퍼셉트론이 성능이 좋음
"""
"""<단층 퍼셉트론 (Single-Layer perceptron, SLF> ANN
-입력층과 출력층으로만 구성되어 있음
-주로 이진 분류에 사용됨, (성능이 낮은 경우 ,다층 퍼셉트론으로 사용)
-선형 활성화 함수 사용
<다층 퍼셉트론(Multi-Layer Perceptron , MLP> DNN
-입력층, 은닉층 (하나이상) , 출력층으로 구성됨
- 주로 다중 분류에 사용됨 (이진분류도 가능)
-단층 퍼셉트론 보다 높은 성능을 나타냄
-여러 층 (입력, 은닉, 출력)으로 이루어져 있다고 해서 "다층"이라고 칭함
-은닉층에는 비선형 활성화 함수를 사용함(시그모이드, 렐루 , 등...)
- 발전된 모델들이 현재 사용되는 모델들... 계속 나오고 있음
"""
"""
* 인공신경망(Artificial Neural Network, ANN)
- 계층이 1개인 경우 또는 인닉계층이 없는 경우
* 심층신경망(Deep Neural network, DNN)
- 은닉계층을 가지고 있는 경우
"""
In [3]:
"""라이브러리 정의"""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
"""단층 퍼셉트론 모델"""
from sklearn.linear_model import Perceptron
"""다층 퍼셉트론 모델"""
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
In [4]:
"""데이터 불러들이기"""
data = pd.read_csv("./data/08_wine.csv")
data.head(1)
Out[4]:
alcohol | sugar | pH | class | |
---|---|---|---|---|
0 | 9.4 | 1.9 | 3.51 | 0.0 |
In [6]:
X = data.iloc[:,:-1]
y= data["class"]
X.shape, y.shape
Out[6]:
((6497, 3), (6497,))
In [7]:
"""정규화하기"""
scaler = StandardScaler()
scaler.fit(X)
X_scaled = scaler.transform(X)
X_scaled
Out[7]:
array([[-0.91546416, -0.7447781 , 1.81308951], [-0.58006813, -0.59764007, -0.11507303], [-0.58006813, -0.66069923, 0.25811972], ..., [-0.91546416, -0.89191614, -1.42124765], [ 1.9354021 , -0.91293585, 0.75571005], [ 1.09691202, -0.97599501, 0.25811972]])
In [12]:
"""훈련:검증:테스트 = 6:2:2로 분리하기 """
X_train, X_temp, y_train, y_temp = train_test_split(X_scaled, y, test_size=0.4, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)
print(X_train.shape, y_train.shape,"\n", X_val.shape, y_val.shape,"\n", X_test.shape, y_test.shape)
(3898, 3) (3898,) (1299, 3) (1299,) (1300, 3) (1300,)
단층 퍼셉트론¶
In [13]:
"""단층 퍼셉트론 모델 생성하기"""
perceptron_model = Perceptron(random_state=42)
perceptron_model
Out[13]:
Perceptron(random_state=42)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Perceptron(random_state=42)
In [15]:
"""하이퍼 파라메터 튜닝 할 매개변수 범위 설정"""
param_grid = {
### 학습율 : 보폭
"alpha" : [0.0001, 0.001, 0.01],
### 반복 횟수 (=epoch 계념)
"max_iter": [100,500,1000]
}
param_grid
Out[15]:
{'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 1000]}
In [16]:
"""그리드 서치 cv 객체 생성하기"""""
grid_search = GridSearchCV(perceptron_model, param_grid, cv=3, scoring="accuracy")
grid_search
Out[16]:
GridSearchCV(cv=3, estimator=Perceptron(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 1000]}, scoring='accuracy')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=Perceptron(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 1000]}, scoring='accuracy')
Perceptron(random_state=42)
Perceptron(random_state=42)
In [17]:
"""최적의 하이퍼파라메터 찾기"""
grid_search.fit(X_train,y_train)
Out[17]:
GridSearchCV(cv=3, estimator=Perceptron(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 1000]}, scoring='accuracy')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=Perceptron(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 1000]}, scoring='accuracy')
Perceptron(random_state=42)
Perceptron(random_state=42)
In [ ]:
"""최적의 하이퍼파라메터 출력"""
grid_search.best_params_
In [21]:
"""최적의 모델"""
best_model = grid_search.best_estimator_
best_model
Out[21]:
Perceptron(max_iter=100, random_state=42)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Perceptron(max_iter=100, random_state=42)
In [22]:
"""최적의 모델로 훈련시키기"""
best_model.fit(X_train, y_train)
Out[22]:
Perceptron(max_iter=100, random_state=42)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Perceptron(max_iter=100, random_state=42)
In [24]:
"""훈련 및 검증 정확도 확인하기"""
score_train = best_model.score(X_train, y_train)
score_val = best_model.score(X_val, y_val)
score_train, score_val
Out[24]:
(0.7637249871729092, 0.7474980754426482)
In [37]:
"""정밀도 ,재현율 , f1-score, 매트릭스 확인하기"""
y_pred = best_model.predict(X_test)
# 정밀도
precision = precision_score(y_test, y_pred)
# 재현율
recall =recall_score(y_test, y_pred)
# f1 score(보통 재현율과 비슷하게 나온다.)
f1 = f1_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
precision,recall,f1, cm
# 분류할때는 매트릭스까지 확인해 봐야 한다.
# 재현율과 f1이 차이가 난다는 것은 긍정오류나 부정오류가 있다는 뜻
Out[37]:
(0.7307692307692307, 1.0, 0.8444444444444443, array([[ 0, 350], [ 0, 950]], dtype=int64))
In [43]:
"""혼동행렬 시각화"""
plt.figure(figsize=(8,4))
plt.title("Confusion Matrix")
# cbar 컬러바
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=["0","1"], yticklabels=["0","1"], cbar=False)
plt.show()
다층퍼셉트론¶
In [44]:
"""다층퍼셉트론 모델생성"""
mlp_model = MLPClassifier(random_state=42)
mlp_model
Out[44]:
MLPClassifier(random_state=42)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MLPClassifier(random_state=42)
In [45]:
"""튜닝할 하이퍼 파라메터 설정하기"""
param_grid = {
### hidden_layer_sizes : 은닉계층 정의
# (10,) : 은닉계층 1개 사용, 출력크기 10개 의미
# (10,10): 은닉계층 2개 사용, 각각의 출력크기가 10개라는 의미
# (10,11,12) : 은닉계층 3개 사용, 각각의 출력크기는 10,11,12
"hidden_layer_sizes" : [(10,),(50,),(100,)],
"alpha" : [0.0001, 0.001, 0.01],
"max_iter" :[1000]
}
param_grid
Out[45]:
{'hidden_layer_sizes': [(10,), (50,), (100,)], 'alpha': [0.0001, 0.001, 0.01], 'max_iter': [1000]}
In [46]:
"""그리드서치CV 객체 생성하기"""
grid_search_mlp = GridSearchCV(mlp_model, param_grid, cv=3, scoring="accuracy")
grid_search_mlp
Out[46]:
GridSearchCV(cv=3, estimator=MLPClassifier(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'hidden_layer_sizes': [(10,), (50,), (100,)], 'max_iter': [1000]}, scoring='accuracy')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=MLPClassifier(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'hidden_layer_sizes': [(10,), (50,), (100,)], 'max_iter': [1000]}, scoring='accuracy')
MLPClassifier(random_state=42)
MLPClassifier(random_state=42)
In [47]:
"""최적의 하이퍼파라메터 """
grid_search_mlp.fit(X_train, y_train)
C:\Users\user\anaconda3\envs\gj_env_dl\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1000) reached and the optimization hasn't converged yet. warnings.warn( C:\Users\user\anaconda3\envs\gj_env_dl\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1000) reached and the optimization hasn't converged yet. warnings.warn( C:\Users\user\anaconda3\envs\gj_env_dl\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1000) reached and the optimization hasn't converged yet. warnings.warn(
Out[47]:
GridSearchCV(cv=3, estimator=MLPClassifier(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'hidden_layer_sizes': [(10,), (50,), (100,)], 'max_iter': [1000]}, scoring='accuracy')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=MLPClassifier(random_state=42), param_grid={'alpha': [0.0001, 0.001, 0.01], 'hidden_layer_sizes': [(10,), (50,), (100,)], 'max_iter': [1000]}, scoring='accuracy')
MLPClassifier(random_state=42)
MLPClassifier(random_state=42)
In [50]:
"""최적의 하이퍼 파라메터 출력"""
grid_search_mlp.best_params_
Out[50]:
{'alpha': 0.01, 'hidden_layer_sizes': (100,), 'max_iter': 1000}
In [52]:
"""최적의 모델"""
best_model_mlp= grid_search_mlp.best_estimator_
best_model_mlp
Out[52]:
MLPClassifier(alpha=0.01, max_iter=1000, random_state=42)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MLPClassifier(alpha=0.01, max_iter=1000, random_state=42)
In [55]:
"""훈련및 검증 정확도 확인하기"""
score_train= best_model_mlp.score(X_train,y_train)
score_val= best_model_mlp.score(X_val,y_val)
score_train, score_val
Out[55]:
(0.8755772190867112, 0.8683602771362586)
In [60]:
"""예측하기"""
y_pred = best_model_mlp.predict(X_test)
precision = precision_score(y_test,y_pred)
recall = recall_score(y_test,y_pred)
f1 = f1_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
precision, recall, f1, cm
Out[60]:
(0.8875255623721882, 0.9136842105263158, 0.900414937759336, array([[240, 110], [ 82, 868]], dtype=int64))
In [62]:
"""평가 매트릭스 (혼동행렬)"""
cm
Out[62]:
array([[240, 110], [ 82, 868]], dtype=int64)
In [63]:
"""혼동 행렬 시각화"""
plt.figure(figsize=(8,4))
plt.title("Confusion Matrix MLP")
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=["0","1"], yticklabels=["0","1"], cbar=False)
plt.show()
In [ ]:
728x90
반응형
'딥러닝' 카테고리의 다른 글
RNN 응용 규칙기반 챗봇 (1) | 2024.01.08 |
---|---|
심플순환신경망(Simple_RNN (0) | 2024.01.05 |
DNN_분류데이터사용 (0) | 2024.01.05 |
[딥러닝]인공신경망_훈련모델_맛보기 (2) | 2024.01.03 |
[딥러닝]심층신경망_훈련_및_성능향상 (0) | 2024.01.03 |