Binarization
Binarization is used when you want to convert a numerical feature vector into a Boolean vector. In the field of digital image processing, image binarization is the process by which a color or grayscale image is transformed into a binary image, that is, an image with only two colors (typically, black and white).
When it is used?
This technique is used for the recognition of objects, shapes, and, specifically, characters. Through binarization, it is possible to distinguish the object of interest from the background on which it is found. Skeletonization is instead an essential and schematic representation of the object, which generally preludes the subsequent real recognition.
Data Pre-processing using Binarization
Let's see how to binarize data in Python:
To binarize data, we will use the preprocessing.Binarizer() function as follows
Syntax: class sklearn.preprocessing.Binarizer(*, threshold=0.0, copy=True)
Binarize data (set feature values to 0 or 1) according to a threshold.
Values greater than the threshold map to 1, while values less than or equal to the threshold map to 0. With the default threshold of 0, only positive values map to 1.
Binarization is a common operation on text count data where the analyst can decide to only consider the presence or absence of a feature rather than a quantified number of occurrences for instance.
It can also be used as a pre-processing step for estimators that consider boolean random variables (e.g. modeled using the Bernoulli distribution in a Bayesian setting).
Example 1:
import numpy as np from sklearn.preprocessing import Binarizer data = np.array([[3, -1.5, 2, -5.4], [0, 4, -0.3, 2.1], [1, 3.3, -1.9, -4.3]]) print("---Orogonal data.............") print(data) data_binarized = Binarizer(threshold=1.4).transform(data) print("...After Binarizer...... ") print(data_binarized)
Output
---Orogonal data............. [[ 3. -1.5 2. -5.4] [ 0. 4. -0.3 2.1] [ 1. 3.3 -1.9 -4.3]] ...After Binarizer...... [[1. 0. 1. 0.] [0. 1. 0. 1.] [0. 1. 0. 0.]]
The preprocessing.Binarizer() function binarizes data according to an imposed threshold. Values greater than the threshold map to 1, while values less than or equal to the threshold map to 0. With the default threshold of 0, only positive values map to 1. In our case, the threshold imposed is 1.4, so values greater than 1.4 are mapped to 1, while values less than 1.4 are mapped to 0.
Example 2:
from sklearn.preprocessing import Binarizer X = [[ 1., -1., 2.], [ 2., 0., 0.], [ 0., 1., -1.]] transformer = Binarizer().fit(X) # fit does nothing. print(transformer) t=transformer.transform(X) print(t)
Output:-
Binarizer() [[1. 0. 1.] [1. 0. 0.] [0. 1. 0.]]
How is Binarization used?
In machine learning, even the most complex concepts can be transformed into binary form. For example, to binarize the sentence “The dog ate the cat,” every word is assigned an ID (for example dog-1, ate-2, the-3, cat-4). Then replace each word with the tag to provide a binary vector. In this case the vector: <3,1,2,3,4> can be refined by providing each word with four possible slots, then setting the slot to correspond with a specific word: <0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,1>. This is commonly referred to as the bag-of-words-method.
Since the ultimate goal is to make this data easier for the classifier to read while minimizing memory usage, it’s not always necessary to encode the whole sentence or all the details of a complex concept. In this case, only the current state of how the data is parsed is needed for the classifier. For example, when the top word on the stack is used as the first word in the input queue. Since order is quite important, a simpler binary vector is preferable.
Source:
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.