liveBook · Manning

Categorical data

As explained in Data Without Labels

A categorical variable records membership in one of a set of qualitative categories rather than a numeric magnitude.

Categorical data records which class an observation belongs to, rather than a measured amount. City, payment mode, product category, and a yes/no outcome are categorical; revenue and height are quantitative. A categorical variable can be binary (two mutually exclusive classes), nominal (classes without an order), or ordinal (classes with an order). section 1.2.2

Categories can be inputs to an analysis or the outcome to predict. Predicting whether a transaction is fraudulent or genuine is a classification problem; predicting a continuous amount is a regression problem. Before modeling, check what the categories mean and whether their values provide useful variation. A city field dominated by one value, or a zip-code field with nearly every value distinct, may add little to an analysis. section 1.5.1 section 11.6

Some algorithms need numeric inputs. K-means clustering, for example, calculates distances between data points and cannot directly use category names. One-hot encoding converts each distinct category into a separate column marked 0 or 1. For a city field containing London and New Delhi, that means two added columns. If it contains 100 cities, it means 100 mostly zero-filled columns; grouping some values may be more practical. section 2.7

Figure 2.36 Using one-hot encoding to convert categorical variables into numeric ones
figure

The figure shows how a city category becomes numeric indicator columns. section 2.7

Prepare categorical inputs in Python

In a mushroom dataset, inspect the shape and first rows before encoding categorical values. One workflow applies scikit-learn’s LabelEncoder to each column, separates features from the edible-or-poisonous class, scales the features with StandardScaler, and passes them to NumPy’s SVD. The shown LabelEncoder code assigns numeric labels; it does not create the separate indicator columns used in one-hot encoding. For one-hot columns, pandas provides get_dummies(). section 3.6.1 section 11.6 section 2.7

How Data Without Labels covers it

How deep it goes
Defines binary, nominal, and ordinal types, then demonstrates numeric conversion and warns about sparse columns and low-value fields.
Examples
Uses city indicator columns, low-variation city and zip-code fields, and edible-or-poisonous mushroom classes.
What it assumes
Introduces qualitative categories from everyday examples; assumes familiarity with basic Python dataframes for the encoding workflow.
What you can do after
Apply it via pandas get_dummies() or scikit-learn LabelEncoder, while checking whether category counts and variation make a field useful.
How it connects
Connects categorical data to classification, regression, k-means clustering, one-hot encoding, and singular value decomposition.

Also explained in

Compare how 16 books explain categorical data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Without Labels. The section links point to the parts of the book it draws on.