How can Tensorflow be used to train and evaluate the titanic dataset?

TensorflowServer Side ProgrammingProgramming

Tensorflow can be used to train and evaluate the titanic dataset using the ‘train’ method and the ‘evaluate’ method respectively.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

An Estimator is TensorFlow's high-level representation of a complete model. It is designed for easy scaling and asynchronous training.  We will train a logistic regression model using the tf.estimator API. The model is used as a baseline for other algorithms. We use the titanic dataset with the goal of predicting passenger survival, given characteristics such as gender, age, class, etc.


linear_est = tf.estimator.LinearClassifier(feature_columns)
print("The model is being trained")
linear_est.train(train_input_fn, max_steps=100)
print("The model is being evaluated")
result = linear_est.evaluate(eval_input_fn)

Code credit −


accuracy              0.765152
accuracy_baseline     0.625000
auc                   0.832844
auc_precision_recall 0.789631
average_loss         0.478908
label/mean           0.375000
loss                  0.478908
precision             0.703297
prediction/mean     0.350790
recall               0.646465
global_step         100.000000
dtype: float64


  • The model is initialized.
  • The features and hyperparameters are also mentioned.
  • The training data is fed to the model with the help of train_input_fn.
  • The model is trained using the train function.
  • The model performance is determined using the evaluation set.
  • The dfeval DataFrame is used to determine the performance.
  • The predictions are verified by checking the labels from y_eval array.
  • Before a Boosted Trees model is trained, a linear classifier is trained.
  • This is done, i.e starting with a simple model to establish a benchmark.
Updated on 25-Feb-2021 10:38:42