Feature type for integer class labels. There are 3 ways to define a `ClassLabel`, which correspond to the 3 arguments: * `num_classes`: Create 0 to (num_classes-1) labels. * `names`: List of label strings. * `names_file`: File containing the list of labels. Under the hood t
| 984 | |
| 985 | @dataclass |
| 986 | class ClassLabel: |
| 987 | """Feature type for integer class labels. |
| 988 | |
| 989 | There are 3 ways to define a `ClassLabel`, which correspond to the 3 arguments: |
| 990 | |
| 991 | * `num_classes`: Create 0 to (num_classes-1) labels. |
| 992 | * `names`: List of label strings. |
| 993 | * `names_file`: File containing the list of labels. |
| 994 | |
| 995 | Under the hood the labels are stored as integers. |
| 996 | You can use -1 to represent unknown/missing labels. |
| 997 | |
| 998 | Args: |
| 999 | num_classes (`int`, *optional*): |
| 1000 | Number of classes. All labels must be < `num_classes`. |
| 1001 | names (`list` of `str`, *optional*): |
| 1002 | String names for the integer classes. |
| 1003 | The order in which the names are provided is kept. |
| 1004 | names_file (`str`, *optional*): |
| 1005 | Path to a file with names for the integer classes, one per line. |
| 1006 | |
| 1007 | Example: |
| 1008 | |
| 1009 | ```py |
| 1010 | >>> from datasets import Features, ClassLabel |
| 1011 | >>> features = Features({'label': ClassLabel(num_classes=3, names=['bad', 'ok', 'good'])}) |
| 1012 | >>> features |
| 1013 | {'label': ClassLabel(names=['bad', 'ok', 'good'])} |
| 1014 | ``` |
| 1015 | """ |
| 1016 | |
| 1017 | num_classes: InitVar[Optional[int]] = None # Pseudo-field: ignored by asdict/fields when converting to/from dict |
| 1018 | names: list[str] = None |
| 1019 | names_file: InitVar[Optional[str]] = None # Pseudo-field: ignored by asdict/fields when converting to/from dict |
| 1020 | id: Optional[str] = field(default=None, repr=False) |
| 1021 | # Automatically constructed |
| 1022 | dtype: ClassVar[str] = "int64" |
| 1023 | pa_type: ClassVar[Any] = pa.int64() |
| 1024 | _str2int: ClassVar[dict[str, int]] = None |
| 1025 | _int2str: ClassVar[dict[int, int]] = None |
| 1026 | _type: str = field(default="ClassLabel", init=False, repr=False) |
| 1027 | |
| 1028 | def __post_init__(self, num_classes, names_file): |
| 1029 | self.num_classes = num_classes |
| 1030 | self.names_file = names_file |
| 1031 | if self.names_file is not None and self.names is not None: |
| 1032 | raise ValueError("Please provide either names or names_file but not both.") |
| 1033 | # Set self.names |
| 1034 | if self.names is None: |
| 1035 | if self.names_file is not None: |
| 1036 | self.names = self._load_names_from_file(self.names_file) |
| 1037 | elif self.num_classes is not None: |
| 1038 | self.names = [str(i) for i in range(self.num_classes)] |
| 1039 | else: |
| 1040 | raise ValueError("Please provide either num_classes, names or names_file.") |
| 1041 | elif not isinstance(self.names, SequenceABC): |
| 1042 | raise TypeError(f"Please provide names as a list, is {type(self.names)}") |
| 1043 | # Set self.num_classes |
no outgoing calls