The Wayback Machine - https://web.archive.org/web/20240620073137/https://www.geeksforgeeks.org/python-binning-method-for-data-smoothing/
Open In App

Python | Binning method for data smoothing

Last Updated : 13 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: ML | Binning or Discretization Binning method is used to smoothing data or to handle noisy data. In this method, the data is first sorted and then the sorted values are distributed into a number of buckets or bins. As binning methods consult the neighbourhood of values, they perform local smoothing. There are three approaches to performing smoothing –

Smoothing by bin means : In smoothing by bin means, each value in a bin is replaced by the mean value of the bin. Smoothing by bin median : In this method each bin value is replaced by its bin median value. Smoothing by bin boundary : In smoothing by bin boundaries, the minimum and maximum values in a given bin are identified as the bin boundaries. Each bin value is then replaced by the closest boundary value.

Approach:

  1. Sort the array of a given data set.
  2. Divides the range into N intervals, each containing the approximately same number of samples(Equal-depth partitioning).
  3. Store mean/ median/ boundaries in each row.

Examples:

Sorted data for price (in dollars): 4, 8, 9, 15, 21, 21, 24, 25, 26, 28, 29, 34

Partition using equal frequency approach:
      - Bin 1 : 4, 8, 9, 15
      - Bin 2 : 21, 21, 24, 25
      - Bin 3 : 26, 28, 29, 34

Smoothing by bin means:
      - Bin 1: 9, 9, 9, 9
      - Bin 2: 23, 23, 23, 23
      - Bin 3: 29, 29, 29, 29

Smoothing by bin boundaries:
      - Bin 1: 4, 4, 4, 15
      - Bin 2: 21, 21, 25, 25
      - Bin 3: 26, 26, 26, 34

Smoothing by bin median:
      - Bin 1: 9 9, 9, 9
      - Bin 2: 23, 23, 23, 23
      - Bin 3: 29, 29, 29, 29

  Below is the Python implementation for the above algorithm – 

Python3




import numpy as np
import math
from sklearn.datasets import load_iris
from sklearn import datasets, linear_model, metrics
 
# load iris data set
dataset = load_iris()
a = dataset.data
b = np.zeros(150)
 
# take 1st column among 4 column of data set
for i in range (150):
    b[i]=a[i,1]
 
b=np.sort(b) #sort the array
 
# create bins
bin1=np.zeros((30,5))
bin2=np.zeros((30,5))
bin3=np.zeros((30,5))
 
# Bin mean
for i in range (0,150,5):
    k=int(i/5)
    mean=(b[i] + b[i+1] + b[i+2] + b[i+3] + b[i+4])/5
    for j in range(5):
        bin1[k,j]=mean
print("Bin Mean: \n",bin1)
     
# Bin boundaries
for i in range (0,150,5):
    k=int(i/5)
    for j in range (5):
        if (b[i+j]-b[i]) < (b[i+4]-b[i+j]):
            bin2[k,j]=b[i]
        else:
            bin2[k,j]=b[i+4]   
print("Bin Boundaries: \n",bin2)
 
# Bin median
for i in range (0,150,5):
    k=int(i/5)
    for j in range (5):
        bin3[k,j]=b[i+2]
print("Bin Median: \n",bin3)




Similar Reads

Binning Data In Python With Scipy &amp; Numpy
Binning data is an essential technique in data analysis that enables the transformation of continuous data into discrete intervals, providing a clearer picture of the underlying trends and distributions. In the Python ecosystem, the combination of numpy and scipy libraries offers robust tools for effective data binning. In this article, we'll explo
8 min read
Binning in Data Mining
Data binning, bucketing is a data pre-processing method used to minimize the effects of small observation errors. The original data values are divided into small intervals known as bins and then they are replaced by a general value calculated for that bin. This has a smoothing effect on the input data and may also reduce the chances of overfitting
2 min read
ML | Binning or Discretization
Real-world data tend to be noisy. Noisy data is data with a large amount of additional meaningless information in it called noise. Data cleaning (or data cleansing) routines attempt to smooth out noise while identifying outliers in the data. There are three data smoothing techniques as follows - Binning : Binning methods smooth a sorted data value
7 min read
Exploration with Hexagonal Binning and Contour Plots
Hexagonal binning is a plot of two numeric variables with the records binned into hexagons. The code below is a hexagon binning plot of the relationship between the finished square feet versus the tax-assessed value for homes. Rather than plotting points, records are grouped into hexagonal bins and color indicating the number of records in that bin
2 min read
Smoothing spline
A spline curve is a mathematical representation for which it is easy to build an interface that will allow a user to design and control the shape of complex curves and surfaces. The general approach is that the user enters a sequence of points, and a curve is constructed whose shape closely follows this sequence. The points are called control point
4 min read
Exponential Smoothing for Time Series Forecasting
Exponential smoothing is a popular time series forecasting method known for its simplicity and accuracy in predicting future trends based on historical data. It assumes that future patterns will be similar to recent past data and focuses on learning the average demand level over time. In this article, we will make a detailed analysis of the Exponen
11 min read
Exponential Smoothing Forecast Formula
Forecasting is an essential tool in various fields, including finance, economics, and inventory management. One of the simplest yet powerful methods for forecasting time series data is Exponential Smoothing. This technique is widely used because it can adapt to different types of data patterns with minimal computational effort. In this article, we
6 min read
Difference between Data Scientist, Data Engineer, Data Analyst
In the world of big data and analytics, there are three key roles that are essential to any data-driven organization: data scientist, data engineer, and data analyst. While the job titles may sound similar, there are significant differences between the roles. In this article, we will explore the differences between data scientist, data engineer, an
4 min read
Processing of Raw Data to Tidy Data in R
The data that is download from web or other resources are often hard to analyze. It is often needed to do some processing or cleaning of the dataset in order to prepare it for further downstream analysis, predictive modeling and so on. This article discusses several methods in R to convert the raw dataset into a tidy data. Raw Data A Raw data is a
5 min read
Data Integration in Data Mining
INTRODUCTION : Data integration in data mining refers to the process of combining data from multiple sources into a single, unified view. This can involve cleaning and transforming the data, as well as resolving any inconsistencies or conflicts that may exist between the different sources. The goal of data integration is to make the data more usefu
5 min read