convert categorical column to numeric in a matrix? (2024)

dpb on 17 Aug 2018

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#answer_333172

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#answer_333172

Open in MATLAB Online

Totally different Q? in follow up than original --

You don't show how you "imported" the file, but

>> [~,~,r]=xlsread('tra2.xls');

>> cell2mat(r(2:10,2))

ans =

3

1

14

13

1

9

5

17

1

>>

From the raw cell array have to skip the header row to convert, hence the (2) as starting index.

Alternatively, and much easier to deal with in the end would be--

>> t=readtable('tra2.xls');

Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in the VariableDescriptions property.

>> t(1:10,:)

ans =

10×26 table

Normalized_losses Make Fuel_type Aspiration Num_of_doors Body_style Drive_wheels Engine_location Wheel_base Length Width Height Curb_weight Engine_type Num_of_cylinders Engine_size Fuel_system Bore Stroke Compression_ratio Horsepower Peak_rpm City_mpg Highway_mpg Price Symboling

_________________ ____ _________ __________ ____________ ___________ ____________ _______________ __________ ______ _____ ______ ___________ ___________ ________________ ___________ ___________ ____ ______ _________________ __________ ________ ________ ___________ _____ _________

110 3 'gas' 'std' 'four' 'sedan' 'fwd' 'front' 96.5 163.4 64 54.5 2010 'ohc' 'four' 92 '1bbl' 2.91 3.41 9.2 76 6000 30 34 7295 '0'

134 1 'gas' 'std' 'two' 'hatchback' 'rwd' 'front' 98.4 176.2 65.6 52 2551 'ohc' 'four' 146 'mpfi' 3.62 3.5 9.3 116 4800 24 30 9989 '2'

125 14 'gas' 'std' 'four' 'sedan' 'fwd' 'front' 96.3 172.4 65.4 51.6 2405 'ohc' 'four' 122 '2bbl' 3.35 3.46 8.5 88 5000 25 32 8189 '1'

104 13 'gas' 'turbo' 'four' 'sedan' 'fwd' 'front' 99.1 186.6 66.5 56.1 2847 'dohc' 'four' 121 'mpfi' 3.54 3.07 9 160 5500 19 26 18620 '2'

91 1 'gas' 'std' 'four' 'sedan' 'fwd' 'front' 95.7 166.3 64.4 52.8 2140 'ohc' 'four' 98 '2bbl' 3.19 3.03 9 70 4800 28 34 9258 '0'

115 9 'gas' 'std' 'four' 'hatchback' 'fwd' 'front' 98.8 177.8 66.5 55.5 2425 'ohc' 'four' 122 '2bbl' 3.39 3.39 8.6 84 4800 26 32 11245 '0'

122 5 'gas' 'std' 'four' 'sedan' 'fwd' 'front' 94.5 165.3 63.8 54.5 1938 'ohc' 'four' 97 '2bbl' 3.15 3.29 9.4 69 5200 31 37 6849 '1'

121 17 'gas' 'std' 'two' 'hatchback' 'fwd' 'front' 88.4 141.1 60.3 53.2 1488 'l' 'three' 61 '2bbl' 2.91 3.03 9.5 48 5100 47 53 5151 '2'

134 1 'gas' 'std' 'two' 'hardtop' 'rwd' 'front' 98.4 176.2 65.6 52 2540 'ohc' 'four' 146 'mpfi' 3.62 3.5 9.3 116 4800 24 30 8449 '2'

93 15 'diesel' 'turbo' 'four' 'wagon' 'rwd' 'front' 110 190.9 70.3 58.7 3750 'ohc' 'five' 183 'idi' 3.58 3.64 21.5 123 4350 22 25 28248 '-1'

>>

>> t.Make(1:10)

ans =

3

1

14

13

1

9

5

17

1

15

>>

and Make will be double by default. I'd suggest converting it to categorical would probably be beneficial, though instead of numeric.

4 Comments

Show 2 older commentsHide 2 older comments

z donyavi on 17 Aug 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_600948

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_600948

sorry I'm so confused.I want convert categorical column of 'tra2' to numeric.

dpb on 17 Aug 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_600994

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_600994

