Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is Binary Classification?
Building Binary Classifier:
Data Set Description
Load the Dataset
Extract Feature With CountVectorizer
Fit the Data
Naive Bayes Classifier
Sample Output
Visualizing the Output and Data
Sample Output
Frequently Asked Questions:
Key Takeaways
Last Updated: Mar 27, 2024

Binary Classification

Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


Have you ever used any detection algorithm where it classifies? For example, let's think of classifying emails into “Spam” and “Not Spam”, so here we label them as Spam - 1 and Not spam-0. The most common examples of Binary Classification are Credit Card Fraud detection, Sentimental analysis. Let’s develop a simple sample model for Binary Classification to understand better.



Also See, Resnet 50 Architecture

What is Binary Classification?

Binary classification is the task of classifying the elements of a set into two groups on the basis of a classification rule. 

In the below graph, we have two classes ‘0’ and ‘1’, where 0→ is represented with blue and 1→ with orange color. Based on the classes a graph is plotted to differentiate two classes.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Building Binary Classifier:

The first and foremost step to build any model is to import libraries like pandas, sklearn. Keras and Tensorflow are mostly used libraries to build a model because they provide better accuracy and fewer entropy values.


from flask import Flask, render_template, request
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split

Data Set Description

So let’s take an easy example to implement a binary classification algorithm. Here we are going to consider the Spam classifier model. Here we have 87% of “Not Spam” and 13% of “Spam”.


Download your dataset here.

Load the Dataset

In the below code we are loading our data set and labeling our features as “Spam” → 1 and “Not spam/ Ham” → 0.  

df = pd.read_csv("spam.csv", encoding="latin-1")
df.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis=1, inplace=True)
# Features and Labels
df['label'] = df['class'].map({'ham': 0, 'spam': 1})
X = df['message']
y = df['label']

Extract Feature With CountVectorizer

Generally, whenever a data set is taken, it always needs to be cleaned. In terms of machine learning, we need to extract the important features from the dataset. To extract the features here we are using the CountVectorizer algorithm.

# Extract Feature With CountVectorizer
    cv = CountVectorizer()

Fit the Data

To fit the data into a model we need to split our data set into train and test data. In general, the data is divided in the ratio of 8:2.

X = cv.fit_transform(X)  # Fit the Data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

Naive Bayes Classifier

Naive Bayes classifiers are a collection of classification algorithms based on Bayes' Theorem. It is not a single algorithm but a family of algorithms where all of them share a common principle, i.e. every pair of features being classified is independent of each other.

Naive Bayes uses a similar method to predict the probability of different classes based on various attributes. This algorithm is mostly used in text classification and with problems having multiple classes.


To train the Spam Classifier we are using Naive Bayes as this is one of the best algorithms for classifying.

# Naive Bayes Classifier
  clf = MultinomialNB(), y_train)
  clf.score(X_test, y_test)
  if request.method == 'POST':
      message = request.form['message']
      data = [message]
      vect = cv.transform(data).toarray()
       my_prediction = clf.predict(vect)
  return render_template('index.html', prediction=my_prediction)

This code snippet returns the prediction of your given text and Spam classifier.

Sample Output


Visualizing the Output and Data

Visualizing data is the most important part while developing any machine learning model. 


Sample Output


import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
acc = hist.history['accuracy']
val = hist.history['val_accuracy']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, '-', label='Training accuracy')
plt.plot(epochs, val, ':', label='Validation accuracy')
plt.title('Training and Validation Accuracy')
plt.legend(loc='lower right')

Also read, Artificial Intelligence in Education

Frequently Asked Questions:

  1. What do you mean by Binary Classification?
    Binary Classification helps to differentiate two classes by different means like plotting graphs, labeling, etc.
  2. What is Binary text Classification?
    It is a supervised learning algorithm in which we try to predict whether a piece of text follows into some category.
  3. List some Binary Classifiers?
    Naive Bayes, K- Nearest Neighbours, Support Vector Machine are some of the Binary Classifiers.
  4. What is Binary Classification Data set?
    The goal of a binary classification problem is to create a machine learning model that makes a prediction in situations where the thing to predict can take one of just two possible values.

Key Takeaways

In this blog, we discussed 

  • Binary Classification
  • Developing a Binary Classification Model
  • Understanding the Binary Classification Model

To learn more about Machine Learning, take this awesome course from CodingNinjas.

Next article
Live masterclass