# ---
# 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] colab_type="text" id="FhGuhbZ6M5tl"
# ##### Copyright 2018 The TensorFlow Authors.

# + cellView="form" colab_type="code" id="AwOEIRJC6Une" colab={}
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# + cellView="form" colab_type="code" id="KyPEtTqk6VdG" colab={}
#@title MIT License
#
# Copyright (c) 2017 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# # copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# + [markdown] colab_type="text" id="EIdT9iu_Z4Rb"
# # Basic regression: Predict fuel efficiency

# + [markdown] colab_type="text" id="bBIlTPscrIT9"
# <table class="tfo-notebook-buttons" align="left">
#   <td>
#     <a target="_blank" href="https://www.tensorflow.org/tutorials/keras/regression"><img src="https://www.tensorflow.org/images/tf_logo_32px.png" />View on TensorFlow.org</a>
#   </td>
#   <td>
#     <a target="_blank" href="https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a>
#   </td>
#   <td>
#     <a target="_blank" href="https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View source on GitHub</a>
#   </td>
#   <td>
#     <a href="https://storage.googleapis.com/tensorflow_docs/docs/site/en/tutorials/keras/regression.ipynb"><img src="https://www.tensorflow.org/images/download_logo_32px.png" />Download notebook</a>
#   </td>
# </table>

# + [markdown] colab_type="text" id="AHp3M9ZmrIxj"
# In a *regression* problem, we aim to predict the output of a continuous value, like a price or a probability. Contrast this with a *classification* problem, where we aim to select a class from a list of classes (for example, where a picture contains an apple or an orange, recognizing which fruit is in the picture).
#
# This notebook uses the classic [Auto MPG](https://archive.ics.uci.edu/ml/datasets/auto+mpg) Dataset and builds a model to predict the fuel efficiency of late-1970s and early 1980s automobiles. To do this, we'll provide the model with a description of many automobiles from that time period. This description includes attributes like: cylinders, displacement, horsepower, and weight.
#
# This example uses the `tf.keras` API, see [this guide](https://www.tensorflow.org/guide/keras) for details.

# + colab_type="code" id="moB4tpEHxKB3" colab={}
# Use seaborn for pairplot
# !pip install seaborn

# Use some functions from tensorflow_docs
# !pip install git+https://github.com/tensorflow/docs

# + colab_type="code" id="1rRo8oNqZ-Rj" colab={}
from __future__ import absolute_import, division, print_function, unicode_literals

import pathlib

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

# + colab_type="code" id="9xQKvCJ85kCQ" colab={}
try:
  # # %tensorflow_version only exists in Colab.
  # %tensorflow_version 2.x
except Exception:
  pass
import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers

print(tf.__version__)

# + colab_type="code" id="Qz4HfsgRQUiV" colab={}
import tensorflow_docs as tfdocs
import tensorflow_docs.plots
import tensorflow_docs.modeling

# + [markdown] colab_type="text" id="F_72b0LCNbjx"
# ## The Auto MPG dataset
#
# The dataset is available from the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/).
#
#

# + [markdown] colab_type="text" id="gFh9ne3FZ-On"
# ### Get the data
# First download the dataset.

# + colab_type="code" id="p9kxxgzvzlyz" colab={}
dataset_path = keras.utils.get_file("auto-mpg.data", "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")
dataset_path

# + [markdown] colab_type="text" id="nslsRLh7Zss4"
# Import it using pandas

# + colab_type="code" id="CiX2FI4gZtTt" colab={}
column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',
                'Acceleration', 'Model Year', 'Origin']
raw_dataset = pd.read_csv(dataset_path, names=column_names,
                      na_values = "?", comment='\t',
                      sep=" ", skipinitialspace=True)

dataset = raw_dataset.copy()
print(dataset.shape)
print(dataset.head())
print(dataset.tail())
dataset.tail()

# + [markdown] colab_type="text" id="3MWuJTKEDM-f"
# ### Clean the data
#
# The dataset contains a few unknown values.

# + colab_type="code" id="JEJHhN65a2VV" colab={}
dataset.isna().sum()

# + [markdown] colab_type="text" id="9UPN0KBHa_WI"
# To keep this initial tutorial simple drop those rows.

# + colab_type="code" id="4ZUDosChC1UN" colab={}
dataset = dataset.dropna()
dataset.isna().sum()
dataset.tail()

# + [markdown] colab_type="text" id="8XKitwaH4v8h"
# The `"Origin"` column is really categorical, not numeric. So convert that to a one-hot:

