mstz commited on
Commit
9c120fb
1 Parent(s): 40edf89

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +32 -1
  2. magic.py +159 -0
  3. magic04.data +0 -0
README.md CHANGED
@@ -1,3 +1,34 @@
1
  ---
2
- license: cc-by-4.0
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - en
4
+ tags:
5
+ - magic
6
+ - tabular_classification
7
+ - binary_classification
8
+ pretty_name: Magic
9
+ size_categories:
10
+ - 10K<n<100K
11
+ task_categories: # Full list at https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts
12
+ - tabular-classification
13
+ configs:
14
+ - magic
15
  ---
16
+ # Magic
17
+ The [Magic dataset](https://archive.ics.uci.edu/ml/datasets/Magic) from the [UCI ML repository](https://archive.ics.uci.edu/ml/datasets).
18
+
19
+ # Configurations and tasks
20
+ | **Configuration** | **Task** | **Description** |
21
+ |-------------------|---------------------------|---------------------------------------------------------------|
22
+ | magic | Binary classification | Classify the person's magic as over or under the threshold. |
23
+
24
+
25
+ # Usage
26
+ ```python
27
+ from datasets import load_dataset
28
+
29
+ dataset = load_dataset("mstz/magic", "magic")["train"]
30
+ ```
31
+
32
+ # Features
33
+ |**Feature** |**Type** | **Description** |
34
+
magic.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Magic"""
2
+
3
+ from typing import List
4
+ from functools import partial
5
+
6
+ import datasets
7
+
8
+ import pandas
9
+
10
+
11
+ VERSION = datasets.Version("1.0.0")
12
+ _BASE_FEATURE_NAMES = [
13
+ "major_axis_length"
14
+ "minor_axis_length"
15
+ "log_of_sum_of_content"
16
+ "ratio_of_sum_of_highest_pixels_and_size"
17
+ "ratio_of_highest_pixel_and_size"
18
+ "projected_distance_highest_to_center_pixel"
19
+ "third_root_of_third_moment_along_major_axis"
20
+ "third_root_of_third_moment_along_minor_axis"
21
+ "angle_major_axis_to_origin"
22
+ "distance_origin_to_center"
23
+ "class"
24
+ ]
25
+
26
+
27
+ DESCRIPTION = "Magic dataset from the UCI ML repository."
28
+ _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Magic"
29
+ _URLS = ("https://archive.ics.uci.edu/ml/datasets/Magic")
30
+ _CITATION = """
31
+ @misc{misc_magic_gamma_telescope_159,
32
+ author = {Bock,R.},
33
+ title = {{MAGIC Gamma Telescope}},
34
+ year = {2007},
35
+ howpublished = {UCI Machine Learning Repository},
36
+ note = {{DOI}: \\url{10.24432/C52C8B}}
37
+ }"""
38
+
39
+ # Dataset info
40
+ urls_per_split = {
41
+ "train": "https://huggingface.co/datasets/mstz/magic/raw/main/magic04.data"
42
+ }
43
+ features_types_per_config = {
44
+ "magic": {
45
+ "major_axis_length": datasets.Value("float64"),
46
+ "minor_axis_length": datasets.Value("float64"),
47
+ "log_of_sum_of_content": datasets.Value("float64"),
48
+ "ratio_of_sum_of_highest_pixels_and_size": datasets.Value("float64"),
49
+ "ratio_of_highest_pixel_and_size": datasets.Value("float64"),
50
+ "projected_distance_highest_to_center_pixel": datasets.Value("float64"),
51
+ "third_root_of_third_moment_along_major_axis": datasets.Value("float64"),
52
+ "third_root_of_third_moment_along_minor_axis": datasets.Value("float64"),
53
+ "angle_major_axis_to_origin": datasets.Value("float64"),
54
+ "distance_origin_to_center": datasets.Value("float64"),
55
+ "class": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
56
+ }
57
+ }
58
+ features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
59
+
60
+
61
+ class MagicConfig(datasets.BuilderConfig):
62
+ def __init__(self, **kwargs):
63
+ super(MagicConfig, self).__init__(version=VERSION, **kwargs)
64
+ self.features = features_per_config[kwargs["name"]]
65
+
66
+
67
+ class Magic(datasets.GeneratorBasedBuilder):
68
+ # dataset versions
69
+ DEFAULT_CONFIG = "magic"
70
+ BUILDER_CONFIGS = [
71
+ MagicConfig(name="magic",
72
+ description="Magic for binary classification.")
73
+ ]
74
+
75
+
76
+ def _info(self):
77
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
78
+ features=features_per_config[self.config.name])
79
+
80
+ return info
81
+
82
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
83
+ downloads = dl_manager.download_and_extract(urls_per_split)
84
+
85
+ return [
86
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]})
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]
magic04.data ADDED
The diff for this file is too large to render. See raw diff