SKLearner Home | About | Contact | Examples

Scikit-Learn binarize() for Data Preprocessing

Transforming continuous data into binary values based on a threshold is a common preprocessing step in machine learning.

The binarize() function from scikit-learn can be used to convert numerical data into binary format, making it easier to handle for certain models.

The key parameter of binarize() is threshold, which determines the cut-off point for binarization.

Values above the threshold are set to 1, and values below or equal to the threshold are set to 0.

This function is particularly useful for feature engineering and simplifying datasets for algorithms that require binary input.

from sklearn.preprocessing import binarize
import numpy as np

# create a synthetic dataset
data = np.array([[0.1, -1.1, 2.3], [1.2, 0.3, -0.7], [0.8, -0.5, 1.5]])

# apply binarization with a threshold of 0.5
binarized_data = binarize(data, threshold=0.5)

# print the original and binarized datasets
print("Original Data:\n", data)
print("Binarized Data:\n", binarized_data)

Running the example gives an output like:

Original Data:
 [[ 0.1 -1.1  2.3]
 [ 1.2  0.3 -0.7]
 [ 0.8 -0.5  1.5]]
Binarized Data:
 [[0. 0. 1.]
 [1. 0. 0.]
 [1. 0. 1.]]

The steps are as follows:

  1. Generate synthetic data: Create a small 2D numpy array with random numerical values.
  2. Apply binarize(): Use binarize() with a threshold of 0.5 to transform the data into binary values.
  3. Output: Display the original and transformed datasets to demonstrate the effect of binarization.

This example shows how to use the binarize() function from scikit-learn to transform a dataset by applying a specified threshold, converting numerical values to binary values, which is useful for preprocessing steps in machine learning workflows.



See Also