# ---
# 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="ZjZ2-ozOVtjB"
# - Linear regression & Titanic
# - <NAME>
# - 2016 - 2022

# + id="SeHL4TTgVbrO"
# Import
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
import tensorflow.compat.v2.feature_column as fc

from IPython.display import clear_output

# + id="Ds5f6DHTaOp4"
# Titanic dataset
dftrain = pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/train.csv") # Training
dfeval  = pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/eval.csv")  # Testing
y_train = dftrain.pop("survived") # Pop survived column in y_train
y_eval  = dfeval.pop("survived")  # Pop survived column in y_eval

# + [markdown] id="JcMZ0CBN1cjy"
# ## Exploration of the dataset
#
# - Most passengers are in their mid 20s
# - Most passengers are male
# - Most passengers are in Third class
# - Survival reate is much higher for females

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="5JGnKlV_kLUh" executionInfo={"status": "ok", "timestamp": 1642693229894, "user_tz": -60, "elapsed": 496, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="416f7852-3d7f-4f6d-ad80-5bece9ec9856"
dftrain.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 297} id="iwT3XFuLkNyD" executionInfo={"status": "ok", "timestamp": 1642693229895, "user_tz": -60, "elapsed": 22, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="a87c98fe-fef1-480c-c8e8-3fa631990a58"
dftrain.describe()

# + [markdown] id="WpwhZITQkLD2"
#

# + colab={"base_uri": "https://localhost:8080/"} id="i8aoYbYUkuZK" executionInfo={"status": "ok", "timestamp": 1642693229895, "user_tz": -60, "elapsed": 21, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="464f7884-923c-4279-ad6f-cadd8eab81f3"
dftrain.shape # 627 entries, 9 features

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="KY3i6V5m09HO" executionInfo={"status": "ok", "timestamp": 1642693229896, "user_tz": -60, "elapsed": 17, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="e3f594b3-6e7b-498b-c687-de37d8512ab1"
# Distribution by age
dftrain.age.hist(bins=25) # Most people in mid 20s

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="R6Ctvcza1Lj_" executionInfo={"status": "ok", "timestamp": 1642693230348, "user_tz": -60, "elapsed": 464, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="45efffb7-7813-4756-e81c-2c406358ff88"
# Distribution by sex
dftrain.sex.value_counts().plot(kind='barh')

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="8q_xgkN_1hHu" executionInfo={"status": "ok", "timestamp": 1642693230348, "user_tz": -60, "elapsed": 13, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="c18166a9-5c28-48fd-bbc9-924e548916bc"
# Distribution by class
dftrain['class'].value_counts().plot(kind='barh')

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="Ww8hQ9YH1_AY" executionInfo={"status": "ok", "timestamp": 1642693230992, "user_tz": -60, "elapsed": 650, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="06805007-7850-4440-dae9-fb6d08e7002b"
# Survival by sex
pd.concat(
    [dftrain, y_train],
    axis=1
).groupby('sex').survived.mean().plot(kind="barh").set_xlabel("% Survive")

# + [markdown] id="jVi0H42nVq-Q"
# # Linear regression
#
# - Be as close as possible to each data point
# - Get as many data points on the left as on the right of the line
#
#
#
# \
#
#
#
#

# + colab={"base_uri": "https://localhost:8080/"} id="f1-cA-X3ZvqM" executionInfo={"status": "ok", "timestamp": 1642693230992, "user_tz": -60, "elapsed": 12, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="5d2d3f55-ea4d-48e3-c099-b915c18e14de"
# Create feature columns

CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']

feature_columns = []

for feature_name in CATEGORICAL_COLUMNS:
  vocabulary = dftrain[feature_name].unique()  # gets a list of all unique values from given feature column
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

print(feature_columns)


# + id="8gwvgTbO4eI3"
# Before creating the model
# We need to create an input function with the appropriate `tf.data.Dataset` object to feed the model
# Load data in *batches*: Not one by one (too slow), not everything at once (maynot fit in RAM)

def make_input_fn(data_df, label_df, num_epochs=50, shuffle=True, batch_size=32):
  def input_function():
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))
    if shuffle:
      ds = ds.shuffle(1000)
    ds = ds.batch(batch_size).repeat(num_epochs)
    return ds
  return input_function

train_input_fn = make_input_fn(dftrain, y_train)
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)

# + colab={"base_uri": "https://localhost:8080/"} id="5bSNs2uzFMll" executionInfo={"status": "ok", "timestamp": 1642693227183, "user_tz": -60, "elapsed": 11676, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb1xkeK5CuuX0arwIASP1IAgMUBXBBzCaDLSJYQw=s64", "userId": "06001106705548366077"}} outputId="d75fd3ad-3d44-43c5-d8cc-9b699cd8b7b3"
# Train and evaluate

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn) # Train
result = linear_est.evaluate(eval_input_fn) # Evaluate

clear_output()
result['accuracy']

# + colab={"base_uri": "https://localhost:8080/"} id="mlw9rqbPF6U5" executionInfo={"status": "ok", "timestamp": 1642693228483, "user_tz": -60, "elapsed": 1307, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/<KEY>", "userId": "06001106705548366077"}} outputId="f097417d-be1d-4ba2-c4b9-d707c7894e0c"
# Make a prediction

result = list(linear_est.predict(eval_input_fn)) # List of dicts of all predictions
clear_output()

person_to_lookup = 2
print("the person...", dfeval.loc[person_to_lookup])
print("...did survive?", y_eval.loc[person_to_lookup])
print("We estimated with probability:", result[person_to_lookup]['probabilities']) # index 0: Not surviving / 1 surviving

# + id="KxYg6hamHjSU"


StarcoderdataJupyterScriptsDedupFiltered