# ---
# 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
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/JSJeong-me/Machine_Learning/blob/main/ML/Class-deeplearning.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="3mUBPSlwm4qA"
# **This notebook is an exercise in the [Intro to Deep Learning](https://www.kaggle.com/learn/intro-to-deep-learning) course.  You can reference the tutorial at [this link](https://www.kaggle.com/ryanholbrook/a-single-neuron).**
#
# ---
#

# + [markdown] id="lu_BKhPIm4qE"
# # Introduction #
#
# In the tutorial we learned about the building blocks of neural networks: *linear units*. We saw that a model of just one linear unit will fit a linear function to a dataset (equivalent to linear regression). In this exercise, you'll build a linear model and get some practice working with models in Keras.
#
# Before you get started, run the code cell below to set everything up.

# + [markdown] id="beV27Dokm4qF"
# The *Red Wine Quality* dataset consists of physiochemical measurements from about 1600 Portuguese red wines.  Also included is a quality rating for each wine from blind taste-tests. 
#
# First, run the next cell to display the first few rows of this dataset.

# + id="BqjVNybKD4kH"
import pandas as pd
from IPython.display import display

# + id="onSOwiQmD-bp" outputId="d98ba3e7-a5cf-49c9-db03-21985a527b42" colab={"base_uri": "https://localhost:8080/"}
# !pwd

# + id="EzQi09ZmEBAj" outputId="b1f096c8-1cb1-4217-da43-35a84bfaf3f5" colab={"base_uri": "https://localhost:8080/"}
# !ls -l

# + id="oOsO38pc7e7i"
red_wine = pd.read_csv('winequality-red.csv')

# + id="wM4SaqT_ELZa" outputId="861d765d-af65-46df-e7d2-7f5ba3ad07ad" colab={"base_uri": "https://localhost:8080/"}
type(red_wine)

# + id="uW9OkhCkESsV" outputId="b4aae2a7-da81-4d63-adbd-facb3135f2cc" colab={"base_uri": "https://localhost:8080/", "height": 399}
red_wine.describe()

# + id="Iay7GYZJGrjY"
# Create training and validation splits
df_train = red_wine.sample(frac=0.7, random_state=0)

# + id="5v2PasVwGycV" outputId="d6590bc1-64a4-4d84-d5cd-37e263f9843b" colab={"base_uri": "https://localhost:8080/"}
type(df_train)

# + id="GGvWPmynG44Z" outputId="5732caf3-f763-4cb5-8627-9033033c396b" colab={"base_uri": "https://localhost:8080/", "height": 399}
df_train.describe()

# + id="7Hd3WHYI7hHy"
df_valid = red_wine.drop(df_train.index)


# + id="r2D1-w11HezH" outputId="1d9713b0-8c24-4154-9b65-1d7c85143f24" colab={"base_uri": "https://localhost:8080/", "height": 399}
df_valid.describe()

# + id="iQGqdQ92HbX6" outputId="e7793585-c55a-490e-fe64-d5e8d2287f65" colab={"base_uri": "https://localhost:8080/", "height": 236}
display(df_train.head(4))

# + id="LzujcpdPHuY7"


# + id="rVOsx8IOHuVS"


# + id="CzWtZP96HuSg"


# + id="pyTx_ebrHuPp"


# + id="4IcEwQYqHuMO"


# + id="DGM7hEugHuJc"


# + id="9KgoOTJmHuGj" outputId="e0785a1a-1222-47df-c2a1-04b814b3233f" colab={"base_uri": "https://localhost:8080/", "height": 399}
df_train.describe()

# + id="JVfWAkMg7jJx"
# Scale to [0, 1]
max_ = df_train.max(axis=0)
min_ = df_train.min(axis=0)
df_train = (df_train - min_) / (max_ - min_)
df_valid = (df_valid - min_) / (max_ - min_)

# + id="nHnyMJMLHtyh" outputId="3c18ca83-c9a4-4152-ba6f-7c3eab9bc5e4" colab={"base_uri": "https://localhost:8080/", "height": 399}
df_train.describe()

# + id="0p7EEnRQHtwm"


# + id="jfcEkEfdHttp"


# + id="v8gXnbqUHtl0"


# + id="jes9zH8YHti0" outputId="8ba82064-2f37-4dbc-b977-0a3d425504e8" colab={"base_uri": "https://localhost:8080/"}
df_train["quality"]

# + id="eetwNncNm4qG"
# Split features and target
X_train = df_train.drop('quality', axis=1)
X_valid = df_valid.drop('quality', axis=1)
y_train = df_train['quality']
y_valid = df_valid['quality']
# + id="3xB0CIahIsZg" outputId="fe08470c-99ce-4b99-95ca-d0f05381770b" colab={"base_uri": "https://localhost:8080/", "height": 399}
X_train.describe()

# + id="FDa-Kuf0IsWQ" outputId="c15f41ff-9435-464c-9d71-342db0618c6e" colab={"base_uri": "https://localhost:8080/"}
y_train.describe()

# + id="ir_PImdqIsTG" outputId="41e6bbcf-9ce5-44be-b6be-6200c6337cb3" colab={"base_uri": "https://localhost:8080/", "height": 361}
X_valid.describe()

# + id="i_Oli_lBIsQT"


# + id="bR37UnwWIsM9"


# + id="dUcgwQyKIsIJ"


# + id="ot4HCX1iIsFG"


# + id="s_8BNbp8IsBm"


# + id="VLqpQWRfm4qH"
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(512, activation='relu', input_shape=[11]),
    layers.Dense(512, activation='relu'),
    layers.Dense(512, activation='relu'),
    layers.Dense(1),
])

# + id="A1v18xtbJ5tM" outputId="ce671127-5e55-452c-d7b2-47bb422a4bee" colab={"base_uri": "https://localhost:8080/"}
model.summary()

# + id="G38qOqZ1J5qT"


# + id="-xM560aiJ5nA"


# + id="SGw29zraJ5kc"


# + id="GGzSjdyUJ5h8"


# + id="jiHEqm-kJ5fS"


# + id="_gAppKYwJ5co"


# + id="QFpFcUgcJ5Z_"


# + id="ZTrKKukbJ5XP"


# + id="3eMdsFmAm4qH"
model.compile(
    optimizer='adam',
    loss='mae',
)

# + colab={"base_uri": "https://localhost:8080/"} id="7cgxvzRrryg3" outputId="e2cc7c30-6147-488a-8d27-0e15aa6a1391"
history = model.fit(
    X_train, y_train,
    validation_data=(X_valid, y_valid),
    batch_size=256,
    epochs=100,
)

# + [markdown] id="CEEa85Xzm4qH"
# # 2) Define a linear model
#
# Now define a linear model appropriate for this task. Pay attention to how many inputs and outputs the model should have.

# + colab={"base_uri": "https://localhost:8080/"} id="RwuUB91vm4qI" outputId="d1593b4d-280e-4c1a-892a-33df48382326"
model.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="i6FtZdYSoj2l" outputId="2e61fa9f-7a6f-4938-a1be-f0e83059ae1c"
import pandas as pd

# convert the training history to a dataframe
history_df = pd.DataFrame(history.history)
# use Pandas native plot method
history_df['loss'].plot();

StarcoderdataJupyterScriptsDedupFiltered