mstz commited on
Commit
763ff6d
1 Parent(s): 9c120fb

Upload magic.py

Browse files
Files changed (1) hide show
  1. magic.py +7 -70
magic.py CHANGED
@@ -87,73 +87,10 @@ class Magic(datasets.GeneratorBasedBuilder):
87
  ]
88
 
89
  def _generate_examples(self, filepath: str):
90
- if self.config.name == "encoding":
91
- data = self.encodings()
92
-
93
- for row_id, row in data.iterrows():
94
- data_row = dict(row)
95
-
96
- yield row_id, data_row
97
-
98
- elif self.config.name in ["magic", "magic-no race", "race"]:
99
- data = pandas.read_csv(filepath)
100
- data = self.preprocess(data, config=self.config.name)
101
-
102
- for row_id, row in data.iterrows():
103
- data_row = dict(row)
104
-
105
- yield row_id, data_row
106
-
107
- else:
108
- raise ValueError(f"Unknown config: {self.config.name}")
109
-
110
- def encodings(self):
111
- data = [pandas.DataFrame([(feature, original_value, encoded_value)
112
- for original_value, encoded_value in d.items()],
113
- columns=["feature", "original_value", "encoded_value"])
114
- for feature, d in _ENCODING_DICS.items()]
115
- data.append(pandas.DataFrame([("race", original_value, encoded_value)
116
- for original_value, encoded_value in _RACE_ENCODING.items()],
117
- columns=["feature", "original_value", "encoded_value"]))
118
- data.append(pandas.DataFrame([("education", original_value, encoded_value)
119
- for original_value, encoded_value in _EDUCATION_ENCODING.items()],
120
- columns=["feature", "original_value", "encoded_value"]))
121
- data = pandas.concat(data, axis="rows").reset_index()
122
- data.drop("index", axis="columns", inplace=True)
123
-
124
- return data
125
-
126
-
127
- def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
128
- data.drop("education", axis="columns", inplace=True)
129
- data = data.rename(columns={"threshold": "over_threshold", "sex": "is_male"})
130
-
131
- data = data[["age", "capital_gain", "capital_loss", "education-num", "final_weight",
132
- "hours_per_week", "marital_status", "native_country", "occupation",
133
- "race", "relationship", "is_male", "workclass", "over_threshold"]]
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
-
141
- if config == "magic":
142
- return data[list(features_types_per_config["magic"].keys())]
143
- elif config == "magic-no race":
144
- return data[list(features_types_per_config["magic-no race"].keys())]
145
- elif config =="race":
146
- data.loc[:, "race"] = data.race.apply(self.encode_race)
147
- data = data[list(features_types_per_config["race"].keys())]
148
-
149
- return data
150
- else:
151
- raise ValueError(f"Unknown config: {config}")
152
-
153
- def encode(self, feature, value):
154
- if feature in _ENCODING_DICS:
155
- return _ENCODING_DICS[feature][value]
156
- raise ValueError(f"Unknown feature: {feature}")
157
-
158
- def encode_race(self, race):
159
- return _RACE_ENCODING[race]
 
87
  ]
88
 
89
  def _generate_examples(self, filepath: str):
90
+ data = pandas.read_csv(filepath)
91
+ data = self.preprocess(data, config=self.config.name)
92
+
93
+ for row_id, row in data.iterrows():
94
+ data_row = dict(row)
95
+
96
+ yield row_id, data_row