논리 회귀 실습

당뇨 예측하기


Kaggle Import

import os
os.environ['KAGGLE_USERNAME'] = 'name' # username
os.environ['KAGGLE_KEY'] = 'Key' # key

Dataset 다운

!kaggle datasets download -d kandij/diabetes-dataset
!unzip diabetes-dataset.zip

라이브러리 Import

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam, SGD
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

Dataset 로드

df = pd.read_csv('diabetes2.csv')

df.head(5)

클래스 개수 확인

sns.countplot(x=df['Outcome'])

전처리 확인

print(df.isnull().sum())

x, y 데이터 분할

x_data = df.drop(columns=['Outcome'], axis=1)
x_data = x_data.astype(np.float32)

x_data.head(5)

y_data = df[['Outcome']]
y_data = y_data.astype(np.float32)

y_data.head(5)

표준화

scaler = StandardScaler()
x_data_scaled = scaler.fit_transform(x_data)

print(x_data.values[0])
print(x_data_scaled[0])

학습/검증 데이터 분할

x_train, x_val, y_train, y_val = train_test_split(x_data_scaled, y_data, test_size=0.2, random_state=2022)

print(x_train.shape, x_val.shape)
print(y_train.shape, y_val.shape)

모델 학습

model = Sequential([
    Dense(1, activation="sigmoid")
])

model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.01), metrics=['acc'])

model.fit(
    x_train,
    y_train,
    validation_data = (x_val,y_val),
    epochs=20
)
/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/adam.py:105: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.
  super(Adam, self).__init__(name, **kwargs)
Epoch 1/20
20/20 [==============================] - 1s 10ms/step - loss: 0.6750 - acc: 0.6531 - val_loss: 0.5732 - val_acc: 0.7403
Epoch 2/20
20/20 [==============================] - 0s 3ms/step - loss: 0.5876 - acc: 0.6954 - val_loss: 0.5313 - val_acc: 0.7597
Epoch 3/20
20/20 [==============================] - 0s 3ms/step - loss: 0.5427 - acc: 0.7296 - val_loss: 0.5122 - val_acc: 0.7727
Epoch 4/20
20/20 [==============================] - 0s 3ms/step - loss: 0.5195 - acc: 0.7476 - val_loss: 0.4964 - val_acc: 0.7727
Epoch 5/20
20/20 [==============================] - 0s 3ms/step - loss: 0.5053 - acc: 0.7492 - val_loss: 0.4832 - val_acc: 0.7792
Epoch 6/20
20/20 [==============================] - 0s 4ms/step - loss: 0.4975 - acc: 0.7541 - val_loss: 0.4776 - val_acc: 0.7662
Epoch 7/20
20/20 [==============================] - 0s 4ms/step - loss: 0.4923 - acc: 0.7606 - val_loss: 0.4727 - val_acc: 0.7792
Epoch 8/20
20/20 [==============================] - 0s 4ms/step - loss: 0.4886 - acc: 0.7622 - val_loss: 0.4700 - val_acc: 0.7727
Epoch 9/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4852 - acc: 0.7622 - val_loss: 0.4633 - val_acc: 0.7857
Epoch 10/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4830 - acc: 0.7671 - val_loss: 0.4660 - val_acc: 0.7662
Epoch 11/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4815 - acc: 0.7752 - val_loss: 0.4659 - val_acc: 0.7727
Epoch 12/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4802 - acc: 0.7704 - val_loss: 0.4610 - val_acc: 0.7792
Epoch 13/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4794 - acc: 0.7752 - val_loss: 0.4603 - val_acc: 0.7792
Epoch 14/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4787 - acc: 0.7736 - val_loss: 0.4591 - val_acc: 0.7792
Epoch 15/20
20/20 [==============================] - 0s 4ms/step - loss: 0.4781 - acc: 0.7785 - val_loss: 0.4535 - val_acc: 0.7857
Epoch 16/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4783 - acc: 0.7785 - val_loss: 0.4533 - val_acc: 0.7792
Epoch 17/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4783 - acc: 0.7720 - val_loss: 0.4558 - val_acc: 0.7857
Epoch 18/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4789 - acc: 0.7736 - val_loss: 0.4511 - val_acc: 0.7857
Epoch 19/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4781 - acc: 0.7736 - val_loss: 0.4574 - val_acc: 0.7857
Epoch 20/20
20/20 [==============================] - 0s 3ms/step - loss: 0.4781 - acc: 0.7752 - val_loss: 0.4560 - val_acc: 0.7857
<keras.callbacks.History at 0x7f1866c272d0>

좋은 웹페이지 즐겨찾기