Open in MATLAB Online

You've got us totally confused by not showing what you've done...we have to try to guess what the starting point is.

Show the code you used to import the data into Matlab and what does

whos tra2

show so we can tell what it is we're trying to work with. Or, attach tra2.mat to provide us the same data precisely as you have.

But, whatever you did, why not convert to using table from the spreadsheet and you'll have the numeric data in numeric form automagically.

z donyavi on 17 Aug 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_601025

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_601025

I use this command for import data

[~,~,raw] = xlsread('tra2.xls')

dpb on 17 Aug 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_601035

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/415270-convert-categorical-column-to-numeric-in-a-matrix#comment_601035

Edited: dpb on 18 Aug 2018

Open in MATLAB Online

Well, that was the first response I showed you...I just did a few elements instead of the whole column. Replace (2:10,2) with (2:end,2). Again note you must manually avoid trying to do something you can't with the header row this way and will have to continue to do that every time you try to access anything.

If you were to forget about xlsread and the cell array in favor of readtable and a table you'll get along a lot faster going forward.

And, that kind of a variable really ought to be categorical, not numeric, as should a significant number of the others.

A subset of the first 10 lines, first six variable...of those four should be categorical assuming the losses are actually a measured or calculated response. and one that is categorical would make a lot of sense as being numerical altho if it is only for classification as is quite probable it could well be left as categorical, too...

>> t(1:10,1:6)

ans =

10×6 table

Normalized_losses Make Fuel_type Aspiration Num_of_doors Body_style

_________________ ____ _________ __________ ____________ ___________

110 3 'gas' 'std' 'four' 'sedan'

134 1 'gas' 'std' 'two' 'hatchback'

125 14 'gas' 'std' 'four' 'sedan'

104 13 'gas' 'turbo' 'four' 'sedan'

91 1 'gas' 'std' 'four' 'sedan'

115 9 'gas' 'std' 'four' 'hatchback'

122 5 'gas' 'std' 'four' 'sedan'

121 17 'gas' 'std' 'two' 'hatchback'

134 1 'gas' 'std' 'two' 'hardtop'

93 15 'diesel' 'turbo' 'four' 'wagon'

>>

Sign in to comment.

convert categorical column to numeric in a matrix? (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.

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 multiple categorical columns to numerical data? ›

One of the methods to create dummy variables involves following steps: 1) creating dummy variables for each of the columns, 2) concatenate the new columns to the main data frame, 3) drop corresponding categorical columns. Above code is dropping first dummy variable columns to avoid dummy variable trap.

Which technique is used to convert categorical data into numerical data? ›

There are other methods to convert categorical data to numerical data, such as binary encoding, frequency encoding, target encoding, or embedding. Each method has its own advantages and disadvantages, and you should choose the one that best suits your data and your ML model.

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.

How to change categorical data to numerical data in DataFrame? ›

In Pandas, there are several ways to convert categorical data to numerical data, including the following:
  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.
Jun 19, 2023

How do I convert columns to numerical values? ›

Convert a column of text to numbers
  1. Select a column. Select a column with this problem. ...
  2. Select Text to Columns. The Text to Columns button is typically used for splitting a column, but it can also be used to convert a single column of text to numbers. ...
  3. Select Apply. ...
  4. Set the format.

Can categorical data be converted to quantitative? ›

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 test is used for categorical data and numerical data? ›

Z-test and t-test

This analysis is appropriate for comparing the averages of a numerical variable for two categories of a categorical variable.

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 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? ›

Using as.

numeric() function in R is your go-to for converting character data into numeric form. This transformation is crucial for subsequent data analysis tasks that require numerical input.

How to convert categorical data into numerical data in R? ›

Target Encoding: Target Encoding is a process of converting categorical variables into numerical variables by replacing each category with the mean of the target variable for that category. You can use the targetMean() function from the cattonum package to create target encoding.

How do you change a variable to numeric? ›

You can only tell the type of a variable by using the describe command. This is easy to fix. If you have a string variable and want to convert it to a numeric variable, you can use the encode command. If you have a string variable that has only numbers in it, then you can alternatively use the real() function.

Top Articles
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5909

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.