fitcecoc and fitclinear are suitable for categorical data ?

2 views (last 30 days)
Hi,
I'm a newbie in Machine Learning and i'm trying to create a model on a kaggle dataset below.
The thing is i'd like to use k-fold crossvalidation on my model but i cannot use fifcecoc and fitclinear functions because they only accept numerical values whereas i also have some categorical ones.
Is there a way to use them with categorical data or i need to use cvpartition for k-fold crossvalidation due to the fact that my dataset not completely numerical ?
Also can i use regularization with categorical variables ?
  1 Comment
Jon Cherrie
Jon Cherrie on 23 Apr 2021
You should be able to use fitcecoc with categorical predictors and k-fold cross-validation. Here is an example using the "carbig" data and k=5 folds:
load carbig.mat
t = table(Acceleration,Cylinders,Displacement,Horsepower,Mfg,Model_Year,MPG,Origin,Weight);
t.Mfg = categorical(cellstr(t.Mfg));
t.Cylinders = categorical(t.Cylinders);
t.Origin = categorical(cellstr(t.Origin));
m = fitcecoc(t,"Cylinders","KFold",5)
In this cases I get warning about unique class values (which I think means I have more folds than this data will tolerate), but I can see the categorical predictors in the output:
>> m
m =
ClassificationPartitionedECOC
CrossValidatedModel: 'ECOC'
PredictorNames: {'Acceleration' 'Displacement' 'Horsepower' 'Mfg' 'Model_Year' 'MPG' 'Origin' 'Weight'}
CategoricalPredictors: [4 7]
ResponseName: 'Cylinders'
NumObservations: 406
KFold: 5
Partition: [1×1 cvpartition]
ClassNames: [3 4 5 6 8]
ScoreTransform: 'none'
If this example, doesn't work for you, can you share some code and an error message?

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!