text_html_output_1.png
from tensorflow import keras

import pandas as pd

import pandas as pd

import tensorflow as tf

def fill_missing(data):
    data['HomePlanet'].fillna('None', inplace=True)
    data['CryoSleep'].fillna(False, inplace=True)
    data['Cabin'].fillna('Unknown/-1/Unknown', inplace=True)
    data['Destination'].fillna('None', inplace=True)
    data['Name'].fillna('Unknown Unknown', inplace=True)
    data['Age'].fillna(int(train['Age'].mode()), inplace=True)
    data['VIP'].fillna(False, inplace=True)
    for key in ['RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']:
        data[key].fillna(data[key].median(), inplace=True)
def feature_engineering(data):
    bool_type = ['VIP', 'CryoSleep']
    data[bool_type] = data[bool_type].astype(int)
    data['Deck'] = data['Cabin'].apply(lambda item: str(item).split('/')[0])
    data['Num'] = data['Cabin'].apply(lambda item: str(item).split('/')[1])
    data['FirstName'] = data['Name'].apply(lambda item: item.split(' ')[0])
    data['LastName'] = data['Name'].apply(lambda item: item.split(' ')[1])
    data['Side'] = data['Cabin'].apply(lambda item: str(item).split('/')[2])
    data['TotalSpend'] = data['RoomService'] + data['FoodCourt'] + data['ShoppingMall'] + data['Spa'] + data['VRDeck'] + 1e-08
    data['PctRoomService'] = data['RoomService'] / data['TotalSpend']
    data['PctFoodCourt'] = data['FoodCourt'] / data['TotalSpend']
    data['PctShoppingMall'] = data['ShoppingMall'] / data['TotalSpend']
    data['PctSpa'] = data['Spa'] / data['TotalSpend']
    data['PctVRDeck'] = data['VRDeck'] / data['TotalSpend']
    data.pop('Cabin')
    data.pop('PassengerId')
    data.pop('Name')

train = pd.read_csv('../input/spaceship-titanic/train.csv')
train_targets = train.pop('Transported').astype(int)
test = pd.read_csv('../input/spaceship-titanic/test.csv')
data = pd.concat([train, test])
fill_missing(data)
feature_engineering(data)
for column in data.columns:
    if 'int' in str(data[column].dtype):
        data[column] = data[column].astype(float)
train = data.iloc[0:len(train)]
test = data.iloc[len(train):]

def preprocess(x, y):
    return (((x[0][0], x[0][1], x[0][2], x[0][3], x[0][4], x[0][5], x[0][6]), x[1]), y)
def make_dataset(category_df, numeric_df, target, batch_size=32, mode='train'):
    dataset = tf.data.Dataset.from_tensor_slices(((category_df, numeric_df), target))
    dataset = dataset.map(preprocess)
    if mode == 'train':
        dataset = dataset.shuffle(buffer_size=batch_size)
    dataset = dataset.batch(batch_size).cache().prefetch(tf.data.AUTOTUNE)
    return dataset

categorical_features = ['HomePlanet', 'Destination', 'Deck', 'Num', 'Side', 'FirstName', 'LastName']
numerical_fetures = ['CryoSleep', 'Age', 'VIP', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck', 'TotalSpend', 'PctRoomService', 'PctFoodCourt', 'PctShoppingMall', 'PctSpa', 'PctVRDeck']

normalization_layer = keras.layers.Normalization()
with tf.device('CPU'):
    normalization_layer.adapt(train[numerical_fetures])