liveBook · Manning

Binary feature

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A binary feature is an input feature whose value has two possible states, such as indicating whether another value is missing.

A binary feature, also called a dichotomous variable, is a categorical input with one label and two states: usually 0 for absence and 1 for presence. It records whether a quality applies, rather than how much of that quality exists. Binary features are a simple special case of low-cardinality categorical data. section 2.1.2

Use a binary feature when presence or absence is useful to a prediction task. Missingness can itself carry information: after a database join, a missing value may mean that no record matched; in survey data, nonresponse can reflect how data was generated. A separate missing-value flag preserves that pattern instead of leaving it implicit. section 2.2.4

Text fields can yield binary flags through simple presence checks. In pandas, search the string and convert the Boolean result to integers:

  • `is_new`: whether a listing description contains “new.” section 7.1.2
  • `is_studio`: whether it contains “studio.” section 7.1.2
  • `has_shared_bath` and `has_half_bath`: whether it contains “shared” or “half.” section 7.1.2

Turning listing text into flags

For an accommodation-name column, `str.contains('studio', case=False).astype(int)` produces 1 when “studio” appears and 0 otherwise. The same pattern creates flags for “new,” “shared,” and “half,” transforming partially structured text into model-ready columns. Use regex when the task requires extracting values such as ratings or bed counts; use a binary flag when only presence matters. section 7.1.2 section 7.1.2

Listing 7.2 Extracting binary flags and time information
import numpy as np
import pandas as pd
from datetime import datetime
 
summary_listings = summary_listings.assign(
    is_new=(summary_listings['name']
               .str.contains('new', case=False)
               .astype(int)),                              #1
    is_studio=(summary_listings['name']
               .str.contains('studio', case=False)
               .astype(int)),                              #2
    has_shared_bath=(summary_listings['name']
                     .str.contains('shared', case=False)
                     .astype(int)),                        #3
    has_half_bath=(summary_listings['name']
                   .str.contains('half', case=False)
                   .astype(int)),                          #4
)
 
summary_listings['days_since_last_review'] = (
    datetime.today() – 
    pd.to_datetime(
        summary_listings['last_review'])
).dt.days
summary_listings['days_since_last_review'] = (
    summary_listings['days_since_last_review'] -
    summary_listings['days_since_last_review'].min()
)                                                          #5
 
zero_reviews = summary_listings['number_of_reviews'] == 0
ratio = summary_listings['number_of_reviews_ltm'] / 
summary_listings['number_of_reviews']
summary_listings['number_of_reviews_ltm_ratio'] = (
    np.where(zero_reviews, 0, ratio)
)                                                          #6

This listing shows four binary flags created from listing descriptions with case-insensitive pandas string checks and integer conversion. section 7.1.2

Binary features differ from numeric measurements and ordered levels because their values represent a yes-or-no quality. They can also be created from data-quality conditions, such as whether an original value is missing. Keep the meaning of 0 and 1 explicit so that the column remains interpretable during preparation and modeling. section 2.1.2 section 2.2.4

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines binary variables and demonstrates pandas-based text flags, while referring readers to MissingIndicator for missingness processing details.
Examples
Uses Airbnb listing names to flag new listings, studios, shared baths, and half-baths, plus missing cases from joins and census responses.
What it assumes
Assumes familiarity with tabular columns and introduces binary features as 0/1 presence-or-absence variables.
What you can do after
Apply it via pandas string checks or Scikit-learn MissingIndicator to create presence and missingness flags.
How it connects
Connects binary features with categorical features, missing data, database joins, regular expressions, and pandas string matching.

Also explained in

Compare how 2 books explain binary feature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.