Repository navigation
Expand file tree
/
Copy pathdata_feature.py
More file actions
84 lines (69 loc) 路 2.87 KB
/
Copy pathdata_feature.py
File metadata and controls
84 lines (69 loc) 路 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# License: BSD 3-Clause
from __future__ import annotations
from typing import TYPE_CHECKING, Any, ClassVar, Sequence
if TYPE_CHECKING:
from IPython.lib import pretty
class OpenMLDataFeature:
"""
Data Feature (a.k.a. Attribute) object.
Parameters
----------
index : int
The index of this feature
name : str
Name of the feature
data_type : str
can be nominal, numeric, string, date (corresponds to arff)
nominal_values : list(str)
list of the possible values, in case of nominal attribute
number_missing_values : int
Number of rows that have a missing value for this feature.
ontologies : list(str)
list of ontologies attached to this feature. An ontology describes the
concept that are described in a feature. An ontology is defined by an
URL where the information is provided.
"""
LEGAL_DATA_TYPES: ClassVar[Sequence[str]] = ["nominal", "numeric", "string", "date"]
def __init__( # noqa: PLR0913
self,
index: int,
name: str,
data_type: str,
nominal_values: list[str],
number_missing_values: int,
ontologies: list[str] | None = None,
):
if not isinstance(index, int):
raise TypeError(f"Index must be `int` but is {type(index)}")
if data_type not in self.LEGAL_DATA_TYPES:
raise ValueError(
f"data type should be in {self.LEGAL_DATA_TYPES!s}, found: {data_type}",
)
if data_type == "nominal":
if nominal_values is None:
raise TypeError(
"Dataset features require attribute `nominal_values` for nominal "
"feature type.",
)
if not isinstance(nominal_values, list):
raise TypeError(
"Argument `nominal_values` is of wrong datatype, should be list, "
f"but is {type(nominal_values)}",
)
elif nominal_values is not None:
raise TypeError("Argument `nominal_values` must be None for non-nominal feature.")
if not isinstance(number_missing_values, int):
msg = f"number_missing_values must be int but is {type(number_missing_values)}"
raise TypeError(msg)
self.index = index
self.name = str(name)
self.data_type = str(data_type)
self.nominal_values = nominal_values
self.number_missing_values = number_missing_values
self.ontologies = ontologies
def __repr__(self) -> str:
return "[%d - %s (%s)]" % (self.index, self.name, self.data_type)
def __eq__(self, other: Any) -> bool:
return isinstance(other, OpenMLDataFeature) and self.__dict__ == other.__dict__
def _repr_pretty_(self, pp: pretty.PrettyPrinter, cycle: bool) -> None: # noqa: FBT001, ARG002
pp.text(str(self))