Linear Classifier in Tensorflow
Last Updated :
28 Apr, 2025
In this article, we will be using tf.estimator.LinearClassifier to build a model and train it on the famous titanic dataset. All of this will be done by using the TensorFlow API.
Importing Libraries
Python libraries make it easy for us to handle the data and perform typical and complex tasks with a single line of code.
- Pandas - This library helps to load the data frame in a 2D array format and has multiple functions to perform analysis tasks in one go.
- Numpy - Numpy arrays are very fast and can perform large computations in a very short time.
- Matplotlib/Seaborn - This library is used to draw visualizations.
Python3
import tensorflow as tf
import tensorflow.feature_column as fc
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
Importing Dataset
We will import the dataset by using the Tensorflow API for datasets and then load it into the panda's data frame.
Python3
x_train = pd.read_csv(
'https://storage.googleapis.com/tf-datasets/titanic/train.csv')
x_val = pd.read_csv(
'https://storage.googleapis.com/tf-datasets/titanic/eval.csv')
x_train.head()
Output:
Python3
y_train = x_train.pop('survived')
y_val = x_val.pop('survived')
We will need the data for the categorical columns and the numeric(continuous) column present in the dataset separately to initialize our Linear Classifier model.
Python3
objects = []
numerics = []
for col in x_train.columns:
if x_train[col].dtype == 'object':
objects.append(col)
elif x_train[col].dtype == 'int':
objects.append(col)
else:
numerics.append(col)
print(objects)
print(numerics)
Output:
['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
['age', 'fare']
Python3
feat_cols = []
for feat_name in objects:
vocabulary = x_train[feat_name].unique()
feat_cols.append(fc.categorical_column_with_vocabulary_list(feat_name,
vocabulary))
for feat_name in numerics:
feat_cols.append(fc.numeric_column(feat_name,
dtype=tf.float32))
We need to make a callable function that can be passed to the LinearClassifier function.
Python3
def make_input_fn(data, label,
num_epochs=10,
shuffle=True,
batch_size=32):
def input_function():
ds = tf.data.Dataset\
.from_tensor_slices((dict(data),
label))
if shuffle:
ds = ds.shuffle(1000)
ds = ds.batch(batch_size)\
.repeat(num_epochs)
return ds
return input_function
train_input_fn = make_input_fn(x_train, y_train)
val_input_fn = make_input_fn(x_val, y_val, num_epochs=1, shuffle=False)
Now we are good to go to train the tf.estimator.LinearClassifier model using the titanic dataset. Linear Classifier as the name suggests is a Linear model which is used to learn decision boundaries between multiple classes of the object but that should be Linear not non-Linear as we do so in the SVM algorithm.
LinearClassifier Model
Python3
linear_est = tf.estimator.LinearClassifier(feature_columns=feat_cols)
linear_est.train(train_input_fn)
result = linear_est.evaluate(val_input_fn)
print(result)
Output:
{'accuracy': 0.75,
'accuracy_baseline': 0.625,
'auc': 0.8377411,
'auc_precision_recall': 0.7833674,
'average_loss': 0.47364476,
'label/mean': 0.375, 'loss': 0.4666896,
'precision': 0.6666667,
'prediction/mean': 0.37083066,
'recall': 0.6666667,
'global_step': 200}
Here we can observe that the model has been evaluated on multiple matrices using the validation dataset and the accuracy obtained is also very satisfactory.
Similar Reads
Tensorflow.js tf.Tensor Class Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. A tf.Tensor object represents an immutable, multidimensional array of numbers that has a shape and a data type. Tensors are the core d
4 min read
Convolutional Layers in TensorFlow Convolutional layers are the foundation of Convolutional Neural Networks (CNNs), which excel at processing spatial data such as images, time-series data, and volumetric data. These layers apply convolutional filters to extract meaningful features like edges, textures, and patterns. List of Convoluti
2 min read
Neural Network Layers in TensorFlow TensorFlow provides powerful tools for building and training neural networks. Neural network layers process data and learn features to make accurate predictions. A neural network consists of multiple layers, each serving a specific purpose. These layers include:Input Layer: The entry point for data.
2 min read
Tensor Indexing in Tensorflow In the realm of machine learning and deep learning, tensors are fundamental data structures used to represent numerical data with multiple dimensions. TensorFlow, a powerful numerical computation library, equips you with an intuitive and versatile set of operations for manipulating and accessing dat
10 min read
Debugging in TensorFlow This article discusses the basics of TensorFlow and also dives deep into debugging in TensorFlow in Python. We will see debugging techniques, and debugging tools, and also get to know about common TensorFlow errors. TensorFlow TensorFlow is an open-source library that helps develop, deploy, and trai
8 min read
Single Layer Perceptron in TensorFlow Single Layer Perceptron is inspired by biological neurons and their ability to process information. To understand the SLP we first need to break down the workings of a single artificial neuron which is the fundamental building block of neural networks. An artificial neuron is a simplified computatio
4 min read
tf.Module in Tensorflow Example TensorFlow is an open-source library for data science. It provides various tools and APIs. One of the core components of TensorFlow is tf.Module, a class that represents a reusable piece of computation. A tf.Module is an object that encapsulates a set of variables and functions that operate on them.
6 min read
TensorArray in TensorFlow In TensorFlow, a tensor is a multi-dimensional array or data structure representing data. It's the fundamental building block of TensorFlow computations. A tensor can be a scalar (0-D tensor), a vector (1-D tensor), a matrix (2-D tensor), or it can have higher dimensions. In this article, we are goi
6 min read
tf.function in TensorFlow TensorFlow is a machine learning framework that has offered flexibility, scalability and performance for deep learning tasks. tf.function helps to optimize and accelerate computation by leveraging graph-based execution. In the article, we will cover the concept of tf.function in TensorFlow. Table of
5 min read