3.다중분류문제_1_아이리스
- -
layout: single
title: "jupyter notebook 변환하기!"
categories: coding
tag: [python, blog, jekyll]
toc: true
author_profile: false
아이리스 품종 다중분류 예측 문제
import warnings
warnings.filterwarnings(action='ignore')
1. scikit-learn 패키지는 머신러닝 교육을 위한 최고의 파이썬 패키지입니다.
scikit-learn를 별칭(alias) sk로 임포트하는 코드를 작성하고 실행하세요.
# 여기에 답안코드를 작성하세요.
import sklearn as sk
2. Pandas는 데이터 분석을 위해 널리 사용되는 파이썬 라이브러리입니다.
Pandas를 사용할 수 있도록 별칭(alias)을 pd로 해서 불러오세요.
# 여기에 답안코드를 작성하세요.
import pandas as pd
3. 모델링을 위해 분석 및 처리할 데이터 파일을 읽어오려고 합니다.
Pandas함수로 데이터 파일을 읽어 데이터프레임 변수명 df에 할당하는 코드를 작성하세요.
path = 'https://raw.githubusercontent.com/khw11044/csv_dataset/master/iris.csv'
df = pd.read_csv(path)
df.head()
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
데이터 설명
아이리스 품종 중 Setosa, Versicolor, Virginica 분류
꽃받침(Sepal)과 꽃잎(Petal)의 길이(Length)와 너비(Width)
Sepal.Length: 꽃받침의 길이
Sepal.Width: 꽃받침의 너비
Petal.Length: 꽃잎의 길이
Petal.Width: 꽃잎의 너비
다음 문항을 풀기 전에 아래 코드를 실행하세요.
# 여기에 답안코드를 작성하세요.
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Malgun Gothic'
df['Species'].value_counts()
Species setosa 50 versicolor 50 virginica 50 Name: count, dtype: int64
4. Species(품종)별 컬럼들의 분포를 알고 싶습니다
Species(품종)별 컬럼들의 분포도를 그리세요
Seaborn을 활용하세요.
histplot 함수를 사용하세요
컬럼들 모도 똑같이
plt.figure(figsize=(16,4))
plt.subplot(1,4,1)
sns.histplot(x='Sepal.Length', data = df, bins=30, kde = True)
plt.title('Sepal.Length')
plt.subplot(1,4,2)
sns.histplot(x='Sepal.Length', data=df[df['Species']=='setosa'], bins=30, kde = True)
plt.title('setosa/Sepal.Length')
plt.subplot(1,4,3)
sns.histplot(x='Sepal.Length', data=df[df['Species']=='versicolor'], bins=30, kde = True)
plt.title('versicolor/Sepal.Length')
plt.subplot(1,4,4)
sns.histplot(x='Sepal.Length', data=df[df['Species']=='virginica'], bins=30, kde = True)
plt.title('virginica/Sepal.Length')
plt.show()
# 어릴 수록 살아남았다
plt.figure(figsize=(16,4))
plt.subplot(1,4,1)
sns.histplot(x='Sepal.Width', data = df, bins=30, kde = True)
plt.title('Sepal.Width')
plt.subplot(1,4,2)
sns.histplot(x='Sepal.Width', data=df[df['Species']=='setosa'], bins=30, kde = True)
plt.title('setosa/Sepal.Width')
plt.subplot(1,4,3)
sns.histplot(x='Sepal.Width', data=df[df['Species']=='versicolor'], bins=30, kde = True)
plt.title('versicolor/Sepal.Width')
plt.subplot(1,4,4)
sns.histplot(x='Sepal.Width', data=df[df['Species']=='virginica'], bins=30, kde = True)
plt.title('virginica/Sepal.Width')
plt.show()
# 어릴 수록 살아남았다
plt.figure(figsize=(16,4))
plt.subplot(1,4,1)
sns.histplot(x='Petal.Length', data = df, bins=30, kde = True)
plt.title('Petal.Length')
plt.subplot(1,4,2)
sns.histplot(x='Petal.Length', data=df[df['Species']=='setosa'], bins=30, kde = True)
plt.title('setosa/Petal.Length')
plt.subplot(1,4,3)
sns.histplot(x='Petal.Length', data=df[df['Species']=='versicolor'], bins=30, kde = True)
plt.title('versicolor/Petal.Length')
plt.subplot(1,4,4)
sns.histplot(x='Petal.Length', data=df[df['Species']=='virginica'], bins=30, kde = True)
plt.title('virginica/Petal.Length')
plt.show()
# 어릴 수록 살아남았다
5. 각 컬럼의 상관관계를 알아 보려고 합니다. 상관관계를 통해 필요없는 컬럼을 제거할 수 있습니다.
상관관계 히트맵을 그리세요.
numeric_only를 통해 숫자형 컬럼들만 나타내세요
cmap은 Blues로 하세요
cbar는 보이지 않습니다
소수점은 소수점 3번째 자리 까지 나타내세요
df['Species'] = df['Species'].astype('category')
df['Species'] = df['Species'].cat.codes
df.head()
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | 0 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | 0 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | 0 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | 0 |
# 여기에 답안코드를 작성하세요.
# 상관관계 시각화
plt.figure(figsize=(6,6))
sns.heatmap(df.corr(numeric_only=True),
annot=True,
cmap='Blues',
cbar=False, # 옆에 칼라 바 제거
square=True,
fmt='.3f', # 소수점
annot_kws={'size':9}
)
plt.show()
# PassengerId, Age, SibSp, Parch 컬럼은 제거한다.
6. 모델링 성능을 제대로 얻기 위해서 결측치 처리는 필수입니다.
아래 가이드를 따라 결측치 처리하세요.
대상 데이터프레임: df
결측치를 확인하는 코드를 작성하세요.
결측치가 있는 행(raw)를 삭제 하세요.
전처리 반영된 결과를 새로운 데이터프레임 변수명 df_na에 저장하세요.
# NaN 값 확인
df.isnull().sum()
Sepal.Length 0 Sepal.Width 0 Petal.Length 0 Petal.Width 0 Species 0 dtype: int64
7. 훈련과 검증 각각에 사용할 데이터셋을 분리하려고 합니다.
Species(품종) 컬럼을 label값 y로, 나머지 컬럼을 feature값 X로 할당한 후 훈련데이터셋과 검증데이터셋으로 분리하세요.
대상 데이터프레임: df_preset
훈련과 검증 데이터셋 분리
훈련 데이터셋 label: y_train, 훈련 데이터셋 Feature: X_train
검증 데이터셋 label: y_valid, 검증 데이터셋 Feature: X_valid
훈련 데이터셋과 검증데이터셋 비율은 80:20
random_state: 42
Scikit-learn의 train_test_split 함수를 활용하세요.
스케일링 수행
sklearn.preprocessing의 MinMaxScaler 함수 사용
훈련데이터셋의 Feature는 MinMaxScaler의 fit_transform 함수를 활용하여 X_train 변수로 할당
검증데이터셋의 Feature는 MinMaxScaler의 transform 함수를 활용하여 X_valid 변수로 할당
# 여기에 답안코드를 작성하세요.
from sklearn.model_selection import train_test_split
target = 'Species'
x = df.drop(target, axis=1)
y = df[target]
X_train, X_valid, y_train, y_valid = train_test_split(x,y, test_size=0.2, random_state=42)
print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape)
(120, 4) (30, 4) (120,) (30,)
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
8. Species(품종)을 예측하는 머신러닝 모델을 만들려고 합니다.
아래 가이드에 따라 다음 모델을 만들고 학습을 진행하세요.
가장 성능이 좋은 모델 이름을 답안8 변수에 저장하세요
- 예. 답안8 = 'KNeighborsClassifier' 혹은 'DecisionTreeClassifier' 혹은 'LogisticRegression' 혹은 'RandomForestClassifier' 등등
# 1단계: 불러오기
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score
models = {
"KNeighborsClassifier": {"model":KNeighborsClassifier(n_neighbors=5)},
"DecisionTreeClassifier": {"model":DecisionTreeClassifier()},
"LogisticRegression": {"model":LogisticRegression()},
"RandomForestClassifier": {"model":RandomForestClassifier()},
"XGBClassifier": {"model":XGBClassifier()},
"LGBMClassifier": {"model":LGBMClassifier(verbose=-1)}
}
from time import perf_counter
# Train모델 학습
for name, model in models.items():
model = model['model']
start = perf_counter()
history = model.fit(X_train, y_train)
# 학습시간과 val_accuracy 저장
duration = perf_counter() - start
duration = round(duration,2)
models[name]['perf'] = duration
y_train_pred = model.predict(X_train)
y_val_pred = model.predict(X_valid)
train_score = round(model.score(X_train, y_train),4)
val_score = round(model.score(X_valid, y_valid),4)
models[name]['train_score'] = train_score
models[name]['val_score'] = val_score
print(f"{name:20} trained in {duration} sec, train_score: {train_score}. val_score: {val_score}")
# Create a DataFrame with the results
models_result = []
for name, v in models.items():
models_result.append([ name, models[name]['val_score'],
models[name]['perf']])
df_results = pd.DataFrame(models_result,
columns = ['model','val_score','Training time (sec)'])
df_results.sort_values(by='val_score', ascending=False, inplace=True)
df_results.reset_index(inplace=True,drop=True)
df_results
KNeighborsClassifier trained in 0.0 sec, train_score: 0.9583. val_score: 1.0 DecisionTreeClassifier trained in 0.0 sec, train_score: 1.0. val_score: 1.0 LogisticRegression trained in 0.0 sec, train_score: 0.925. val_score: 0.9667 RandomForestClassifier trained in 0.12 sec, train_score: 1.0. val_score: 1.0 XGBClassifier trained in 0.04 sec, train_score: 1.0. val_score: 1.0 LGBMClassifier trained in 0.02 sec, train_score: 1.0. val_score: 1.0
model | val_score | Training time (sec) | |
---|---|---|---|
0 | KNeighborsClassifier | 1.0000 | 0.00 |
1 | DecisionTreeClassifier | 1.0000 | 0.00 |
2 | RandomForestClassifier | 1.0000 | 0.12 |
3 | XGBClassifier | 1.0000 | 0.04 |
4 | LGBMClassifier | 1.0000 | 0.02 |
5 | LogisticRegression | 0.9667 | 0.00 |
def check_performance_for_model(df_results):
plt.figure(figsize = (15,5))
sns.barplot(x = 'model', y = 'val_score', data = df_results)
plt.title('ACC (%) on the Test set', fontsize = 15)
plt.ylim(0,1.2)
plt.xticks(rotation=90)
plt.show()
check_performance_for_model(df_results)
답안8='DecisionTreeClassifier'
from sklearn.metrics import roc_auc_score, accuracy_score
model = DecisionTreeClassifier()
# 3단계: 학습하기
model.fit(X_train, y_train)
# 4단계: 예측하기
y_pred = model.predict(X_valid)
# 5단계 평가하기
print(classification_report(y_valid, y_pred))
print('Acc Score :', accuracy_score(y_valid, y_pred))
precision recall f1-score support 0 1.00 1.00 1.00 10 1 1.00 1.00 1.00 9 2 1.00 1.00 1.00 11 accuracy 1.00 30 macro avg 1.00 1.00 1.00 30 weighted avg 1.00 1.00 1.00 30 Acc Score : 1.0
9. Species(품종)을 예측하는 딥러닝 모델을 만들려고 합니다.
아래 가이드에 따라 모델링하고 학습을 진행하세요.
Tensoflow framework를 사용하여 딥러닝 모델을 만드세요.
히든레이어(hidden layer) 2개이상으로 모델을 구성하세요.
손실함수는 sparse_categorical_crossentropy 사용하세요.
하이퍼파라미터 epochs: 100, batch_size: 16으로 설정해주세요.
각 에포크마다 loss와 metrics 평가하기 위한 데이터로 X_valid, y_valid 사용하세요.
학습정보는 history 변수에 저장해주세요
import tensorflow as tf
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Activation, Dropout, BatchNormalization
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.utils import to_categorical
from keras.optimizers import Adam
# 규제를 위해 필요한 함수 불러오기
from tensorflow.keras.regularizers import l1, l2
tf.random.set_seed(1)
nfeatures = X_train.shape[1]
outputs = len(y_valid.unique())
# Sequential 모델 만들기
model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(nfeatures,), kernel_regularizer = l1(0.01)))
model.add(Dense(16, activation='relu', kernel_regularizer = l1(0.01)))
model.add(Dense(outputs, activation= 'softmax'))
model.compile(optimizer=Adam(learning_rate=0.01), loss='sparse_categorical_crossentropy',metrics=['acc'])
# es = EarlyStopping(monitor='val_loss', patience=4, mode='min', verbose=1) # val_loss
history = model.fit(X_train, y_train,
batch_size=16,
epochs=100,
# callbacks=[es],
validation_data=(X_valid, y_valid),
verbose=1).history
Epoch 1/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 19ms/step - acc: 0.3737 - loss: 2.2113 - val_acc: 0.7000 - val_loss: 1.8303 Epoch 2/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.6438 - loss: 1.7456 - val_acc: 0.8667 - val_loss: 1.4972 Epoch 3/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.7135 - loss: 1.4258 - val_acc: 0.9000 - val_loss: 1.2199 Epoch 4/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.8479 - loss: 1.1587 - val_acc: 0.7000 - val_loss: 0.9891 Epoch 5/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.6864 - loss: 0.9592 - val_acc: 0.7000 - val_loss: 0.8371 Epoch 6/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.6608 - loss: 0.8298 - val_acc: 0.7333 - val_loss: 0.7154 Epoch 7/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.7440 - loss: 0.7199 - val_acc: 0.9333 - val_loss: 0.6398 Epoch 8/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9447 - loss: 0.6418 - val_acc: 0.9667 - val_loss: 0.5881 Epoch 9/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9717 - loss: 0.5892 - val_acc: 0.9667 - val_loss: 0.5408 Epoch 10/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9717 - loss: 0.5424 - val_acc: 0.9667 - val_loss: 0.4945 Epoch 11/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9731 - loss: 0.5010 - val_acc: 0.9667 - val_loss: 0.4578 Epoch 12/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9771 - loss: 0.4691 - val_acc: 0.9667 - val_loss: 0.4298 Epoch 13/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9771 - loss: 0.4427 - val_acc: 0.9667 - val_loss: 0.4088 Epoch 14/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9760 - loss: 0.4241 - val_acc: 0.9667 - val_loss: 0.3933 Epoch 15/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9800 - loss: 0.4092 - val_acc: 0.9667 - val_loss: 0.3807 Epoch 16/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9800 - loss: 0.3970 - val_acc: 0.9667 - val_loss: 0.3704 Epoch 17/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9800 - loss: 0.3868 - val_acc: 0.9667 - val_loss: 0.3627 Epoch 18/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9800 - loss: 0.3781 - val_acc: 0.9667 - val_loss: 0.3551 Epoch 19/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9800 - loss: 0.3701 - val_acc: 0.9667 - val_loss: 0.3475 Epoch 20/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9800 - loss: 0.3626 - val_acc: 0.9667 - val_loss: 0.3410 Epoch 21/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9800 - loss: 0.3562 - val_acc: 0.9667 - val_loss: 0.3350 Epoch 22/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.3501 - val_acc: 0.9667 - val_loss: 0.3297 Epoch 23/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.3443 - val_acc: 0.9667 - val_loss: 0.3246 Epoch 24/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.3394 - val_acc: 0.9667 - val_loss: 0.3201 Epoch 25/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.3346 - val_acc: 0.9667 - val_loss: 0.3159 Epoch 26/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.3298 - val_acc: 0.9667 - val_loss: 0.3114 Epoch 27/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.3246 - val_acc: 0.9667 - val_loss: 0.3070 Epoch 28/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.3199 - val_acc: 0.9667 - val_loss: 0.3021 Epoch 29/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.3151 - val_acc: 0.9667 - val_loss: 0.2980 Epoch 30/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.3107 - val_acc: 0.9667 - val_loss: 0.2942 Epoch 31/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.3064 - val_acc: 0.9667 - val_loss: 0.2897 Epoch 32/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.3021 - val_acc: 0.9667 - val_loss: 0.2869 Epoch 33/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9599 - loss: 0.2989 - val_acc: 0.9667 - val_loss: 0.2821 Epoch 34/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2939 - val_acc: 0.9667 - val_loss: 0.2782 Epoch 35/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - acc: 0.9642 - loss: 0.2897 - val_acc: 0.9667 - val_loss: 0.2747 Epoch 36/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2861 - val_acc: 0.9667 - val_loss: 0.2707 Epoch 37/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9599 - loss: 0.2821 - val_acc: 0.9667 - val_loss: 0.2665 Epoch 38/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2777 - val_acc: 0.9667 - val_loss: 0.2637 Epoch 39/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2748 - val_acc: 0.9667 - val_loss: 0.2600 Epoch 40/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2713 - val_acc: 0.9667 - val_loss: 0.2563 Epoch 41/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2676 - val_acc: 0.9667 - val_loss: 0.2527 Epoch 42/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2644 - val_acc: 0.9667 - val_loss: 0.2505 Epoch 43/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2618 - val_acc: 0.9667 - val_loss: 0.2472 Epoch 44/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2590 - val_acc: 0.9667 - val_loss: 0.2445 Epoch 45/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9670 - loss: 0.2562 - val_acc: 0.9667 - val_loss: 0.2423 Epoch 46/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2540 - val_acc: 0.9667 - val_loss: 0.2401 Epoch 47/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2517 - val_acc: 0.9667 - val_loss: 0.2380 Epoch 48/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2496 - val_acc: 0.9667 - val_loss: 0.2361 Epoch 49/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9670 - loss: 0.2477 - val_acc: 0.9667 - val_loss: 0.2339 Epoch 50/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2454 - val_acc: 0.9667 - val_loss: 0.2319 Epoch 51/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2433 - val_acc: 0.9667 - val_loss: 0.2298 Epoch 52/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2411 - val_acc: 0.9667 - val_loss: 0.2284 Epoch 53/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2394 - val_acc: 0.9667 - val_loss: 0.2265 Epoch 54/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2374 - val_acc: 0.9667 - val_loss: 0.2251 Epoch 55/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2354 - val_acc: 0.9667 - val_loss: 0.2234 Epoch 56/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2333 - val_acc: 0.9667 - val_loss: 0.2217 Epoch 57/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2315 - val_acc: 0.9667 - val_loss: 0.2202 Epoch 58/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2297 - val_acc: 0.9667 - val_loss: 0.2190 Epoch 59/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2284 - val_acc: 0.9667 - val_loss: 0.2175 Epoch 60/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2269 - val_acc: 0.9667 - val_loss: 0.2162 Epoch 61/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2253 - val_acc: 0.9667 - val_loss: 0.2152 Epoch 62/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2243 - val_acc: 0.9667 - val_loss: 0.2138 Epoch 63/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2230 - val_acc: 0.9667 - val_loss: 0.2129 Epoch 64/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2219 - val_acc: 0.9667 - val_loss: 0.2112 Epoch 65/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2204 - val_acc: 0.9667 - val_loss: 0.2098 Epoch 66/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2193 - val_acc: 0.9667 - val_loss: 0.2092 Epoch 67/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2182 - val_acc: 0.9667 - val_loss: 0.2077 Epoch 68/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2168 - val_acc: 0.9667 - val_loss: 0.2059 Epoch 69/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2157 - val_acc: 0.9667 - val_loss: 0.2050 Epoch 70/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2144 - val_acc: 0.9667 - val_loss: 0.2040 Epoch 71/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2133 - val_acc: 0.9667 - val_loss: 0.2028 Epoch 72/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2123 - val_acc: 0.9667 - val_loss: 0.2016 Epoch 73/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2112 - val_acc: 0.9667 - val_loss: 0.2003 Epoch 74/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2100 - val_acc: 0.9667 - val_loss: 0.1995 Epoch 75/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2093 - val_acc: 0.9667 - val_loss: 0.1986 Epoch 76/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2082 - val_acc: 0.9667 - val_loss: 0.1973 Epoch 77/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2074 - val_acc: 0.9667 - val_loss: 0.1974 Epoch 78/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2070 - val_acc: 0.9667 - val_loss: 0.1957 Epoch 79/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2056 - val_acc: 0.9667 - val_loss: 0.1947 Epoch 80/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2045 - val_acc: 0.9667 - val_loss: 0.1937 Epoch 81/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2036 - val_acc: 0.9667 - val_loss: 0.1935 Epoch 82/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2032 - val_acc: 0.9667 - val_loss: 0.1919 Epoch 83/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2018 - val_acc: 0.9667 - val_loss: 0.1910 Epoch 84/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.2011 - val_acc: 0.9667 - val_loss: 0.1903 Epoch 85/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.2004 - val_acc: 0.9667 - val_loss: 0.1892 Epoch 86/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.1993 - val_acc: 0.9667 - val_loss: 0.1879 Epoch 87/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1983 - val_acc: 0.9667 - val_loss: 0.1873 Epoch 88/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.1974 - val_acc: 0.9667 - val_loss: 0.1863 Epoch 89/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - acc: 0.9642 - loss: 0.1966 - val_acc: 0.9667 - val_loss: 0.1851 Epoch 90/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 5ms/step - acc: 0.9642 - loss: 0.1956 - val_acc: 0.9667 - val_loss: 0.1844 Epoch 91/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1946 - val_acc: 0.9667 - val_loss: 0.1828 Epoch 92/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.1934 - val_acc: 0.9667 - val_loss: 0.1820 Epoch 93/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - acc: 0.9642 - loss: 0.1926 - val_acc: 0.9667 - val_loss: 0.1805 Epoch 94/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1914 - val_acc: 0.9667 - val_loss: 0.1796 Epoch 95/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1904 - val_acc: 0.9667 - val_loss: 0.1787 Epoch 96/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1897 - val_acc: 0.9667 - val_loss: 0.1777 Epoch 97/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1886 - val_acc: 0.9667 - val_loss: 0.1767 Epoch 98/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1875 - val_acc: 0.9667 - val_loss: 0.1753 Epoch 99/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1866 - val_acc: 0.9667 - val_loss: 0.1748 Epoch 100/100 [1m8/8[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - acc: 0.9642 - loss: 0.1859 - val_acc: 0.9667 - val_loss: 0.1735
# 함수로 만들어서 사용합시다.
def dl_history_plot(history):
plt.figure(figsize=(16,4))
plt.subplot(1,2,1)
plt.plot(history['loss'], label='loss', marker = '.')
plt.plot(history['val_loss'], label='val_loss', marker = '.')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend()
plt.grid()
plt.subplot(1,2,2)
plt.plot(history['acc'], label='acc', marker = '.')
plt.plot(history['val_acc'], label='val_acc', marker = '.')
plt.ylabel('ACC')
plt.xlabel('Epochs')
plt.legend()
plt.grid()
plt.show()
dl_history_plot(history)
import numpy as np
from sklearn.metrics import roc_auc_score, accuracy_score
pred = model.predict(X_valid)
print(classification_report(y_valid, y_pred))
print('Acc Score :', accuracy_score(y_valid, y_pred))
[1m1/1[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 38ms/step precision recall f1-score support 0 1.00 1.00 1.00 10 1 1.00 1.00 1.00 9 2 1.00 1.00 1.00 11 accuracy 1.00 30 macro avg 1.00 1.00 1.00 30 weighted avg 1.00 1.00 1.00 30 Acc Score : 1.0
# 모듈 불러오기
from sklearn.metrics import precision_score
# 성능 평가
print('정밀도(Precision):', precision_score(y_valid, y_pred, average=None))
print('정밀도(Precision):', precision_score(y_valid, y_pred, average='macro'))
print('정밀도(Precision):', precision_score(y_valid, y_pred, average='weighted'))
정밀도(Precision): [1. 1. 1.] 정밀도(Precision): 1.0 정밀도(Precision): 1.0
# 모듈 불러오기
from sklearn.metrics import recall_score
# 성능 평가
print('각각 0과 1에 대한 재현율(Recall):', recall_score(y_valid, y_pred, average=None))
각각 0과 1에 대한 재현율(Recall): [1. 1. 1.]
# 모듈 불러오기
from sklearn.metrics import f1_score
# 성능 평가
print('각각 0과 1에 대한 F1 score:', f1_score(y_valid, y_pred, average=None))
각각 0과 1에 대한 F1 score: [1. 1. 1.]
'Machine Learning' 카테고리의 다른 글
AUC가 뭔가요? (0) | 2024.08.11 |
---|---|
4.시계열_1_따릉이 (0) | 2024.06.02 |
2.이진분류문제_3_악성사이트 (0) | 2024.06.02 |
2.이진분류문제_2_대학진학 (0) | 2024.06.02 |
2.이진분류문제_1_타이타닉 (0) | 2024.06.02 |
소중한 공감 감사합니다