mstz commited on
Commit
7b3e7e2
·
1 Parent(s): d7ddc8f

Upload car.py

Browse files
Files changed (1) hide show
  1. car.py +45 -45
car.py CHANGED
@@ -24,30 +24,30 @@ _BASE_FEATURE_NAMES = [
24
  "class"
25
  ]
26
  urls_per_split = {
27
- "train": "https://huggingface.co/datasets/mstz/car/raw/main/car.data"
28
  }
29
  features_types_per_config = {
30
- "car": {
31
- "buying": datasets.Value("int8"),
32
  "maint": datasets.Value("int8"),
33
  "doors": datasets.Value("int8"),
34
  "persons": datasets.Value("int8"),
35
  "lug_boot": datasets.Value("int8"),
36
  "safety": datasets.Value("int8"),
37
- "class": datasets.ClassLabel(num_classes=4,
38
  names=("unacceptable", "acceptable", "good", "very good"))
39
- },
40
  "car_binary": {
41
- "buying": datasets.Value("int8"),
42
  "maint": datasets.Value("int8"),
43
  "doors": datasets.Value("int8"),
44
  "persons": datasets.Value("int8"),
45
  "lug_boot": datasets.Value("int8"),
46
  "safety": datasets.Value("int8"),
47
- "class": datasets.ClassLabel(num_classes=2,
48
  names=("unacceptable", "acceptable"))
49
- },
50
-
51
  }
52
  features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
53
 
@@ -92,58 +92,58 @@ _ENCODING_DICS = {
92
  }
93
 
94
  class CarConfig(datasets.BuilderConfig):
95
- def __init__(self, **kwargs):
96
- super(CarConfig, self).__init__(version=VERSION, **kwargs)
97
- self.features = features_per_config[kwargs["name"]]
98
 
99
 
100
  class Car(datasets.GeneratorBasedBuilder):
101
- # dataset versions
102
- DEFAULT_CONFIG = "car"
103
- BUILDER_CONFIGS = [
104
- CarConfig(name="car",
105
- description="Car for 4-ary classification."),
106
  CarConfig(name="car_binary",
107
- description="Car for binary classification."),
108
- ]
109
 
110
 
111
- def _info(self):
112
- info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
113
- features=features_per_config[self.config.name])
114
 
115
- return info
116
-
117
- def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
118
- downloads = dl_manager.download_and_extract(urls_per_split)
119
 
120
- return [
121
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]})
122
- ]
123
-
124
- def _generate_examples(self, filepath: str):
125
- data = pandas.read_csv(filepath, header=None)
126
  data = self.preprocess(data)
127
 
128
- for row_id, row in data.iterrows():
129
- data_row = dict(row)
130
 
131
- yield row_id, data_row
132
 
133
  def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
134
- data.columns = _BASE_FEATURE_NAMES
135
 
136
- for feature in _ENCODING_DICS:
137
- encoding_function = partial(self.encode, feature)
138
- data.loc[:, feature] = data[feature].apply(encoding_function)
139
 
140
- if config == "car_binary":
141
  data.loc[:, "class"] = data["class"].apply(lambda x: if x == 0 else 1)
142
 
143
 
144
  return data
145
-
146
- def encode(self, feature, value):
147
- if feature in _ENCODING_DICS:
148
- return _ENCODING_DICS[feature][value]
149
- raise ValueError(f"Unknown feature: {feature}")
 
24
  "class"
25
  ]
26
  urls_per_split = {
27
+ "train": "https://huggingface.co/datasets/mstz/car/raw/main/car.data"
28
  }
29
  features_types_per_config = {
30
+ "car": {
31
+ "buying": datasets.Value("int8"),
32
  "maint": datasets.Value("int8"),
33
  "doors": datasets.Value("int8"),
34
  "persons": datasets.Value("int8"),
35
  "lug_boot": datasets.Value("int8"),
36
  "safety": datasets.Value("int8"),
37
+ "class": datasets.ClassLabel(num_classes=4,
38
  names=("unacceptable", "acceptable", "good", "very good"))
39
+ },
40
  "car_binary": {
41
+ "buying": datasets.Value("int8"),
42
  "maint": datasets.Value("int8"),
43
  "doors": datasets.Value("int8"),
44
  "persons": datasets.Value("int8"),
45
  "lug_boot": datasets.Value("int8"),
46
  "safety": datasets.Value("int8"),
47
+ "class": datasets.ClassLabel(num_classes=2,
48
  names=("unacceptable", "acceptable"))
49
+ },
50
+
51
  }
52
  features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
53
 
 
92
  }
93
 
94
  class CarConfig(datasets.BuilderConfig):
95
+ def __init__(self, **kwargs):
96
+ super(CarConfig, self).__init__(version=VERSION, **kwargs)
97
+ self.features = features_per_config[kwargs["name"]]
98
 
99
 
100
  class Car(datasets.GeneratorBasedBuilder):
101
+ # dataset versions
102
+ DEFAULT_CONFIG = "car"
103
+ BUILDER_CONFIGS = [
104
+ CarConfig(name="car",
105
+ description="Car for 4-ary classification."),
106
  CarConfig(name="car_binary",
107
+ description="Car for binary classification."),
108
+ ]
109
 
110
 
111
+ def _info(self):
112
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
113
+ features=features_per_config[self.config.name])
114
 
115
+ return info
116
+
117
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
118
+ downloads = dl_manager.download_and_extract(urls_per_split)
119
 
120
+ return [
121
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]})
122
+ ]
123
+
124
+ def _generate_examples(self, filepath: str):
125
+ data = pandas.read_csv(filepath, header=None)
126
  data = self.preprocess(data)
127
 
128
+ for row_id, row in data.iterrows():
129
+ data_row = dict(row)
130
 
131
+ yield row_id, data_row
132
 
133
  def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
134
+ data.columns = _BASE_FEATURE_NAMES
135
 
136
+ for feature in _ENCODING_DICS:
137
+ encoding_function = partial(self.encode, feature)
138
+ data.loc[:, feature] = data[feature].apply(encoding_function)
139
 
140
+ if config == "car_binary":
141
  data.loc[:, "class"] = data["class"].apply(lambda x: if x == 0 else 1)
142
 
143
 
144
  return data
145
+
146
+ def encode(self, feature, value):
147
+ if feature in _ENCODING_DICS:
148
+ return _ENCODING_DICS[feature][value]
149
+ raise ValueError(f"Unknown feature: {feature}")