Convert double to categorical array

50 visualizaciones (últimos 30 días)
Parichada Trairat
Parichada Trairat el 16 de Jun. de 2022
Comentada: Peter Perkins el 17 de Jun. de 2022
Hello,
I have a problem with convert double to categorical.
the matlab showed:
Error using categorical
Unable to create default category names. Specify category names using the CATEGORYNAMES input argument.
This is my code:
YTrain = trainYweekday';
YTrain = categorical(YTrain);
YTrain = num2cell(YTrain,1);
trainYweekday' is 1x360 double.
How can I convert trainYweekday to categorical?
Thank you very much.
  1 comentario
Adam Danz
Adam Danz el 16 de Jun. de 2022
@Parichada Trairat thanks for the YTrain values but I deleted them from your question so my mouse scroll wheel doesn't catch on fire 🔥 🙂
Feel free to attach long data sets as files.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 16 de Jun. de 2022
Editada: Adam Danz el 16 de Jun. de 2022
The function categorical can convert continuous data into categorical data. However, this function cannot categorize two numbers if the difference between them is less than 5e-5, unless their differences are 0.
Example:
categorical([pi, pi])
ans = 1×2 categorical array
3.1416 3.1416
categorical([pi, pi+5e-5])
Error using categorical
Unable to create default category names. Specify category names using the CATEGORYNAMES input argument.
To get around this, you must discretize the vector
vals = [pi, pi+5e-5];
categorical(discretize(vals, min(vals):1e-6:max(vals)))
See this MathWorks Support Team article for more details.
Also see this example from the categorical doc page that shows the use of discretize to prevent this problem.
  2 comentarios
Parichada Trairat
Parichada Trairat el 17 de Jun. de 2022
Thank you very much
Peter Perkins
Peter Perkins el 17 de Jun. de 2022
In addition to what Adam said:
The categorical function tries to make categories out of the unique values of whatever you give it. "Convert double to categorical" sometimes makes sense if you mean "convert continuous numeric data into categorical with one category for each unique numeric value." But if you run into this error, you almost certainly should be binning your continuous data using discretize instead.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by