[null,null,["最后更新时间 (UTC):2025-08-06。"],[[["\u003cp\u003eDimension refers to the number of elements in a feature vector, and some categorical features have low dimensionality.\u003c/p\u003e\n"],["\u003cp\u003eMachine learning models require numerical input; therefore, categorical data like strings must be converted to numerical representations.\u003c/p\u003e\n"],["\u003cp\u003eOne-hot encoding transforms categorical values into numerical vectors where each category is represented by a unique element with a value of 1.\u003c/p\u003e\n"],["\u003cp\u003eFor high-dimensional categorical features with numerous categories, one-hot encoding might be inefficient, and embeddings or hashing are recommended.\u003c/p\u003e\n"],["\u003cp\u003eSparse representation efficiently stores one-hot encoded data by only recording the position of the '1' value to reduce memory usage.\u003c/p\u003e\n"]]],[],null,["# Categorical data: Vocabulary and one-hot encoding\n\nThe term **dimension** is a synonym for the number of elements in a\n[**feature vector**](/machine-learning/glossary#feature_vector).\nSome categorical features are low dimensional. For example:\n\n| Feature name | # of categories | Sample categories |\n|--------------|-----------------|--------------------------------|\n| snowed_today | 2 | True, False |\n| skill_level | 3 | Beginner, Practitioner, Expert |\n| season | 4 | Winter, Spring, Summer, Autumn |\n| day_of_week | 7 | Monday, Tuesday, Wednesday |\n| planet | 8 | Mercury, Venus, Earth |\n\nWhen a categorical feature has a low number of possible categories, you can\nencode it as a **vocabulary** . With a vocabulary encoding, the model treats each\npossible categorical value as a *separate feature*. During training, the\nmodel learns different weights for each category.\n\nFor example, suppose you are creating a model to predict a car's price based,\nin part, on a categorical feature named `car_color`.\nPerhaps red cars are worth more than green cars.\nSince manufacturers offer a limited number of exterior colors, `car_color` is\na low-dimensional categorical feature.\nThe following illustration suggests a vocabulary (possible values) for\n`car_color`:\n**Figure 1.** A unique feature for each category.\n\nExercise: Check your understanding\n----------------------------------\n\nTrue or False: A machine learning model can train *directly* on raw string values, like \"Red\" and \"Black\", without converting these values to numerical vectors. \nTrue \nDuring training, a model can only manipulate floating-point numbers. The string `\"Red\"` is not a floating-point number. You must convert strings like `\"Red\"` to floating-point numbers. \nFalse \nA machine learning model can only train on features with floating-point values, so you'll need to convert those strings to floating-point values before training.\n\nIndex numbers\n-------------\n\nMachine learning models can only manipulate floating-point numbers.\nTherefore, you must convert each string to a unique index number, as in\nthe following illustration:\n**Figure 2.** Indexed features.\n\nAfter converting strings to unique index numbers, you'll need to process the\ndata further to represent it in ways that help the model learn meaningful\nrelationships between the values. If the categorical feature data is left as\nindexed integers and loaded into a model, the model would treat the indexed\nvalues as continuous floating-point numbers. The model would then consider\n\"purple\" six times more likely than \"orange.\"\n\nOne-hot encoding\n----------------\n\nThe next step in building a vocabulary is to convert each index number to\nits [**one-hot encoding**](/machine-learning/glossary#one-hot_encoding).\nIn a one-hot encoding:\n\n- Each category is represented by a vector (array) of N elements, where N is the number of categories. For example, if `car_color` has eight possible categories, then the one-hot vector representing will have eight elements.\n- Exactly *one* of the elements in a one-hot vector has the value 1.0; all the remaining elements have the value 0.0.\n\nFor example, the following table shows the one-hot encoding for each color in\n`car_color`:\n\n| Feature | Red | Orange | Blue | Yellow | Green | Black | Purple | Brown |\n|----------|-----|--------|------|--------|-------|-------|--------|-------|\n| \"Red\" | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n| \"Orange\" | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n| \"Blue\" | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |\n| \"Yellow\" | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |\n| \"Green\" | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |\n| \"Black\" | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |\n| \"Purple\" | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |\n| \"Brown\" | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |\n\nIt is the one-hot vector, not the string or the index number, that gets passed\nto the feature vector. The model learns a separate weight for each element of\nthe feature vector.\n| **Note:** In a true one-hot encoding, only one element has the value 1.0. In a variant known as **multi-hot encoding**, multiple values can be 1.0.\n\nThe following illustration suggests the various transformations in the\nvocabulary representation:\n**Figure 3.** The end-to-end process to map categories to feature vectors.\n\n### Sparse representation\n\nA feature whose values are predominantly zero (or empty) is termed a\n[**sparse feature**](/machine-learning/glossary#sparse-feature). Many\ncategorical features, such as `car_color`, tend to be sparse features.\n[**Sparse representation**](/machine-learning/glossary#sparse-representation)\nmeans storing the *position* of the 1.0\nin a sparse vector. For example, the one-hot vector for `\"Blue\"` is:\n\u003e \\[0, 0, 1, 0, 0, 0, 0, 0\\]\n\nSince the `1` is in position 2 (when starting the count at 0), the\nsparse representation for the preceding one-hot vector is:\n\u003e 2\n\nNotice that the sparse representation consumes far less memory than the\neight-element one-hot vector. Importantly, the model must *train* on the\none-hot vector, not the sparse representation.\n| **Note:** The sparse representation of a multi-hot encoding stores the positions of *all* the nonzero elements. For example, the sparse representation of a car that is both `\"Blue\"` and `\"Black\"` is `2, 5`.\n\nOutliers in categorical data\n----------------------------\n\nLike numerical data, categorical data also contains outliers. Suppose\n`car_color` contains not only the popular colors, but also some rarely used\noutlier colors, such as `\"Mauve\"` or `\"Avocado\"`.\nRather than giving each of these outlier colors a separate category, you\ncan lump them into a single \"catch-all\" category called *out-of-vocabulary\n(OOV)*. In other words, all the outlier colors are binned into a single\noutlier bucket. The system learns a single weight for that outlier bucket.\n\nEncoding high-dimensional categorical features\n----------------------------------------------\n\nSome categorical features have a high number of dimensions, such as\nthose in the following table:\n\n| Feature name | # of categories | Sample categories |\n|-----------------------|-----------------|------------------------|\n| words_in_english | \\~500,000 | \"happy\", \"walking\" |\n| US_postal_codes | \\~42,000 | \"02114\", \"90301\" |\n| last_names_in_Germany | \\~850,000 | \"Schmidt\", \"Schneider\" |\n\nWhen the number of categories is high, one-hot encoding is usually a bad choice.\n*Embeddings* , detailed in a separate\n[Embeddings module](/machine-learning/crash-course/embeddings), are usually\na much better choice. Embeddings substantially reduce the number of\ndimensions, which benefits models in two important ways:\n\n- The model typically trains faster.\n- The built model typically infers predictions more quickly. That is, the model has lower latency.\n\n[**Hashing**](/machine-learning/glossary#hashing) (also called the *hashing\ntrick*) is a less common way to reduce the number of dimensions.\n**Click here to learn about hashing** \nIn brief, hashing maps a category (for example, a color) to a small\ninteger---the number of the \"bucket\" that will hold that category.\n\nIn detail, you implement a hashing algorithm as follows:\n\n1. Set the number of bins in the vector of categories to N, where N is less than the total number of remaining categories. As an arbitrary example, say N = 100.\n2. Choose a hash function. (Often, you will choose the range of hash values as well.)\n3. Pass each category (for example, a particular color) through that hash function, generating a hash value, say 89237.\n4. Assign each bin an index number of the output hash value modulo N. In this case, where N is 100 and the hash value is 89237, the modulo result is 37 because 89237 % 100 is 37.\n5. Create a one-hot encoding for each bin with these new index numbers.\n\nFor more details about hashing data, see the\n[Randomization](/machine-learning/crash-course/production-ml-systems/monitoring#randomization)\nsection of the\n[Production machine learning systems](/machine-learning/crash-course/production-ml-systems/monitoring)\nmodule.\n| **Key terms:**\n|\n| - [Categorical data](/machine-learning/glossary#categorical_data)\n| - [Dimensions](/machine-learning/glossary#dimensions)\n| - [Discrete feature](/machine-learning/glossary#discrete_feature)\n| - [Feature cross](/machine-learning/glossary#feature_cross)\n| - [Feature vector](/machine-learning/glossary#feature_vector)\n| - [Hashing](/machine-learning/glossary#hashing)\n| - [One-hot encoding](/machine-learning/glossary#one-hot_encoding)\n| - [Sparse feature](/machine-learning/glossary#sparse-feature)\n- [Sparse representation](/machine-learning/glossary#sparse-representation) \n[Help Center](https://support.google.com/machinelearningeducation)"]]