Datasets:
File size: 5,267 Bytes
d3d1931 50b4b3b 69d4f0c 50b4b3b d3d1931 b4b6905 d3d1931 f4f9c0d d3d1931 96c70a3 d3d1931 b4b6905 d3d1931 ad5c8ff d3d1931 06b1793 d3d1931 06b1793 d3d1931 c8da4f1 d3d1931 be7f1e4 d3d1931 73d4a4e d3d1931 d69b3fc d3d1931 96c70a3 d3e681a 96c70a3 d3e681a d3d1931 |
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 |
"""Hypo Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {
"class": {
"negative": 0,
"compensatedhypothyroid": 1,
"secondaryhypothyroid": 2,
"primaryhypothyroid": 3
}
}
DESCRIPTION = "Hypo dataset."
_HOMEPAGE = ""
_URLS = ("")
_CITATION = """"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/hypo/resolve/main/hypo.data"
}
features_types_per_config = {
"hypo": {
"age": datasets.Value("int64"),
"sex": datasets.Value("string"),
"on_thyroxine": datasets.Value("bool"),
"query_on_thyroxine": datasets.Value("bool"),
"on_antithyroid_medication": datasets.Value("bool"),
"sick": datasets.Value("bool"),
"pregnant": datasets.Value("bool"),
"thyroid_surgery": datasets.Value("bool"),
"I131_treatment": datasets.Value("bool"),
"query_hypothyroid": datasets.Value("bool"),
"query_hyperthyroid": datasets.Value("bool"),
"lithium": datasets.Value("bool"),
"goitre": datasets.Value("bool"),
"tumor": datasets.Value("bool"),
"hypopituitary": datasets.Value("bool"),
"psych": datasets.Value("bool"),
"TSH_measured": datasets.Value("bool"),
"TSH": datasets.Value("string"),
"T3_measured": datasets.Value("bool"),
"T3": datasets.Value("float64"),
"TT4_measured": datasets.Value("bool"),
"TT4": datasets.Value("float64"),
"T4U_measured": datasets.Value("bool"),
"T4U": datasets.Value("float64"),
"FTI_measured": datasets.Value("bool"),
"FTI": datasets.Value("float64"),
"TBG_measured": datasets.Value("string"),
"referral_source": datasets.Value("string"),
"class": datasets.ClassLabel(num_classes=4,
names=("negative", "compensated hypothyroid", "secondary hypothyroid", "primary hypothyroid"))
},
"has_hypo": {
"age": datasets.Value("int64"),
"sex": datasets.Value("string"),
"on_thyroxine": datasets.Value("bool"),
"query_on_thyroxine": datasets.Value("bool"),
"on_antithyroid_medication": datasets.Value("bool"),
"sick": datasets.Value("bool"),
"pregnant": datasets.Value("bool"),
"thyroid_surgery": datasets.Value("bool"),
"I131_treatment": datasets.Value("bool"),
"query_hypothyroid": datasets.Value("bool"),
"query_hyperthyroid": datasets.Value("bool"),
"lithium": datasets.Value("bool"),
"goitre": datasets.Value("bool"),
"tumor": datasets.Value("bool"),
"hypopituitary": datasets.Value("bool"),
"psych": datasets.Value("bool"),
"TSH_measured": datasets.Value("bool"),
"TSH": datasets.Value("string"),
"T3_measured": datasets.Value("bool"),
"T3": datasets.Value("string"),
"TT4_measured": datasets.Value("bool"),
"TT4": datasets.Value("float64"),
"T4U_measured": datasets.Value("bool"),
"T4U": datasets.Value("float64"),
"FTI_measured": datasets.Value("bool"),
"FTI": datasets.Value("float64"),
"TBG_measured": datasets.Value("string"),
"referral_source": datasets.Value("string"),
"class": datasets.ClassLabel(num_classes=2)
},
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
class HypoConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(HypoConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Hypo(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "hypo"
BUILDER_CONFIGS = [
HypoConfig(name="hypo", description="Hypo for multiclass classification."),
HypoConfig(name="has_hypo", description="Hypo for binary classification."),
]
def _info(self):
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"]}),
]
def _generate_examples(self, filepath: str):
data = pandas.read_csv(filepath)
data = self.preprocess(data)
for row_id, row in data.iterrows():
data_row = dict(row)
yield row_id, data_row
def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame:
data.drop("id", axis="columns", inplace=True)
data.drop("TBG", axis="columns", inplace=True)
data = data[data.age != "?"]
data = data[data.sex != "?"]
data = data[data.TSH != "?"]
data.loc[data.T3 == "?", "T3"] = -1
data.loc[data.TT4 == "?", "TT4"] = -1
data.loc[data.T4U == "?", "T4U"] = -1
data.loc[data.FTI == "?", "FTI"] = -1
data = data.infer_objects()
for feature in _ENCODING_DICS:
encoding_function = partial(self.encode, feature)
data[feature] = data[feature].apply(encoding_function)
if self.config.name == "has_hypo":
data["class"] = data["class"].apply(lambda x: 0 if x == 0 else 1)
print("has hypo\n\n\n")
print("classes")
print(data["class"].unique())
return data[list(features_types_per_config[self.config.name].keys())]
def encode(self, feature, value):
if feature in _ENCODING_DICS:
return _ENCODING_DICS[feature][value]
raise ValueError(f"Unknown feature: {feature}")
|