One Hot Encoding of a Categorical Variable in a Table

An alternative to MATLAB's dummyvar command for creating ONE HOT ENCODING of categorical data in a table
456 Downloads
Updated 30 Jan 2019

View License

Code written by Christopher L. Stokely, January 30, 2019
Written in MATLAB R2018B.

Command:
outputTable = createOneHotEncoding(T,tableVariable)

Input variable T needs to be a table and the tableVariable should be
a variable in that table. tableVariable should be a variable that is
categorical but it does not have to be. The code below converts the
variable to categorical if it is not already so. A table will be
returned that is the original input table without tableVariable, but
with new variables representing the one-hot encoded tableVariable.

By one hot encoding, predictor importances can become very useful
when employing machine learning - from a model interpretability stand
-point. Being able to assign an importance to an individual category
can be useful and important in some cases.

For educational purposes, try looking into these Machine Learning
toolbox commands after building a model:
1) oobPermutedPredictorImportance
2) predictorImportance (Be careful - this one is known to mislead)
3) FeatureSelectionNCARRegression
4) fsrnca or fscnca
5) sequentialfs
6) plotPartialDependence
7) Individual Conditional Expectation (ICE) plots

Note a MATLAB bug or oversight from MathWorks regarding having an
underscore in the variable names that are in the table...
Note that the output table has new variables with labels that have an
underscore. Removing these variables with "removevars" requires the
user to specify the column to be removed with the column number, not
the variable name. Otherwise unintended columns will be deleted.

Cite As

Christopher Stokely (2024). One Hot Encoding of a Categorical Variable in a Table (https://www.mathworks.com/matlabcentral/fileexchange/70140-one-hot-encoding-of-a-categorical-variable-in-a-table), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Import Data in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0