Datasets:
File size: 8,418 Bytes
b707e93 071839c 8b0e745 ee3eeab 071839c b707e93 8b0e745 3236b25 8b0e745 3236b25 8b0e745 3236b25 b707e93 |
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
"""Diva: A Fraud Detection Dataset"""
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
__ORIGINAL_FEATURE_NAMES = [
"age",
"workclass",
"final_weight",
"education", "education-num",
"marital_status",
"occupation",
"relationship",
"race",
"sex",
"capital_gain",
"capital_loss",
"hours_per_week",
"native_country",
"threshold"
]
__BASE_FEATURE_NAMES = [
"age",
"capital_gain",
"capital_loss",
"education",
"final_weight",
"hours_per_week",
"marital_status",
"native_country",
"occupation",
"race",
"relationship",
"sex",
"workclass",
"threshold",
]
__DESCRIPTION = "Adult dataset from the UCI ML repository."
__HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Adult"
__URLS = ("https://huggingface.co/datasets/mstz/adult/raw/adult.csv")
__CITATION = """
@inproceedings{DBLP:conf/kdd/Kohavi96,
author = {Ron Kohavi},
editor = {Evangelos Simoudis and
Jiawei Han and
Usama M. Fayyad},
title = {Scaling Up the Accuracy of Naive-Bayes Classifiers: {A} Decision-Tree
Hybrid},
booktitle = {Proceedings of the Second International Conference on Knowledge Discovery
and Data Mining (KDD-96), Portland, Oregon, {USA}},
pages = {202--207},
publisher = {{AAAI} Press},
year = {1996},
url = {http://www.aaai.org/Library/KDD/1996/kdd96-033.php},
timestamp = {Mon, 05 Jun 2017 13:20:21 +0200},
biburl = {https://dblp.org/rec/conf/kdd/Kohavi96.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}"""
# Dataset info
__urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/adult/raw/adult_tr.csv",
"test": "https://huggingface.co/datasets/mstz/adult/raw/adult_ts.csv"
}
__features_per_config = {
"income": datasets.Features({"age": datasets.Value("int8"),
"capital_gain": datasets.Value("float16"),
"capital_loss": datasets.Value("float16"),
"education": datasets.Value("int8"),
"final_weight": datasets.Value("int16"),
"hours_per_week": datasets.Value("int16"),
"marital_status": datasets.Value("string"),
"native_country": datasets.Value("string"),
"occupation": datasets.Value("string"),
"race": datasets.Value("string"),
"relationship": datasets.Value("string"),
"sex": datasets.Value("binary"),
"workclass": datasets.Value("binary"),
"threshold": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
}),
"income-no race": datasets.Features({"age": datasets.Value("int8"),
"capital_gain": datasets.Value("float16"),
"capital_loss": datasets.Value("float16"),
"education": datasets.Value("int8"),
"final_weight": datasets.Value("int16"),
"hours_per_week": datasets.Value("int16"),
"marital_status": datasets.Value("string"),
"native_country": datasets.Value("string"),
"occupation": datasets.Value("string"),
"relationship": datasets.Value("string"),
"sex": datasets.Value("binary"),
"workclass": datasets.Value("binary"),
"threshold": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
}),
"race": datasets.Features({"age": datasets.Value("int8"),
"capital_gain": datasets.Value("float16"),
"capital_loss": datasets.Value("float16"),
"education": datasets.Value("int8"),
"final_weight": datasets.Value("int16"),
"hours_per_week": datasets.Value("int16"),
"marital_status": datasets.Value("string"),
"native_country": datasets.Value("string"),
"occupation": datasets.Value("string"),
"relationship": datasets.Value("string"),
"sex": datasets.Value("binary"),
"workclass": datasets.Value("binary"),
"over_threshold": datasets.Value("binary"),
"race": datasets.ClassLabel(num_classes=5, names=["White",
"Black",
"Asian-Pac-Islander",
"Amer-Indian-Eskimo",
"Other"]),
}),
}
class AdultConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(AdultConfig, self).__init__(version=VERSION, **kwargs)
print(kwargs)
print(kwargs["name"])
self.features = __features_per_config[kwargs["name"]]
class Adult(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "income"
BUILDER_CONFIGS = [
AdultConfig(name="income",
description="Adult for income threshold binary classification."),
AdultConfig(name="income-no race",
description="Adult for income threshold binary classification, race excluded from features."),
AdultConfig(name="race",
description="Adult for race multiclass classification."),
]
def _info(self):
if self.config_name not in __features_per_config:
raise ValueError(f"Unknown configuration: {self.config_name}")
info = datasets.DatasetInfo(description=__DESCRIPTION, citation=__CITATION, homepage=__HOMEPAGE,
features=__features_per_config[self.config_name])
return info
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
downloads = dl_manager.download_and_extract(__urls_per_split)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloads["test"]}),
]
def _generate_examples(self, filepath: str):
data = pandas.read_csv(filepath)
data = self.preprocess(data, config=self.config_name)
for row in data.iterrows():
data_row = dict(row)
row_id = hash(str(data_row))
yield row_id, data_row
def preprocess(self, data: pandas.DataFrame, config: str = "income") -> pandas.DataFrame:
data.drop(["education"], inplace=True)
data = data[["age", "capital_gain", "capital_loss", "education", "final_weight",
"hours_per_week", "marital_status", "native_country", "occupation",
"race", "relationship", "sex", "workclass", "threshold"]]
data.columns = __BASE_FEATURE_NAMES
return data
def income_preprocessing(data: pandas.DataFrame) -> pandas.DataFrame:
data = data[__features_per_config["income"]]
return data
def income_norace_preprocessing(data: pandas.DataFrame) -> pandas.DataFrame:
data = data[__features_per_config["income-no race"]]
return data
def race_preprocessing(data: pandas.DataFrame) -> pandas.DataFrame:
data["over_threshold"] = df.threshold
data = data[__features_per_config["race"]]
return data
# TODO: add custom split? |