{"id":4635,"date":"2026-10-18T12:14:41","date_gmt":"2026-10-18T12:14:41","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=4635"},"modified":"2026-10-19T13:50:28","modified_gmt":"2026-10-19T13:50:28","slug":"categorical-data-encoding","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/","title":{"rendered":"Categorical Data Encoding"},"content":{"rendered":"<h1><strong><b>Categorical Data Encoding in Machine Learning<\/b><\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4636\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg\" alt=\"Approaches for Categorical Data Encoding\" width=\"850\" height=\"567\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg 939w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1-300x200.jpg 300w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1-768x512.jpg 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>A spreadsheet of patients might say Gender is female, region is northwest and Diabetes is yes. To a person, that is perfectly clear. To most machine learning algorithms, it is gibberish. Algorithms do arithmetic, and arithmetic needs numbers. Before the model can learn anything, every piece of text has to be converted into a number.<\/p>\n<p>That conversion is called categorical data encoding, and the way you do it matters more than most beginners expect. A careless choice can give the model a false idea that one category is bigger than another, or can blow up your dataset with thousands of extra columns. The right choice depends on the type of category, how many of them there are, and which model you plan to use.<\/p>\n<p>In this tutorial, we explain the types of categorical data and the four main encoding methods: label, one hot, ordinal and binary. We run all of them on a small patient table with real Python code that was tested, show the exact results as text, and finish with a simple guide for choosing between them, the mistakes to avoid and a short FAQ.<\/p>\n<h2><strong><b>What is Categorical Data?<\/b><\/strong><\/h2>\n<p>Categorical data is data that falls into groups instead of measuring an amount. There are two kinds:<\/p>\n<ul>\n<li><b><\/b><strong><b>Nominal data<\/b><\/strong>has no order. Examples are gender, city, colour and region. Northwest is not larger than southeast.<\/li>\n<li><b><\/b><strong><b>Ordinal data<\/b><\/strong>has a natural order. Examples are small, medium and large, or low, medium and high. The gaps between them are not necessarily equal, but the order is real.<\/li>\n<\/ul>\n<p>This difference decides which encoding is correct, so keep it in mind.<\/p>\n<h2><strong><b>Why do We Need to Encode Categorical Data?<\/b><\/strong><\/h2>\n<ul>\n<li><b><\/b><strong><b>Most algorithms need numbers.<\/b><\/strong>Linear and logistic regression, SVM, KNN and neural networks cannot read text at all.<\/li>\n<li><b><\/b><strong><b>Some tools accept text but encode it for you.<\/b><\/strong>Certain tree libraries do this internally. It is still wise to know what happens.<\/li>\n<li><b><\/b><strong><b>The encoding changes the result.<\/b><\/strong>The same data encoded two ways can give models with different accuracy.<\/li>\n<li><b><\/b><strong><b>It keeps meaning intact.<\/b><\/strong>Good encoding keeps the real relationships and does not invent false ones.<\/li>\n<\/ul>\n<h2><strong><b>Types of Categorical Data Encoding<\/b><\/strong><\/h2>\n<p>No single method fits every case. Here is the quick comparison before we go through each one.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 800px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Encoding methods compared<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 170px;\" scope=\"col\">Method<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">How it works<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Best for<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Main risk<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Label encoding<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">One whole number per category<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Target labels, or tree based models<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Implies a false order<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">One hot encoding<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">One 0 or 1 column per category<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Nominal data with few categories<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Too many columns<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Ordinal encoding<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Numbers that follow a real order<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Ordered categories<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Wrong if no order exists<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Binary encoding<\/th>\n<td style=\"padding: 12px 16px;\">Category number written in binary, split into columns<\/td>\n<td style=\"padding: 12px 16px;\">Nominal data with many categories<\/td>\n<td style=\"padding: 12px 16px;\">Less readable<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><strong><b>Our Example Data<\/b><\/strong><\/h2>\n<p>We will use a small table of 10 patients with five columns: age, Gender, BMI, Diabetes and region. Gender has 2 values, Diabetes has 2 values and region has 4. Here is the setup, which also loads the libraries that the original page forgot to import.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">import pandas as pd\r\nimport category_encoders as ce\r\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder, OrdinalEncoder\r\n\r\ndf = pd.DataFrame({\r\n    \"age\":       [19, 33, 28, 45, 52, 23, 61, 37, 29, 48],\r\n    \"Gender\":    [\"female\", \"male\", \"male\", \"male\", \"female\", \"female\", \"male\", \"female\", \"male\", \"female\"],\r\n    \"BMI\":       [27.9, 33.8, 33.0, 22.7, 28.9, 25.7, 31.9, 26.2, 30.1, 29.4],\r\n    \"Diabetes\":  [\"yes\", \"no\", \"no\", \"no\", \"no\", \"no\", \"yes\", \"no\", \"no\", \"yes\"],\r\n    \"region\":    [\"southwest\", \"southeast\", \"southeast\", \"northwest\", \"northwest\",\r\n                  \"southeast\", \"northeast\", \"northwest\", \"southeast\", \"northwest\"],\r\n})\r\nprint(df.dtypes)\r\n\r\n# 1. Label encoding with pandas category codes\r\nlabel_df = df.copy()\r\nfor col in [\"Gender\", \"Diabetes\", \"region\"]:\r\n    label_df[col] = label_df[col].astype(\"category\").cat.codes\r\nprint(label_df)\r\nprint(sorted(df[\"region\"].unique()))\r\n\r\n# 1b. Label encoding with scikit learn\r\nle = LabelEncoder().fit(df[\"region\"])\r\nprint(dict(zip(le.classes_, le.transform(le.classes_))))\r\n\r\n# 2. One hot encoding with pandas\r\nonehot_df = pd.get_dummies(df, columns=[\"Gender\", \"Diabetes\", \"region\"], dtype=int)\r\nprint(list(onehot_df.columns))\r\nprint(onehot_df.shape)\r\n\r\n# 2b. One hot encoding with scikit learn (drop one column per feature)\r\nohe = OneHotEncoder(drop=\"first\", sparse_output=False).fit(df[[\"Gender\", \"Diabetes\", \"region\"]])\r\nprint(list(ohe.get_feature_names_out()))\r\n\r\n# 3. Binary encoding on the original data\r\nbin_df = ce.BinaryEncoder(cols=[\"region\"]).fit_transform(df[[\"region\"]])\r\nprint(pd.concat([df[\"region\"], bin_df], axis=1).drop_duplicates())\r\n\r\n# 4. Ordinal encoding when the order is real\r\nsizes = pd.DataFrame({\"size\": [\"small\", \"large\", \"medium\", \"small\", \"large\"]})\r\noe = OrdinalEncoder(categories=[[\"small\", \"medium\", \"large\"]])\r\nprint(oe.fit_transform(sizes).ravel())\r\n\r\n# 5. How many columns each method makes for 100 categories\r\nprint(\"one hot:\", 100, \"binary:\", len(bin(99)) - 2, \"label:\", 1)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">age           int64\r\nGender          str\r\nBMI         float64\r\nDiabetes        str\r\nregion          str\r\ndtype: object\r\n   age  Gender   BMI  Diabetes  region\r\n0   19       0  27.9         1       3\r\n1   33       1  33.8         0       2\r\n2   28       1  33.0         0       2\r\n3   45       1  22.7         0       1\r\n4   52       0  28.9         0       1\r\n5   23       0  25.7         0       2\r\n6   61       1  31.9         1       0\r\n7   37       0  26.2         0       1\r\n8   29       1  30.1         0       2\r\n9   48       0  29.4         1       1\r\n['northeast', 'northwest', 'southeast', 'southwest']\r\n{'northeast': np.int64(0), 'northwest': np.int64(1), 'southeast': np.int64(2), 'southwest': np.int64(3)}\r\n['age', 'BMI', 'Gender_female', 'Gender_male', 'Diabetes_no', 'Diabetes_yes', 'region_northeast', 'region_northwest', 'region_southeast', 'region_southwest']\r\n(10, 10)\r\n['Gender_male', 'Diabetes_yes', 'region_northwest', 'region_southeast', 'region_southwest']\r\n      region  region_0  region_1  region_2\r\n0  southwest         0         0         1\r\n1  southeast         0         1         0\r\n3  northwest         0         1         1\r\n6  northeast         1         0         0\r\n[0. 2. 1. 0. 2.]\r\none hot: 100 binary: 7 label: 1<\/pre>\n<\/div>\n<p>The data is loaded and ready. Check the data types of each column with df.dtypes. Age and BMI are numbers. Gender, Diabetes and region are text, so they need encoding.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4637\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod2.jpg.png\" alt=\"Categorical Data Encoding - Label encoding in Machine Learning\" width=\"850\" height=\"917\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod2.jpg.png 353w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod2.jpg-278x300.png 278w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3>Then check the data types of columns in df<\/h3>\n<p>df.dtypes<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4638\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod3.png\" alt=\"Categorical Data Encoding - Label encoding object in Machine Learning\" width=\"850\" height=\"425\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod3.png 310w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod3-300x150.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2><strong><b>Label Encoding in Machine Learning<\/b><\/strong><\/h2>\n<p>Label encoding gives every category its own whole number. By default the numbers are assigned in alphabetical order. Gender becomes 0 for female and 1 for male. Diabetes becomes 0 for no and 1 for yes.<\/p>\n<h3><strong><b>Label Encoding With pandas<\/b><\/strong><\/h3>\n<p>First, change the column type to category, and check that it worked. Then take the codes with cat.codes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4639\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod4.jpg.png\" alt=\"Categorical Data Encoding - Label encoding output in Machine Learning\" width=\"850\" height=\"159\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod4.jpg.png 498w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod4.jpg-300x56.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3>Here is the result for the first rows:<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">   age  Gender   BMI  Diabetes  region\r\n0   19       0  27.9         1       3\r\n1   33       1  33.8         0       2\r\n2   28       1  33.0         0       2\r\n3   45       1  22.7         0       1\r\n4   52       0  28.9         0       1<\/pre>\n<\/div>\n<p>Look at the region column. It has four values, and alphabetical order gives northeast 0, northwest 1, southeast 2 and southwest 3. The original page says southwest is 4, which is wrong, because counting starts at zero.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4640\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod5.jpg.png\" alt=\"Categorical Data Encoding - Label encoding example in Machine Learning\" width=\"850\" height=\"871\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod5.jpg.png 373w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod5.jpg-293x300.png 293w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>As we can see in the result the column has been converted into 0 and 1 (female &#8211; 0, male &#8211; 1). In this manner, we can apply the <strong><em>cat.codes<\/em><\/strong> to all columns.<\/p>\n<p>In the above case, the columns were limited and we could count the categories in each column easily. But the solution gets critical if the number of columns and categories gets increased. So in that case scikit-learn&#8217;s Label Encoder comes in picture.<\/p>\n<p>For this, we have to import LabelEncoder from sklearn.preprocessing<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4641\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod6.jpg.png\" alt=\"Categorical Data Encoding - Label encoder from sklearn in Machine Learning\" width=\"850\" height=\"105\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod6.jpg.png 395w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod6.jpg-300x37.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{'northeast': 0, 'northwest': 1, 'southeast': 2, 'southwest': 3}<\/pre>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4642\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod7.png\" alt=\"Categorical Data Encoding - Label encoder in Machine Learning\" width=\"850\" height=\"755\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod7.png 547w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod7-300x267.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong><b>he Catch With Label Encoding<\/b><\/strong><\/h3>\n<p>Label encoding has a serious side effect. The numbers imply an order and a distance. A model like linear regression or KNN will treat southwest, at 3, as three times as large as northwest, at 1, and as being closer to southeast than to northeast. For a nominal feature like region, none of that is true. The codes are only labels. They do not show weight or importance, which the original page wrongly suggests at one point.<\/p>\n<p>Label encoding is safe in two cases. One is when you encode the target column, the thing you want to predict. The other is when you use tree based models, such as decision trees and random forests, which split on thresholds and are much less bothered by the false order.<\/p>\n<h2><strong><b>One Hot Encoding in Machine Learning<\/b><\/strong><\/h2>\n<p>One hot encoding removes the false order. It creates a new column for each category, with 1 where the category applies and 0 where it does not. Each row has exactly one 1 in each group of columns, which is where the name comes from.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">['age', 'BMI', 'Gender_female', 'Gender_male', 'Diabetes_no', 'Diabetes_yes', 'region_northeast', 'region_northwest', 'region_southeast', 'region_southwest']<\/pre>\n<\/div>\n<p>The table started with 5 columns and now has 10. Gender made 2 columns, Diabetes made 2 and region made 4. The original page has a typo, Dibetes, that would make this step fail. The correct spelling is Diabetes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4649\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/Table.jpg\" alt=\"Categorical Data Encoding - One hot encoding in Machine Learning\" width=\"850\" height=\"402\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/Table.jpg 688w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/Table-300x142.jpg 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong><b>The Dummy Variable Trap<\/b><\/strong><\/h3>\n<p>Look at Gender_female and Gender_male. If you know one, you know the other, since a patient who is not male must be female. Keeping both columns adds duplicate information, which can cause trouble in linear and logistic regression. This is called the dummy variable trap. The fix is to drop one column per feature, and scikit learn does it with drop set to first. With that setting, the columns become:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">['Gender_male', 'Diabetes_yes', 'region_northwest', 'region_southeast', 'region_southwest']<\/pre>\n<\/div>\n<p>Now Gender and Diabetes need one column each, and region needs three, not four. Tree based models do not need this step.<\/p>\n<h3><strong><b>The Downside: Too Many Columns<\/b><\/strong><\/h3>\n<p>One hot encoding grows with the number of categories. A column with 100 cities creates 100 new columns, and one with 10,000 product codes creates 10,000. That makes the data large, slow and sparse. For columns like that, consider binary encoding or another method.<\/p>\n<h2><strong><b>Ordinal Encoding<\/b><\/strong><\/h2>\n<p>When categories do have a real order, you should keep it. Ordinal encoding lets you state the order yourself, so small, medium and large become 0, 1 and 2, in that order. If you used plain label encoding, alphabetical order would give large 0, medium 1 and small 2, which is backwards.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">[0. 2. 1. 0. 2.]<\/pre>\n<\/div>\n<p>That is the result for the list small, large, medium, small, large, when the order small, medium, large is given to OrdinalEncoder. Use ordinal encoding only when the order is real. Never force an order on region or colour.<\/p>\n<h2><strong><b>Binary Encoding in Machine Learning<\/b><\/strong><\/h2>\n<p>Binary encoding is a middle path for columns with many categories. It first gives each category a number, then writes that number in binary, and then puts each binary digit in its own column. It needs far fewer columns than one hot encoding.<\/p>\n<p>It lives in the category_encoders library, which you install once from the command line with pip install category_encoders. That is a shell command, so it does not go inside a Python block.<\/p>\n<p>Here is the region column after binary encoding. Four regions need only 3 columns here:<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 640px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Binary encoding of the region column<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 180px;\" scope=\"col\">region<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">region_0<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">region_1<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">region_2<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">southwest<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">southeast<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">northwest<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">0<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">northeast<\/th>\n<td style=\"padding: 12px 16px;\">1<\/td>\n<td style=\"padding: 12px 16px;\">0<\/td>\n<td style=\"padding: 12px 16px;\">0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The numbers follow the order in which each region first appears in the data. The ranking is arbitrary, so binary encoding still creates some false closeness between categories that share digits. It is a trade of a little accuracy for a lot of size.<\/p>\n<p>The saving grows fast. For 100 categories, one hot encoding needs 100 columns, and binary encoding needs only 7. For 1,000 categories it needs 10.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 680px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Columns created by each encoding<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 220px;\" scope=\"col\">Number of categories<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Label<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">One hot<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Binary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">4<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1 column<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">4 columns<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">3 columns<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">100<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">1 column<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">100 columns<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">7 columns<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">1,000<\/th>\n<td style=\"padding: 12px 16px;\">1 column<\/td>\n<td style=\"padding: 12px 16px;\">1,000 columns<\/td>\n<td style=\"padding: 12px 16px;\">10 columns<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>An important correction to the original code: the binary step must run on the original data, not on the output of get_dummies, because by then the original columns are gone.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4643\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod8.jpg.png\" alt=\"Categorical Data Encoding - Binary Encoding in Machine Learning\" width=\"850\" height=\"421\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod8.jpg.png 636w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod8.jpg-300x149.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2><strong><b>Which Encoding Should You Use?<\/b><\/strong><\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 760px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Which encoding to use<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 300px;\" scope=\"col\">Your situation<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 220px;\" scope=\"col\">Use<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Why<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Target column to predict<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Label encoding<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The model needs class numbers, order does not matter<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Nominal feature, few categories, linear or distance based model<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">One hot encoding<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No false order<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Ordered feature such as size or grade<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Ordinal encoding<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Keeps the real order<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Nominal feature with many categories<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Binary encoding<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Far fewer columns<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a;\" scope=\"row\">Tree based model, any feature<\/th>\n<td style=\"padding: 12px 16px;\">Label or ordinal encoding<\/td>\n<td style=\"padding: 12px 16px;\">Trees are not bothered by the number order<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><strong><b>Common Mistakes to Avoid<\/b><\/strong><\/h2>\n<ul>\n<li><b><\/b><strong><b>Label encoding a nominal feature for a linear or distance model.<\/b><\/strong>It creates a false order.<\/li>\n<li><b><\/b><strong><b>Encoding before splitting the data, then fitting on everything.<\/b><\/strong>Fit encoders on training data only, then apply them to test data. A category that appears only in test data needs a plan.<\/li>\n<li><b><\/b><strong><b>Running one method on the output of another. <\/b><\/strong>Each method should start from the original columns.<\/li>\n<li><b><\/b><strong><b>Falling into the dummy variable trap. <\/b><\/strong>Drop one column per feature for linear models.<\/li>\n<li><b><\/b><strong><b>One hot encoding a column with thousands of values. <\/b><\/strong>Use binary encoding or group the rare values.<\/li>\n<li><b><\/b><strong><b>Forcing order on unordered data. <\/b><\/strong>Never use ordinal encoding for region or colour.<\/li>\n<li><b><\/b><strong><b>Typing column names by hand and misspelling them. <\/b><\/strong>Copy them from the data.<\/li>\n<\/ul>\n<p>Encoding turns a table that people can read into a table that models can learn from. Start by asking whether your categories have an order, then count how many there are. Use one hot for a few unordered categories, ordinal for ordered ones, and binary when the count grows large. Run the program above on your own data and see how each choice changes your columns.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Categorical Data Encoding in Machine Learning A spreadsheet of patients might say Gender is female, region is northwest and Diabetes is yes. To a person, that is perfectly clear. To most machine learning algorithms, it is gibberish. Algorithms do arithmetic, and arithmetic needs numbers. Before the model can learn anything, every piece of text has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,1696],"tags":[411,1748,1749,412],"class_list":["post-4635","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-machine-learning-modules","tag-categorical-data-encoding","tag-categorical-data-encoding-techniques-by-krishnakanth-naik-jarapala","tag-feature-engineering-for-machine-learning-categorical-encoding","tag-how-to-convert-categorical-data-to-numerical-data"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Categorical Data Encoding in Machine Learning With Python<\/title>\n<meta name=\"description\" content=\"Data Encoding in Machine Learning: label, one hot, ordinal and binary encoding for categorical data, when to use each &amp; run a tested pandas and scikit learn example.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Categorical Data Encoding in Machine Learning With Python\" \/>\n<meta property=\"og:description\" content=\"Data Encoding in Machine Learning: label, one hot, ordinal and binary encoding for categorical data, when to use each &amp; run a tested pandas and scikit learn example.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-18T12:14:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-19T13:50:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"939\" \/>\n\t<meta property=\"og:image:height\" content=\"626\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/\",\"url\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/\",\"name\":\"Categorical Data Encoding in Machine Learning With Python\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg\",\"datePublished\":\"2026-10-18T12:14:41+00:00\",\"dateModified\":\"2026-10-19T13:50:28+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Data Encoding in Machine Learning: label, one hot, ordinal and binary encoding for categorical data, when to use each & run a tested pandas and scikit learn example.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg\",\"width\":939,\"height\":626},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categorical Data Encoding\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Categorical Data Encoding in Machine Learning With Python","description":"Data Encoding in Machine Learning: label, one hot, ordinal and binary encoding for categorical data, when to use each & run a tested pandas and scikit learn example.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/","og_locale":"en_US","og_type":"article","og_title":"Categorical Data Encoding in Machine Learning With Python","og_description":"Data Encoding in Machine Learning: label, one hot, ordinal and binary encoding for categorical data, when to use each & run a tested pandas and scikit learn example.","og_url":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-18T12:14:41+00:00","article_modified_time":"2026-10-19T13:50:28+00:00","og_image":[{"width":939,"height":626,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg","type":"image\/jpeg"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/","url":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/","name":"Categorical Data Encoding in Machine Learning With Python","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg","datePublished":"2026-10-18T12:14:41+00:00","dateModified":"2026-10-19T13:50:28+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Data Encoding in Machine Learning: label, one hot, ordinal and binary encoding for categorical data, when to use each & run a tested pandas and scikit learn example.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/01\/dataencod1.jpg","width":939,"height":626},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/categorical-data-encoding\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Categorical Data Encoding"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=4635"}],"version-history":[{"count":18,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4635\/revisions"}],"predecessor-version":[{"id":12020,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4635\/revisions\/12020"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=4635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=4635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=4635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}