# + colab_type="code" id="gWNTD2QjBWFJ" colab={}
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})

# + colab_type="code" id="ulXz4J7PAUzk" colab={}
dataset = pd.get_dummies(dataset, prefix='', prefix_sep='')
dataset.tail()

# + [markdown] colab_type="text" id="Cuym4yvk76vU"
# ### Split the data into train and test
#
# Now split the dataset into a training set and a test set.
#
# We will use the test set in the final evaluation of our model.

# + colab_type="code" id="qn-IGhUE7_1H" colab={}
train_dataset = dataset.sample(frac=0.8,random_state=0)
test_dataset = dataset.drop(train_dataset.index)
print(train_dataset.shape)
train_dataset

# + id="00ASbRmi5_8R" colab_type="code" colab={}
print(test_dataset.shape)
test_dataset

# + [markdown] colab_type="text" id="J4ubs136WLNp"
# ### Inspect the data
#
# Have a quick look at the joint distribution of a few pairs of columns from the training set.

# + colab_type="code" id="oRKO_x8gWKv-" colab={}
sns.pairplot(train_dataset[["MPG", "Cylinders", "Displacement", "Weight"]], diag_kind="kde")

# + [markdown] colab_type="text" id="gavKO_6DWRMP"
# Also look at the overall statistics:

# + colab_type="code" id="yi2FzC3T21jR" colab={}
train_stats = train_dataset.describe()
train_stats.pop("MPG")
train_stats = train_stats.transpose()
train_stats

# + [markdown] colab_type="text" id="Db7Auq1yXUvh"
# ### Split features from labels
#
# Separate the target value, or "label", from the features. This label is the value that you will train the model to predict.

# + colab_type="code" id="t2sluJdCW7jN" colab={}
train_labels = train_dataset.pop('MPG')
test_labels = test_dataset.pop('MPG')
train_labels

# + id="GHkyg0U998l0" colab_type="code" colab={}
test_labels


# + [markdown] colab_type="text" id="mRklxK5s388r"
# ### Normalize the data
#
# Look again at the `train_stats` block above and note how different the ranges of each feature are.

# + [markdown] colab_type="text" id="-ywmerQ6dSox"
# It is good practice to normalize features that use different scales and ranges. Although the model *might* converge without feature normalization, it makes training more difficult, and it makes the resulting model dependent on the choice of units used in the input.
#
# Note: Although we intentionally generate these statistics from only the training dataset, these statistics will also be used to normalize the test dataset. We need to do that to project the test dataset into the same distribution that the model has been trained on.

# + colab_type="code" id="JlC5ooJrgjQF" colab={}
def norm(x):
  return (x - train_stats['mean']) / train_stats['std']
normed_train_data = norm(train_dataset)
normed_test_data = norm(test_dataset)
normed_train_data


# + [markdown] colab_type="text" id="BuiClDk45eS4"
# This normalized data is what we will use to train the model.
#
# Caution: The statistics used to normalize the inputs here (mean and standard deviation) need to be applied to any other data that is fed to the model, along with the one-hot encoding that we did earlier.  That includes the test set as well as live data when the model is used in production.

# + [markdown] colab_type="text" id="SmjdzxKzEu1-"
# ## The model

# + [markdown] colab_type="text" id="6SWtkIjhrZwa"
# ### Build the model
#
# Let's build our model. Here, we'll use a `Sequential` model with two densely connected hidden layers, and an output layer that returns a single, continuous value. The model building steps are wrapped in a function, `build_model`, since we'll create a second model, later on.

# + colab_type="code" id="c26juK7ZG8j-" colab={}
def build_model():
  model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
    layers.Dense(64, activation='relu'),
    layers.Dense(1)
  ])

  optimizer = tf.keras.optimizers.RMSprop(0.001)

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


# + colab_type="code" id="cGbPb-PHGbhs" colab={}
model = build_model()

# + [markdown] colab_type="text" id="Sj49Og4YGULr"
# ### Inspect the model
#
# Use the `.summary` method to print a simple description of the model

# + colab_type="code" id="ReAD0n6MsFK-" colab={}
model.summary()

# + [markdown] colab_type="text" id="Vt6W50qGsJAL"
#
# Now try out the model. Take a batch of `10` examples from the training data and call `model.predict` on it.

# + colab_type="code" id="-d-gBaVtGTSC" colab={}
example_batch = normed_train_data[:10]
example_result = model.predict(example_batch)
example_result

# + [markdown] colab_type="text" id="QlM8KrSOsaYo"
# It seems to be working, and it produces a result of the expected shape and type.

