{
  "id": "one-hot-encoding",
  "title": "One-Hot Encoding",
  "lang": "en",
  "source_lang": "en",
  "summary": "One-hot encoding represents each category as a binary vector with exactly one active component, enabling categorical data to be processed numerically.",
  "category": "computer-science",
  "subcategory": "artificial-intelligence",
  "type": "concept",
  "aliases": [
    "One-of-K encoding",
    "1-of-N encoding"
  ],
  "generated_at": "2026-10-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/one-hot-encoding/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/one-hot-encoding/",
      "json": "https://aiwiki.page/zh/one-hot-encoding.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "One-hot encoding is a representation of a categorical value by a vector containing exactly one component equal to 1 and all other components equal to 0. Each position corresponds to a distinct category. In machine learning, it converts discrete labels into numerical inputs without assigning them an artificial order. In digital electronics, the same principle represents states using separate active bits. The number of components equals the number of categories or states represented. (developers.google.com)"
    },
    {
      "heading": "Definition and mathematical structure",
      "text": "For a category set \\(C=\\{c_1,\\ldots,c_K\\}\\), the encoding maps \\(c_i\\) to the vector \\(e_i\\in\\{0,1\\}^K\\), defined by\n\n\\[(e_i)_j= \\begin{cases} 1,&j=i,\\\\ 0,&j\\ne i. \\end{cases}\\]\n\nEach coordinate is an indicator function for membership in one category. For example, a vocabulary ordered as red, green, and blue gives:\n\nCategory | Red | Green | Blue\nRed | 1 | 0 | 0\nGreen | 0 | 1 | 0\nBlue | 0 | 0 | 1\n\nThe category order fixes the meaning of each coordinate; it does not establish a ranking among categories. (developers.google.com)\n\nViewed within a real vector space, these vectors form the standard orthonormal basis of \\(\\mathbb{R}^K\\). Consequently, distinct categories have inner product zero and Euclidean distance \\(\\sqrt{2}\\). These mathematical consequences explain why the representation distinguishes identities but does not itself express semantic similarity: two closely related categories are initially as far apart as two unrelated ones. (developers.google.com)"
    },
    {
      "heading": "Categorical features and statistical models",
      "text": "One-hot encoding is a common feature-engineering transformation for nominal variables, such as product type or geographic region. Assigning these categories consecutive integers can introduce unintended numerical relationships when a model treats the codes as quantities. One-hot encoding instead supplies a separate feature for each category, allowing category-specific model parameters. It is widely used with linear models and support vector machines. (developers.google.com)\n\nFor multiple categorical variables, their encoded vectors are concatenated into separate blocks. If the variables have \\(K_1,\\ldots,K_m\\) categories, the resulting row has \\(\\sum_r K_r\\) components and ordinarily \\(m\\) ones. Thus, “one-hot” applies to each categorical block, not necessarily to the entire feature vector. Encoding a dataset produces a matrix whose columns retain fixed category meanings. (scikit-learn.org)\n\nIn linear regression with an intercept, retaining every indicator column creates a dependency: the columns for one categorical variable sum to the intercept column. The resulting design is not of full rank, so its coefficients are not uniquely identifiable without additional constraints. A common alternative retains \\(K-1\\) indicators and treats the omitted category as a reference. This reduced representation is often called dummy coding; its reference category has an all-zero vector and is therefore not strictly one-hot. Dropping a category is not universally necessary, and it can affect models using regularization by breaking symmetry among categories. (scikit-learn.org)"
    },
    {
      "heading": "Classification targets and embeddings",
      "text": "In supervised learning, one-hot vectors can represent mutually exclusive target classes. If \\(y=e_i\\) and a model predicts class probabilities \\(p_j\\), the cross-entropy loss becomes\n\n\\[L=-\\sum_{j=1}^{K}y_j\\log p_j=-\\log p_i.\\]\n\nA softmax output commonly supplies these probabilities. Explicit target vectors are not always required: some loss implementations accept class indices and calculate the equivalent objective directly. Soft targets, including smoothed labels, can contain several nonzero probabilities and are not one-hot vectors. (docs.pytorch.org)\n\nIn natural language processing, vocabulary items can likewise be represented by one-hot vectors. A learned embedding transforms this sparse identity representation into a dense vector. If an embedding matrix \\(E\\) stores one vector per row, then \\(e_i^\\top E\\) selects row \\(i\\). This algebraic interpretation underlies word embeddings; implementations can perform an indexed lookup without constructing the full one-hot input. The embedding’s learned geometry, unlike the original encoding, can capture relationships relevant to the training task. (developers.google.com)"
    },
    {
      "heading": "Storage and vocabulary management",
      "text": "Large category vocabularies create high-dimensional representations. A dense dataset with \\(n\\) observations and \\(K\\) categories stores \\(nK\\) entries, although only \\(n\\) entries are nonzero for one categorical variable. A sparse matrix stores nonzero values and their positions more efficiently. Sparse inputs, however, do not automatically eliminate the cost of a large model: connecting \\(K\\) inputs to \\(h\\) neural units still introduces \\(Kh\\) weights. Embeddings and feature hashing offer alternative representations when vocabulary size is large. (developers.google.com)\n\nAn encoder requires a stable vocabulary and column order. Categories may be specified externally or learned from training data. During evaluation, learned preprocessing is fitted within the training portion and reused on held-out observations; fitting it using the test set risks data leakage. The same separation applies within cross-validation folds. (scikit-learn.org)\n\nPreviously unseen categories require an explicit policy. Implementations may reject them, map them to an unknown-category column, or return an all-zero block. Rare categories can be grouped into a shared bucket. An all-zero block is an operational extension rather than a strict one-hot vector; if a reference category was also dropped, the two cases may become indistinguishable. (scikit-learn.org)"
    },
    {
      "heading": "Digital state encoding",
      "text": "In a finite-state machine, one-hot encoding assigns one storage bit to each state, with exactly one asserted during valid operation. A \\(K\\)-state machine therefore uses \\(K\\) state bits rather than the \\(\\lceil\\log_2K\\rceil\\) bits sufficient for compact binary encoding. This can simplify decoding and improve timing, at the cost of additional storage. Actual area and performance depend on the device and synthesized logic. All-zero and multiple-active-bit patterns are invalid under the strict scheme; recovery logic can explicitly return such patterns to a reset state. (intel.com)"
    }
  ],
  "links": [
    "machine-learning",
    "bit",
    "indicator-function",
    "vector-space",
    "orthonormal-basis",
    "inner-product",
    "euclidean-distance",
    "feature-engineering",
    "integer",
    "support-vector-machine",
    "matrix",
    "linear-regression",
    "matrix-rank",
    "regularization",
    "supervised-learning",
    "cross-entropy",
    "loss-function",
    "softmax-function",
    "natural-language-processing",
    "word-embedding",
    "sparse-matrix",
    "training-data",
    "test-set",
    "data-leakage",
    "cross-validation",
    "finite-state-machine"
  ],
  "sources": [
    {
      "title": "developers.google.com/machine-learning/crash-course/categorical-data/one-hot-en…",
      "url": "https://developers.google.com/machine-learning/crash-course/categorical-data/one-hot-encoding"
    },
    {
      "title": "scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder",
      "url": "https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder"
    },
    {
      "title": "docs.pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html",
      "url": "https://docs.pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html"
    },
    {
      "title": "developers.google.com/machine-learning/crash-course/embeddings/obtaining-embedd…",
      "url": "https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings"
    },
    {
      "title": "intel.com/content/www/us/en/docs/programmable/683122/current/fsm-compiler.html",
      "url": "https://www.intel.com/content/www/us/en/docs/programmable/683122/current/fsm-compiler.html"
    }
  ],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}