با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
اصطلاح بعد مترادفی برای تعداد عناصر در یک بردار ویژگی است. برخی از ویژگی های طبقه بندی شده دارای ابعاد کم هستند. به عنوان مثال:
نام ویژگی
# دسته بندی
دسته بندی نمونه
برف_امروز
2
درست، نادرست
skill_level
3
مبتدی، کارشناس، متخصص
فصل
4
زمستان، بهار، تابستان، پاییز
روز_هفته
7
دوشنبه، سه شنبه، چهارشنبه
سیاره
8
عطارد، زهره، زمین
هنگامی که یک ویژگی طبقهبندی تعداد دستههای ممکن کم دارد، میتوانید آن را به عنوان یک واژگان رمزگذاری کنید. با رمزگذاری واژگان، مدل با هر مقدار طبقه بندی ممکن به عنوان یک ویژگی جداگانه برخورد می کند. در طول تمرین، مدل وزن های مختلفی را برای هر دسته یاد می گیرد.
برای مثال، فرض کنید در حال ایجاد مدلی برای پیشبینی قیمت خودرو هستید که تا حدی بر اساس یک ویژگی طبقهبندی به نام car_color است. شاید ارزش خودروهای قرمز بیشتر از خودروهای سبز باشد. از آنجایی که تولیدکنندگان تعداد محدودی از رنگ های بیرونی را ارائه می دهند، car_color یک ویژگی طبقه بندی کم بعدی است. تصویر زیر یک واژگان (مقادیر ممکن) برای car_color را پیشنهاد میکند:
شکل 1. یک ویژگی منحصر به فرد برای هر دسته.
تمرین: درک خود را بررسی کنید
درست یا نادرست: یک مدل یادگیری ماشینی میتواند مستقیماً روی مقادیر رشته خام مانند «قرمز» و «سیاه» بدون تبدیل این مقادیر به بردارهای عددی آموزش ببیند.
درست است
در طول آموزش، یک مدل فقط می تواند اعداد ممیز شناور را دستکاری کند. رشته "Red" یک عدد ممیز شناور نیست. شما باید رشته هایی مانند "Red" را به اعداد ممیز شناور تبدیل کنید.
نادرست
یک مدل یادگیری ماشینی فقط میتواند روی ویژگیهایی با مقادیر ممیز شناور آموزش ببیند، بنابراین قبل از آموزش باید آن رشتهها را به مقادیر ممیز شناور تبدیل کنید.
اعداد شاخص
مدل های یادگیری ماشینی فقط می توانند اعداد ممیز شناور را دستکاری کنند. بنابراین، باید هر رشته را مانند شکل زیر به یک عدد شاخص منحصر به فرد تبدیل کنید:
شکل 2. ویژگی های نمایه شده.
پس از تبدیل رشته ها به اعداد شاخص منحصر به فرد، باید داده ها را بیشتر پردازش کنید تا آن ها را به گونه ای نشان دهید که به مدل کمک کند روابط معنی دار بین مقادیر را بیاموزد. اگر داده های ویژگی طبقه بندی شده به عنوان اعداد صحیح نمایه شده رها شوند و در یک مدل بارگذاری شوند، مدل با مقادیر نمایه شده به عنوان اعداد ممیز شناور پیوسته برخورد می کند. سپس مدل «بنفش» را شش برابر بیشتر از «نارنجی» در نظر میگیرد.
رمزگذاری تک داغ
گام بعدی در ساخت واژگان این است که هر عدد شاخص را به رمزگذاری تکی آن تبدیل کنید. در یک رمزگذاری یکباره:
هر دسته با یک بردار (آرایه) از N عنصر نشان داده می شود که N تعداد دسته ها است. به عنوان مثال، اگر car_color دارای هشت دسته ممکن باشد، بردار یک داغ که نشان دهنده هشت عنصر خواهد بود.
دقیقاً یکی از عناصر در یک بردار یک داغ دارای مقدار 1.0 است. تمام عناصر باقی مانده دارای مقدار 0.0 هستند.
به عنوان مثال، جدول زیر کدگذاری تک داغ برای هر رنگ در car_color را نشان می دهد:
ویژگی
قرمز
نارنجی
آبی
زرد
سبز
مشکی
بنفش
قهوه ای
"قرمز"
1
0
0
0
0
0
0
0
"نارنجی"
0
1
0
0
0
0
0
0
"آبی"
0
0
1
0
0
0
0
0
"زرد"
0
0
0
1
0
0
0
0
"سبز"
0
0
0
0
1
0
0
0
"سیاه"
0
0
0
0
0
1
0
0
"بنفش"
0
0
0
0
0
0
1
0
"قهوه ای"
0
0
0
0
0
0
0
1
این بردار یک داغ است، نه رشته یا عدد شاخص که به بردار ویژگی منتقل می شود. مدل وزن جداگانه ای برای هر عنصر از بردار ویژگی یاد می گیرد.
تصویر زیر دگرگونی های مختلف در نمایش واژگان را نشان می دهد:
شکل 3. فرآیند پایان به انتها برای نگاشت دسته ها به بردارهای ویژگی.
نمایندگی پراکنده
مشخصه ای که مقادیر آن عمدتاً صفر (یا خالی) است، ویژگی پراکنده نامیده می شود. بسیاری از ویژگیهای طبقهبندی، مانند car_color ، معمولاً ویژگیهای پراکنده هستند. نمایش Sparse به معنای ذخیره موقعیت 1.0 در یک بردار پراکنده است. به عنوان مثال، بردار یک داغ برای "Blue" عبارت است از:
[0، 0، 1، 0، 0، 0، 0، 0]
از آنجایی که 1 در موقعیت 2 قرار دارد (هنگامی که شمارش را از 0 شروع می کنیم)، نمایش پراکنده برای بردار یک داغ قبلی است:
2
توجه داشته باشید که نمایش پراکنده نسبت به بردار هشت عنصری یک داغ حافظه بسیار کمتری مصرف می کند. نکته مهم این است که مدل باید بر روی بردار یک داغ تمرین کند ، نه نمایش پراکنده.
پرت در داده های طبقه بندی شده
مانند دادههای عددی، دادههای طبقهبندی نیز حاوی مقادیر پرت هستند. فرض کنید car_color نه تنها رنگهای محبوب را شامل میشود، بلکه برخی از رنگهای پرت که به ندرت مورد استفاده قرار میگیرند، مانند "Mauve" یا "Avocado" . به جای اینکه به هر یک از این رنگهای پرت یک دسته جداگانه بدهید، میتوانید آنها را در یک دسته «کلاسی» به نام خارج از واژگان (OOV) قرار دهید. به عبارت دیگر، تمام رنگ های پرت در یک سطل پرت قرار می گیرند. سیستم یک وزن واحد را برای آن سطل پرت یاد می گیرد.
رمزگذاری ویژگی های دسته بندی با ابعاد بالا
برخی از ویژگی های دسته بندی دارای ابعاد بالایی هستند، مانند مواردی که در جدول زیر آمده است:
نام ویژگی
# دسته بندی
دسته بندی نمونه
کلمات_به_انگلیسی
~ 500000
"شاد"، "راه رفتن"
کدهای_پستی_ایالات متحده
~42000
"02114", "90301"
نامهای_خانوادگی_در_آلمان
850000 ~
"اشمیت"، "اشنایدر"
وقتی تعداد دستهها زیاد است، رمزگذاری یکطرفه معمولاً انتخاب بدی است. تعبیهها ، که در یک ماژول جاسازی جداگانه توضیح داده شدهاند، معمولاً انتخاب بسیار بهتری هستند. تعبیهها بهطور قابلتوجهی تعداد ابعاد را کاهش میدهند، که به دو روش مهم برای مدلها مفید است:
این مدل معمولاً سریعتر تمرین می کند.
مدل ساخته شده معمولاً پیش بینی ها را سریعتر استنباط می کند. یعنی مدل تاخیر کمتری دارد.
هش کردن (همچنین به آن ترفند هش کردن نیز می گویند) روشی کمتر رایج برای کاهش تعداد ابعاد است.
برای آشنایی با هش اینجا را کلیک کنید
به طور خلاصه، هش کردن یک دسته (به عنوان مثال، یک رنگ) را به یک عدد صحیح کوچک - تعداد "سطل" که آن دسته را نگه می دارد، نگاشت می کند.
در جزئیات، شما یک الگوریتم هش را به صورت زیر پیاده سازی می کنید:
تعداد bin ها را در بردار دسته ها بر روی N قرار دهید، جایی که N کمتر از تعداد کل دسته های باقی مانده است. به عنوان یک مثال دلخواه، بگویید N = 100.
یک تابع هش را انتخاب کنید. (اغلب، شما محدوده مقادیر هش را نیز انتخاب می کنید.)
هر دسته (مثلاً یک رنگ خاص) را از طریق آن تابع هش عبور دهید، مثلاً 89237 یک مقدار هش ایجاد کنید.
به هر bin یک عدد شاخص از مدول مقدار هش خروجی N اختصاص دهید. در این مورد، جایی که N 100 و مقدار هش 89237 است، نتیجه مدول 37 است زیرا 89237 % 100 برابر با 37 است.
با این اعداد فهرست جدید، یک رمزگذاری یکطرفه برای هر سطل ایجاد کنید.
تاریخ آخرین بهروزرسانی 2025-08-05 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-05 بهوقت ساعت هماهنگ جهانی."],[[["\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)"]]