일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- python
- Database
- 해석
- 알고리즘기초
- 데이터
- python기초
- HeidiSQL
- SQL예제
- 데이터 분석
- pythone
- 크롤링(crawling)
- 선형회기모델
- 머신러닝
- 정확도
- 딥러닝
- 회귀모델
- sklearn
- keras
- 시각화
- 데이터베이스
- 데이터 가공
- Deep Learning
- tensorflow
- pandas
- 훈련
- 데이터 수집
- 예측
- MariaDB
- 데이터전처리
- 파이썬
- Today
- Total
코딩헤딩
Deep learning[딥러닝] 퍼셉트론(Perceptron) 본문
<퍼셉트론(Perceptron)>
- 인공신경망의 한 종류
- 주로 이진분류 또는 다중분류에 사용되는 초기 인공신경망 모델
- 종속변수가 연속형인 회귀에서는 사용되지 않음(분류에서만 사용)
- 퍼셉트론에는 단층 퍼셉트론과 다층 퍼셉트론이 있음
- 주로 다층 퍼셉트론이 성능이 좋음
<단층 퍼셉트론(Single-Layer Perceptron, SLF)>
- 입력층과 출력층으로만 구성되어 있음.
- 주로 이진 분류에 사용됨(성능이 낮은 경우, 다층 퍼셉트론으로 사용)
- 선형 활성화 함수를 사용
<층 퍼셉트론(Multi-Layer Perceptron, MLF)>
- 입력층, 은닉층(하나이상), 출력층으로 구성됨
- 주로 다중 분류에 사용됨(이진 분류도 가능)
- 여러 층(입력, 은닉, 출력)으로 이루어져 있다고 해서 "다층"이라고 칭함
- 은닉층에서는 비선형 활성화 함수를 사용할(시그모이드, 렐루, 등등...)
- 발전된 모델들이 현재 사용되는 모델임 현재도 계속 나오고 있음
* 라이브러리 정의
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
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
< 단층 퍼셉트론 모델 >
1. 독립변수와 종속변수로 분리하기
X = data.iloc[:, :-1]
y = data["class"]
X.shape, y.shape
결과 : ((6497, 3), (6497,))
2. 정규화하기
scaler = StandardScaler()
scaler.fit(X)
X_scaled = scaler.transform(X)
X_scaled.shape
결과 : (6497, 3)
3. 훈련 : 검증 : 테스트 = 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)
4. 단층 퍼셉트론 모델 생성하기
perceptron_model = Perceptron(random_state=42)
perceptron_model
5. 하이퍼파라미터 튜닝 매개변수 범위 설정
param_grid = {
### 학습율 : 보폭
"alpha" : [0.0001, 0.001, 0.01],
### 반복 횟수(epoxh)
"max_iter" : [100, 500, 10000]
}
param_grid
결과 : {'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 10000]}
6. 그리드 서치 cv 객체 생성하기
grid_search = GridSearchCV(perceptron_model, param_grid, cv=3, scoring="accuracy")
grid_search
7. 최적의 하이퍼 파라미터 찾기
grid_search.fit(X_train, y_train)
8. 최적의 하이퍼파라미터 출력
grid_search.best_params_
결과 : {'alpha': 0.0001, 'max_iter': 100}
9. 최적의 모델 / 최적의 모델로 훈련시키기
best_model = grid_search.best_estimator_
best_model.fit(X_train, y_train)
10. 훈련 및 검증 정확도 확인하기
score_train = best_model.score(X_train, y_train)
score_val = best_model.score(X_val, y_val)
score_train, score_val
결과 : (0.7637249871729092, 0.7474980754426482)
11. 예측하기
y_pred = best_model.predict(X_test)
y_pred
결과 : array([1., 1., 1., ..., 1., 1., 1.])
12. 정밀도, 재현율, f1-score, 매트릭스 확인하기
acc_score = accuracy_score(y_test, y_pred)
pre_score = precision_score(y_test, y_pred)
f1_score = f1_score(y_test, y_pred)
matrix = confusion_matrix(y_test, y_pred)
acc_score, pre_score, f1_score, matrix
결과 :
(0.7307692307692307,
0.7307692307692307,
0.8444444444444443,
array([[ 0, 350],
[ 0, 950]], dtype=int64))
13. 혼돈 행렬 시각화 하기
plt.figure(figsize=(8,4))
plt.title("confusion_matrix")
sns.heatmap(matrix, annot=True, fmt="d", cbar=False, cmap="Blues", xticklabels=["0", "1"], yticklabels=["0", "1"])
plt.show()
- 1을 1로 모두 맞추었지만 0은 0으로 단 한건도 맞추지 못했다.
< 디층 퍼셉트론 모델 >
* 데이터 전처리까지 동일
1. 다층 퍼셉트론 모델 생성하기
mlp_model = MLPClassifier(random_state=42)
mlp_model
2. 튜닝할 하이퍼 파라미터 설정하기
param_grid = {
"hidden_layer_sizes" : [(10,), (50,), (100,)],
"alpha" : [0.0001, 0.001, 0.01],
"max_iter" : [1000]
}
param_grid
결과 :
{'hidden_layer_sizes': [(10,), (50,), (100,)],
'alpha': [0.0001, 0.001, 0.01],
'max_iter': [1000]}
- hidden_layer_sizes : 은닉계층 정의
- (10,) : 은닉계층 1개 사용, 출력크기 10개를 의미한
- (10, 10) : 은닉계층 2개 사용, 각각의 출력 크기가 10개라는 의미
- (10,11,12) : 은닉계층 3개 사용, 각각의 출력크기는 10, 11, 12
3. 그리드 서치 cv 객체 생성하기
grid_search_mlp = GridSearchCV(mlp_model, param_grid, cv=3, scoring="accuracy")
grid_search_mlp
4. 최적의 하이퍼파라미터 찾기
grid_search_mlp.fit(X_train, y_train)
5. 최적의 하이퍼파라미터 출력
grid_search_mlp.best_params_
결과 : {'alpha': 0.01, 'hidden_layer_sizes': (100,), 'max_iter': 1000}
6. 최적의 모델 찾고 훈련
best_model_mlp = grid_search_mlp.best_estimator_
best_model_mlp.fit(X_train, y_train)
7. 훈련 및 검증 정확도 확인하기
score_train = best_model_mlp.score(X_train, y_train)
score_val = best_model_mlp.score(X_train, y_train)
score_train, score_val
결과 : (0.8755772190867112, 0.8755772190867112)
8. 예측하기
y_pred = best_model_mlp.predict(X_test)
y_pred
결과 : array([1., 0., 0., ..., 0., 0., 1.])
9. 성능 평가하기
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
precision, recall
결과 : (0.7307692307692307, 1.0)
10. 평가 매트릭스(혼동행렬)
conf_matrix = confusion_matrix(y_test, y_pred)
conf_matrix
결과 :
array([[ 0, 350],
[ 0, 950]], dtype=int64)
11. 혼돈 행렬 시각화 하기
plt.figure(figsize=(8,4))
plt.title("confusion_matrix")
sns.heatmap(conf_matrix, annot=True, fmt="d", cbar=False, cmap="Blues", xticklabels=["0", "1"], yticklabels=["0", "1"])
plt.show()
'머신러닝 | 딥러닝' 카테고리의 다른 글
Deep learning[딥러닝] LSTM GRU (0) | 2024.01.08 |
---|---|
Deep learning[딥러닝] 심플순환신경망(simple RNN) (1) | 2024.01.08 |
Deep learning[딥러닝] 모델 생성 함수 및 손실률/정확도 시각화 (0) | 2024.01.05 |
Deep learning[딥러닝] 신경망계층 성능향상 (1) | 2024.01.04 |
Deep learning[딥러닝] 신경망계층 추가 (1) | 2024.01.03 |