"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!