convert categorical to numeric (2024)

Peter Perkins on 23 Mar 2018

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#answer_311566

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#answer_311566

Open in MATLAB Online

Calling categorical is a data conversion, so

 c = categorical([12 12 13])

completely throws away the numeric values. In general, there is no way to get them back unless you have saved them, any more than you can get back the original values from int8([1.1 2.2 3.3]). Calling categorical is a data conversion.

That being said, you can certainly save the unique numeric values, and then index into those using the categorical array:

 n = uniqueNumericValues(c)

You can also call double on a categorical, but what you will get back are the category numbers, not the original numeric values.

But here's the question: if you need to convert back to the original numbers, and you are not using meaningful category names when converting from those numbers, why use categorical to begin with? There may be things you haven't mentioned.

4 Comments

Show 2 older commentsHide 2 older comments

Ian Blake on 4 Jun 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_711408

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_711408

I have the same problem, and the help file.... does not help at all

My data is categorical because the importdata chose that for it, I can force but then if I import new data and don't force it to numerical, my processing will stop working. I'm running a script so I can put a conversion there -> automate not rely on human memory!

In particular I have 160,000 lines of data in a table, one of 46 fields is an odo reading. This has converted to categorical, with 16983 categories - so might be more efficient, fair enough. But now I want to plot data against odo, so I need numerical. example subset:

>> catdata

ans = 1×8 categorical array

37241 37364 37099 4264 6339 38209 38070 16777215

So the original numbers are NOT lost, but are coded in the categories:

>> catcats=categories(catdata);

>> length(catcats)

ans = 16983

As noted above, double () gives the index not the value

>> double(catdata)

ans =

10880 10902 10858 11593 13789 11022 11004 4659

>> catcats(4659)

ans = 1×1 cell array

{'16777215'}

But cell2mat gives you a string not a number:

>> cell2mat(catcats(4659))

ans = '16777215'

So you then need to convert again using str2num (why no cell2num? There is a num2cell):

>> str2num(cell2mat(catcats(4659)))

ans = 16777215

So this works for one item, but when I use the 8 element data with the resulting strings being different length, it fails

>> catcats(double(catdata))

ans = 8×1 cell array

{'37241' }

{'37364' }

{'37099' }

{'4264' }

{'6339' }

{'38209' }

{'38070' }

{'16777215'}

>> cell2mat(catcats(double(catdata)))

Error using cat

Dimensions of matrices being concatenated are not consistent.

Error in cell2mat (line 83)

m{n} = cat(1,c{:,n});

This seems like way more difficult than it should be.

Peter Perkins on 5 Jun 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_711778

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_711778

The fundamental problem is that your numeric data are being read in as categorical. I don't have your file, so I can't tell why that is, but I recommend you use detectimportoptions, and set the type, and use that in calls to readtable to read in all of your other data.

Ian Blake on 10 Jun 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_713274

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_713274

Edited: Ian Blake on 10 Jun 2019

Open in MATLAB Online

Thanks.

I've come to the conclusion that would have been easiest, although I've developed an effective though crude workaround.

vfdbdata.DD01km is my categorical data array (from a table of data)

odocats = categories (vfdbdata.DD01km);

odoval = zeros (1, length (odocats) ); % preallocate space

for kk=1:length(odocats),

odoval(kk)=str2num(cell2mat(odocats(kk)));

end

So this is run before the main processing, the numeric data can then be extracted as required by

odotemp = double ( vfdbdata.DD01km(vidx) ) ;

odotemp = odoval (max (1, odotemp) ) ;

The max ensures that 'undefined' values are processed without throwing an error (they give a NaN after translation to double, which causes a subscript error), I also have some code to process specific values that can occur (hence the use of a temporary variable).

Matthew Anderson on 13 Apr 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_826947

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/264383-convert-categorical-to-numeric#comment_826947

Open in MATLAB Online

a = categorical(["2" "3" "3"])

double(a) % returns [1 2 2] - maybe desired for some reason

double(string(a)) % returns [2 3 3] - maybe desired for some reason

categorical(double(string(a)) % returns the same thing as a

Sign in to comment.

convert categorical to numeric (2024)

FAQs

How do you convert categorical data to numeric? ›

One common method is to assign labels based on the alphabetical order of categories, though the labels could also be assigned randomly or based on the order of appearance in the data. Once these assignments are determined, the categorical values in the dataset are replaced with their corresponding numerical labels.

How do you convert categorical features to numerical features? ›

The method of transforming categorical features to numerical generally includes the following stages:
  1. Permutating the set of input objects in a random order.
  2. Converting the label value from a floating point to an integer. ...
  3. Transforming categorical features to numerical features.

Which method is used to convert categorical to numeric attributes? ›

Target encoding is a technique used in machine learning and data preprocessing to transform categorical variables into numerical values.

How to convert categorical data into numeric in Excel? ›

How to Convert Categorical Data to Numeric in Excel
  1. Step 1: Enter the Data. First, enter the data values into Excel: ...
  2. Step 2: Use the IFS Function to Convert Categorical Values to Numeric Values. ...
  3. Step 3: Drag the Formula Down to All Cells.
May 12, 2022

How do you convert character to numeric data type? ›

A: To convert character data to numeric in R, you can use the as. numeric() function. Pass the character vector you want to convert as the argument to this function. For example: numeric_vector <- as.

What is the process of transforming categorical variables into numerical values called? ›

Categorical encoding is the process of transforming a categorical column into one (or more) numeric column(s). This is necessary because computers are more at ease working with numbers than with strings.

How do you convert categorical data to quantitative data? ›

Multiplying by 1 converts the Series of booleans to a Series of integers. Now we can manipulate this new variable as we would any other quantitative variable.

Does categorical data have to be numeric? ›

Although categorical data is qualitative, it can also be calculated in numerical values. However, these possible values don't have quantitative qualities—meaning you can't calculate anything from them. Categorical data may also be classified as binary and nonbinary depending on its nature.

What is the difference between categorical and numeric attributes? ›

Categorical data can be stored and identified by names or labels. Numerical data are numbers, not words or descriptions. Because it qualifies data before categorizing it, it is sometimes referred to as qualitative data. Quantitative data represents numerical values for arithmetic processes.

Which encoding is best for categorical data? ›

One-hot encoding, also popularly known as dummy encoding, is a widely used technique for converting categorical data into a numerical format. It's particularly suitable for nominal categorical features, where the categories have no inherent order or ranking.

How do you change categorical values to numerical values? ›

Converting Categorical Data to Numerical Data
  1. Method 1: Using the cat.codes Attribute. The easiest way to convert categorical data to numerical data in Pandas is to use the cat. ...
  2. Method 2: Using the replace() Method. ...
  3. Method 3: Using the LabelEncoder Class.
Jan 5, 2024

How do I change a character variable to numeric in Excel? ›

If your cells display an error indicator (green triangle in the top left corner), converting text strings to numbers is a two-click thing:
  1. Select all the cells containing numbers formatted as text.
  2. Click the warning sign and select Convert to Number.
Mar 16, 2023

How to convert categorical data to numerical data in Python code? ›

Steps To Convert Categorical Variable To Numeric Python
  1. Step 1 - Import the library. import pandas as pd. ...
  2. Step 2 - Setting up the Data. We have created a dictionary and passed it through the pd. ...
  3. Step 3 - Making Dummy Variables and Printing the final Dataset.
Sep 6, 2023

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5768

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.