v Discretize Features - Machine Learning

Discretize Features

Preliminaries

# Load libraries
from sklearn.preprocessing import Binarizer
import numpy as np

Create Data

# Create feature
age = np.array([[6], 
                [12], 
                [20], 
                [36], 
                [65]])

Option 1: Binarize Feature

# Create binarizer
binarizer = Binarizer(18)

# Transform feature
binarizer.fit_transform(age)
array([[0],
       [0],
       [1],
       [1],
       [1]])

Option 2: Break Up Feature Into Bins

# Bin feature
np.digitize(age, bins=[20,30,64])
array([[0],
       [0],
       [1],
       [2],
       [3]])