"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일)
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
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개)

카테고리

Help CenterFile Exchange에서 Categorical Arrays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by