How can Tensorflow be used with Estimators for feature engineering the model?


Tensorflow can be used with estimators for feature engineering by first defining the columns and iterating through the categorical columns. The unique names of features are obtained, and is appended to an empty list.

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. Estimators use feature columns to describe how the model would interpret the raw input features. An Estimator expects a vector of numeric inputs, and feature columns will help describe how the model should convert every feature in the dataset.

Selecting and using the right set of feature columns is essential to learning an effective model. A feature column can be one of the raw inputs in the original features dict, or a new column created using transformations that are defined on one or multiple base columns.

The linear estimator uses as well as numeric and categorical features. Feature columns work with all the TensorFlow estimators. Their goal is to define the features used for modeling. They also have feature engineering capabilities like one-hot-encoding, normalization, and bucketization.

Example

print("Feature engineering")
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']
feature_columns = []
print("Iterating through categorical columns")
for feature_name in CATEGORICAL_COLUMNS:
vocabulary = dftrain[feature_name].unique()
feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))
print("Iterating through numeric columns")
for feature_name in NUMERIC_COLUMNS:
feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

Code credit −https://www.tensorflow.org/tutorials/estimator/linear

Output

Feature engineering
Iterating through categorical columns
Iterating through numeric columns

Explanation

  • Here, feature engineering is performed.
  • The columns ar eiterated over, and are appended to a list.

Updated on: 22-Feb-2021

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements