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

# + [markdown] id="tgRudCZekg2g" colab_type="text"
# ## <i>Import libraries</i>

# + id="Hd_bVXCKkX0k" colab_type="code" colab={}
from pandas import read_csv
from matplotlib import pyplot as plt

# %tensorflow_version 2.x
from tensorflow.keras import Sequential, layers, optimizers

from sklearn.model_selection import train_test_split

from sklearn.neural_network import MLPRegressor
from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

# + [markdown] id="5c5XOOCzksXb" colab_type="text"
# ## Muat data

# + id="eDIrR-9Mkmo6" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 424} outputId="75a3d81e-a98a-4d6a-ad68-9e3ad3928abe"
data = read_csv("https://pastebin.com/raw/jp0aG9tv", sep=";")
data

# + [markdown] id="WkWTX-QFkvgn" colab_type="text"
# Kita pake <i>dataset</i> dari sini ya :<br>https://archive.ics.uci.edu/ml/datasets/QSAR+aquatic+toxicity<br>Sengaja saya taruh di pastebin biar enak aja waktu nge<i>load</i>'nya

# + [markdown] id="gx73k9ZmlLYs" colab_type="text"
# ## Bagi data

# + id="61xs0LaUkuS6" colab_type="code" colab={}
x_train, x_test, y_train, y_test = train_test_split(data.drop(axis=0, columns='quantitive response'),
                                                   data['quantitive response'],
                                                   test_size=0.3)


# + [markdown] id="yq33Um5xlQ14" colab_type="text"
# Pada notebook ini saya ndak bahas <i>exploratory data analysis</i> yuy<br>Soalnya emang mau bahas cara bikin NN sederhana pake TensorFlow doang

# + [markdown] id="ifx1rHaElrDr" colab_type="text"
# ## Inisialisasi model

# + [markdown] id="_D6jioSnmCMp" colab_type="text"
# ### Jaringan saraf tiruan dari tensorflow

# + id="WJUpFW1dmKf8" colab_type="code" colab={}
def Simple_NN():
  model = Sequential([
    layers.Dense(8, activation='relu', input_shape=[8]), # DELAPAN INI SOALNYA KOLOM DATA YANG DIPAKE NGETRAIN CUMA DELAPAN
    layers.Dense(8, activation='relu'),
    layers.Dense(1)
  ])

  optimizer = optimizers.RMSprop(0.001)

  model.compile(loss='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])
  return model


# + [markdown] id="BEMgQxrdmMoi" colab_type="text"
# Terdiri dari :<br>8 node yang ada pada <i>input layer</i><br>8 node yang ada pada <i>hidden layer</i> 1 dan 2 beserta fungsi aktivasi yang digunakan yaitu ReLU<br>Terus 1 node pada <i>output layer</i> karena ini kita studi kasusnya regresi ya

# + id="RDXupSD6lsC5" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 469} outputId="c322c846-bd11-4da8-b43e-ac2d2932c9c9"
lnr = LinearRegression()
mlp = MLPRegressor()
nn_tf = Simple_NN()

print(lnr, "\n")
print(mlp, "\n")
print(nn_tf.summary())

# + [markdown] id="yucLurRFnahX" colab_type="text"
# ## Latih model-model

# + id="Ei8Ac1QLlPt6" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="3d17a669-bd2a-4eae-a278-ddd2d01eef49"
lnr.fit(x_train, y_train)

mlp.fit(x_train, y_train)

nn_tf.fit(x_train, y_train, epochs=200, validation_split = 0.2, verbose=1)

# + [markdown] id="S6ucTvpwn3GT" colab_type="text"
# ## Uji & evaliasi model

# + id="RQpzEcJQnexP" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 69} outputId="4fe2ecbb-523e-4bc4-bc8e-2d5db3ed23cc"
hasil_lnr = lnr.predict(x_test)
hasil_mlp = mlp.predict(x_test)
hasil_nn_tf = nn_tf.predict(x_test)

print("Mead Squared Error Linear Regression : " ,mean_squared_error(y_test, hasil_lnr))
print("Mead Squared Error Multilayer Perceptron : " ,mean_squared_error(y_test, hasil_mlp))
print("Mead Squared Error NN TensorFlow : " ,mean_squared_error(y_test, hasil_nn_tf))

# + [markdown] id="bLEgYQ1vomKO" colab_type="text"
# Semakin kecil MSE sebuah model maka semakin bagus model tersebut dalam memprediksi

# + [markdown] id="vKNauCItoM7B" colab_type="text"
# ## Grafik garis

# + id="YNJYbOagoDqf" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="631f8d00-d934-4c2b-ce00-9774e50e12dc"
plt.plot(list(hasil_lnr), label='Linear Regression')
plt.plot(list(y_test), label='Nilai Asli')

plt.legend(loc="upper right")

# + id="NkPHaxFToVIU" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="a9b92243-e451-47e2-8d9f-f99848c370cf"
plt.plot(list(hasil_mlp), label='Multilayer Perceptron')
plt.plot(list(y_test), label='Nilai Asli')

plt.legend(loc="upper right")

# + id="AxdJl2mUoXg9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="3c4bb0d1-bdc6-4203-95f6-e6bf7d976261"
plt.plot(list(hasil_nn_tf), label='Tensorflow')
plt.plot(list(y_test), label='Nilai Asli')

plt.legend(loc="upper right")

StarcoderdataJupyterScriptsDedupFiltered