# + [markdown] colab_type="text" id="0-qWCsh6DlyH"
# ### Train the model
#
# Train the model for 1000 epochs, and record the training and validation accuracy in the `history` object.

# + colab_type="code" id="sD7qHCmNIOY0" colab={}
EPOCHS = 1000

history = model.fit(
  normed_train_data, train_labels,
  epochs=EPOCHS, validation_split = 0.2, verbose=0,
  callbacks=[tfdocs.modeling.EpochDots()])

# + [markdown] colab_type="text" id="tQm3pc0FYPQB"
# Visualize the model's training progress using the stats stored in the `history` object.

# + colab_type="code" id="4Xj91b-dymEy" colab={}
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()

# + colab_type="code" id="czYtZS9A6D-X" colab={}
plotter = tfdocs.plots.HistoryPlotter(smoothing_std=2)

# + colab_type="code" id="nMCWKskbUTvG" colab={}
plotter.plot({'Basic': history}, metric = "mae")
plt.ylim([0, 10])
plt.ylabel('MAE [MPG]')

# + colab_type="code" id="N9u74b1tXMd9" colab={}
plotter.plot({'Basic': history}, metric = "mse")
plt.ylim([0, 20])
plt.ylabel('MSE [MPG^2]')

# + [markdown] colab_type="text" id="AqsuANc11FYv"
# This graph shows little improvement, or even degradation in the validation error after about 100 epochs. Let's update the `model.fit` call to automatically stop training when the validation score doesn't improve. We'll use an *EarlyStopping callback* that tests a training condition for  every epoch. If a set amount of epochs elapses without showing improvement, then automatically stop the training.
#
# You can learn more about this callback [here](https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping).

# + colab_type="code" id="fdMZuhUgzMZ4" colab={}
model = build_model()

# The patience parameter is the amount of epochs to check for improvement
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)

early_history = model.fit(normed_train_data, train_labels, 
                    epochs=EPOCHS, validation_split = 0.2, verbose=0, 
                    callbacks=[early_stop, tfdocs.modeling.EpochDots()])

# + colab_type="code" id="LcopvQh3X-kX" colab={}
plotter.plot({'Early Stopping': early_history}, metric = "mae")
plt.ylim([0, 10])
plt.ylabel('MAE [MPG]')

# + [markdown] colab_type="text" id="3St8-DmrX8P4"
# The graph shows that on the validation set, the average error is usually around +/- 2 MPG. Is this good? We'll leave that decision up to you.
#
# Let's see how well the model generalizes by using the **test** set, which we did not use when training the model.  This tells us how well we can expect the model to predict when we use it in the real world.

# + colab_type="code" id="jl_yNr5n1kms" colab={}
loss, mae, mse = model.evaluate(normed_test_data, test_labels, verbose=2)

print("Testing set Mean Abs Error: {:5.2f} MPG".format(mae))

# + [markdown] colab_type="text" id="ft603OzXuEZC"
# ### Make predictions
#
# Finally, predict MPG values using data in the testing set:

# + colab_type="code" id="Xe7RXH3N3CWU" colab={}
test_predictions = model.predict(normed_test_data).flatten()

a = plt.axes(aspect='equal')
plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [MPG]')
plt.ylabel('Predictions [MPG]')
lims = [0, 50]
plt.xlim(lims)
plt.ylim(lims)
_ = plt.plot(lims, lims)


# + [markdown] colab_type="text" id="19wyogbOSU5t"
# It looks like our model predicts reasonably well. Let's take a look at the error distribution.

# + colab_type="code" id="f-OHX4DiXd8x" colab={}
error = test_predictions - test_labels
plt.hist(error, bins = 25)
plt.xlabel("Prediction Error [MPG]")
_ = plt.ylabel("Count")

# + [markdown] colab_type="text" id="m0CB5tBjSU5w"
# It's not quite gaussian, but we might expect that because the number of samples is very small.

# + [markdown] colab_type="text" id="vgGQuV-yqYZH"
# ## Conclusion
#
# This notebook introduced a few techniques to handle a regression problem.
#
# * Mean Squared Error (MSE) is a common loss function used for regression problems (different loss functions are used for classification problems).
# * Similarly, evaluation metrics used for regression differ from classification. A common regression metric is Mean Absolute Error (MAE).
# * When numeric input data features have values with different ranges, each feature should be scaled independently to the same range.
# * If there is not much training data, one technique is to prefer a small network with few hidden layers to avoid overfitting.
# * Early stopping is a useful technique to prevent overfitting.

StarcoderdataJupyterScriptsDedupFiltered