How can Tensorflow be used with Estimators to explore the titanic data?

The titanic dataset can be explored using the estimator with Tensorflow by using the ‘head’ method, the ‘describe’ method, and the ‘shape’ method. The head method gives the first few rows of the dataset, and the describe method gives information about the dataset, such as column names, types, mean, variance, standard deviation and so on. The shape method gives the dimensions of the data.

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. 

TensorFlow Text contains collection of text related classes and ops that can be used with TensorFlow 2.0. The TensorFlow Text can be used to preprocess sequence modelling.

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.


print("Sample data is being displayed")
print("The metadata about data is being displayed")
print("The dimensions of the data is being displayed")
print(dftrain.shape[0], dfeval.shape[0])

Code credit −


Sample data is being displayed
   sex    age  n_siblings_spouses parch ... class deck embark_town alone
0  male   22.0  1 0   ... Third unknown Southampton      n
1  female 38.0  1 0   ... First C Cherbourg              n
2  female 26.0  0 0   ... Third unknown Southampton      y
3  female 35.0  1 0   ... First C Southampton            n
4  male   28.0  0 0   .. Third unknown Queenstown        y
[5 rows x 9 columns]
The metadata about data is being displayed
        age    n_siblings_spouses  parch       fare
count  627.000000  627.000000   627.000000   627.000000
mean   29.631308     0.545455     0.379585    34.385399
std    12.511818     1.151090     0.792999    54.597730
min     0.750000     0.000000     0.000000     0.000000
25%    23.000000     0.000000     0.000000     7.895800
50%    28.000000     0.000000     0.000000    15.045800
75%    35.000000     1.000000     0.000000    31.387500
max    80.000000     8.000000     5.000000   512.329200
The dimensions of the data is being displayed
627 264


  • Sample data of the titanic dataset is displayed on the console.

  • The describe method is used to provide metadata about the dataset.

  • The shape method gives the dimensions of the dataset.

Updated on: 22-Feb-2021


Kickstart Your Career

Get certified by completing the course

Get Started