Title: Discretize Features
Slug: discretize_features
Summary: How to discretize features for machine learning in Python.
Date: 2016-09-06 12:00
Category: Machine Learning
Tags: Preprocessing Structured Data
Authors: Chris Albon
In [1]:
# Load libraries
from sklearn.preprocessing import Binarizer
import numpy as np
In [2]:
# Create feature
age = np.array([[6],
[12],
[20],
[36],
[65]])
In [3]:
# Create binarizer
binarizer = Binarizer(18)
# Transform feature
binarizer.fit_transform(age)
Out[3]:
In [4]:
# Bin feature
np.digitize(age, bins=[20,30,64])
Out[4]: