Classifying Titanic Data with Multiple Inputs using Python’s TensorFlow Functional API

Posted by

Python TensorFlow Functional API Multiple Inputs Titanic Classification

Python TensorFlow Functional API Multiple Inputs Titanic Classification

TensorFlow is a powerful open source software library for machine learning developed by Google. It is widely used for various tasks such as image and speech recognition, natural language processing, and more. In this article, we will explore how to use TensorFlow’s Functional API to build a model with multiple inputs for the Titanic classification problem.

Problem Statement

The Titanic classification problem is a classic machine learning task where the goal is to predict whether a passenger on the Titanic survived or not, based on various input features such as age, gender, ticket class, and fare. In this example, we will build a model using TensorFlow’s Functional API to solve this classification problem.

Using TensorFlow’s Functional API

The Functional API in TensorFlow allows us to create complex models with multiple inputs, outputs, and branches. This is useful when building models for tasks such as image captioning, multitask learning, and more.

First, we will import the necessary libraries and load the Titanic dataset. Then, we will preprocess the data by handling missing values, encoding categorical features, and splitting the dataset into training and testing sets.


import tensorflow as tf
from sklearn.model_selection import train_test_split
import pandas as pd

# Load the Titanic dataset
url = 'https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'
df = pd.read_csv(url)

# Preprocess the data
# ...

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Next, we will define the model using the Functional API. We will create multiple input layers for different features, and then concatenate them before passing them through the rest of the model.


# Define the input layers
input_age = tf.keras.Input(shape=(1,), name="age")
input_gender = tf.keras.Input(shape=(1,), name="gender")
input_class = tf.keras.Input(shape=(1,), name="class")
input_fare = tf.keras.Input(shape=(1,), name="fare")

# Combine the input layers
combined_input = tf.keras.layers.concatenate([input_age, input_gender, input_class, input_fare])

# Add hidden layers
# ...

# Define the output layer
output = tf.keras.layers.Dense(1, activation='sigmoid', name="survived")(combined_input)

# Create the model
model = tf.keras.Model(inputs=[input_age, input_gender, input_class, input_fare], outputs=output)

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

Finally, we will train the model on the training data and evaluate its performance on the testing data.


# Train the model
model.fit({
    "age": X_train['Age'],
    "gender": X_train['Sex'],
    "class": X_train['Pclass'],
    "fare": X_train['Fare']
}, y_train, epochs=10)

# Evaluate the model
model.evaluate({
    "age": X_test['Age'],
    "gender": X_test['Sex'],
    "class": X_test['Pclass'],
    "fare": X_test['Fare']
}, y_test)

Conclusion

In this article, we have explored how to use TensorFlow’s Functional API to build a model with multiple inputs for the Titanic classification problem. The Functional API provides a flexible and powerful way to create complex models, and is particularly useful for tasks that involve multiple inputs, outputs, and branches. By leveraging the Functional API, we can build more sophisticated and accurate models for various machine learning tasks.