How can Tensorflow be used to define feature columns in Python?

TensorFlow feature columns provide a way to describe how raw input data should be transformed and fed into estimator models. They act as a bridge between raw data and the features used by machine learning models.

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

We will use TensorFlow's feature column API to define how our dataset features should be processed. Feature columns tell the estimator how to interpret the raw input data from your features dictionary.

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.

Defining Feature Columns

Here's how to create feature columns by iterating through your training dataset keys ?

import tensorflow as tf
import pandas as pd

# Sample training data
train_data = {
    'feature1': [1.0, 2.0, 3.0],
    'feature2': [4.0, 5.0, 6.0], 
    'feature3': [7.0, 8.0, 9.0],
    'target': [0, 1, 0]
}
train = pd.DataFrame(train_data)

print("Building list of feature columns for estimator model")
my_feature_columns = []
for key in train.keys():
    if key != 'target':  # Skip target column
        my_feature_columns.append(tf.feature_column.numeric_column(key=key))

print("Feature columns created:")
for col in my_feature_columns:
    print(f"- {col.key}")

The output of the above code is ?

Building list of feature columns for estimator model
Feature columns created:
- feature1
- feature2  
- feature3

How Feature Columns Work

  • A feature column describes how the model should use raw input data from the features dictionary. When an Estimator model is built, a list of feature columns is passed to it.

  • They describe each of the features the model should use.

  • The tf.feature_column module gives many options to represent the data to the model.

  • We build a list of feature columns to tell Estimator model to represent each feature as 32-bit floating-point values.

Types of Feature Columns

TensorFlow provides different types of feature columns for various data types ?

import tensorflow as tf

# Numeric column for continuous values
numeric_col = tf.feature_column.numeric_column("age")

# Categorical column with vocabulary list  
categorical_col = tf.feature_column.categorical_column_with_vocabulary_list(
    "gender", ["male", "female"])

# Bucketized column for binning continuous values
bucketized_col = tf.feature_column.bucketized_column(
    numeric_col, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])

print("Feature column types:")
print(f"Numeric: {numeric_col.key}")
print(f"Categorical: {categorical_col.key}")  
print(f"Bucketized: {bucketized_col.source_column.key}")
Feature column types:
Numeric: age
Categorical: gender
Bucketized: age

Conclusion

Feature columns in TensorFlow provide a flexible way to transform raw data into features suitable for machine learning models. Use tf.feature_column.numeric_column() for continuous data and categorical columns for discrete values. They serve as the interface between your raw data and the estimator model.

Updated on: 2026-03-25T16:36:47+05:30

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements