"categorical()" cannot convert continuous data into categorical data with an error: "Unable to create default category names. Specify category names using the CATEGORYNAMES input argument."
10 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2022-4-26
回答: MathWorks Support Team
2022-4-26
I tried to convert numbers in double array into categorical data using "categorical()" function.
However, such an error occurs as below. What is the reason for this?
"Unable to create default category names. Specify category names using the CATEGORYNAMES input argument."
To reproduce the issue, run the code below.
a= [0.157132, 0.157132, 0.157135, 0.157135, 0.19604, 0.19604];
categorical(a)
采纳的回答
MathWorks Support Team
2022-4-26
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, i.e., 0.00005. For example, the function does not categorize 1 and 1.00001 into different categories. This behavior is expected and explained well in the categorical document as below.
-----------------------------------------------
If the input array has numeric, datetime, or duration values that are too close together, then the categorical function truncates them to duplicate values. For example, categorical([1 1.00001]) truncates the second element of the input array. To create categories from numeric data, use the discretize function.
-----------------------------------------------
In your example, 0.157132 and 0.157135 are very close to each other and the difference is 3e-6 which is less than 5e-5. Therefore "categorical()" cannot classify the two numbers into two categories. As a workaround, please consider using "discretize()" as below.
categorical(discretize(a, min(a):1e-6:max(a)))
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Categorical Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!