# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## 텐서플로우로 분류 모델 만들기
# * [첫 번째 신경망 훈련하기: 기초적인 분류 문제  |  TensorFlow Core](https://www.tensorflow.org/tutorials/keras/classification)

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("data/diabetes.csv")
df.shape

df.head()

df["Insulin"] = df["Insulin"].replace(0, np.nan)

Insulin_median = df.groupby("Outcome")["Insulin"].median()
Insulin_median[0]

df.loc[df["Outcome"] == 0, "Insulin"] = Insulin_median[0]
df.loc[df["Outcome"] == 1, "Insulin"] = Insulin_median[1]

# +
train = df.sample(frac=0.8,random_state=0)
test = df.drop(train.index)

train.shape, test.shape
# -

h = df.hist(figsize=(24, 12))

label_name = "Outcome"

# +
X_train = train.drop(label_name, axis=1)
y_train = train[label_name]

X_train.shape, y_train.shape

# +
X_test = test.drop(label_name, axis=1)
y_test = test[label_name]

X_test.shape, y_test.shape
# -

df.sample()

data = df.drop(label_name, axis=1)
input_shape = data.shape[1]
# input_shape

# +
import tensorflow as tf

model = tf.keras.models.Sequential([
  tf.keras.layers.Input(shape=(input_shape)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(2, activation='softmax') # sigmoid, softmax
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
# -

model.fit(X_train, y_train, 
            epochs=100,
            use_multiprocessing=True,
            verbose=0)

y_pred = model.predict(X_test)
y_pred[:5]

y_pred[0]

# +
test_loss, test_acc = model.evaluate(X_test,  y_test, verbose=2)

print('\n테스트 정확도:', test_acc)
# -

y_predict = np.argmax(y_pred, axis=1)
y_predict

(y_test == y_predict).mean()





StarcoderdataJupyterScriptsDedupFiltered