Datasets:
File size: 6,467 Bytes
803974e |
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 |
"""StudentPerformance Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
"sex",
"ethnicity",
"parental_level_of_education",
"has_standard_lunch",
"has_completed_preparation_test",
"math_score",
"reading_score",
"writing_score"
]
_ENCODING_DICS = {
"gender": {
"\"female\"": 0,
"\"male\"": 1
},
"parental_level_of_education": {
"some high school": 0,
"high school": 1,
"some college": 2,
"bachelor's degree": 3,
"master's degree": 4,
"associate's degree": 5,
},
"has_standard_lunch" : {
"free/reduced": 0,
"standard": 1
},
"has_completed_preparation_test": {
"none": 0,
"completed": 1
}
}
DESCRIPTION = "StudentPerformance dataset."
_HOMEPAGE = "https://www.kaggle.com/datasets/ulrikthygepedersen/student_performances"
_URLS = ("https://www.kaggle.com/datasets/ulrikthygepedersen/student_performances")
_CITATION = """"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/student_performances/raw/main/student_performances.csv",
}
features_types_per_config = {
"encoding": {
"feature": datasets.Value("string"),
"original_value": datasets.Value("string"),
"encoded_value": datasets.Value("int64")
},
"math": {
"sex": datasets.Value("int8"),
"ethnicity": datasets.Value("string"),
"parental_level_of_education": datasets.Value("int8"),
"has_standard_lunch": datasets.Value("int8"),
"test_preparation_course": datasets.Value("string"),
"reading_score": datasets.Value("int64"),
"writing_score": datasets.Value("int64"),
"has_passed_math_exam": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
},
"writing": {
"sex": datasets.Value("int8"),
"ethnicity": datasets.Value("string"),
"parental_level_of_education": datasets.Value("int8"),
"has_standard_lunch": datasets.Value("int8"),
"test_preparation_course": datasets.Value("string"),
"reading_score": datasets.Value("int64"),
"math_score": datasets.Value("int64"),
"has_passed_writing_exam": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
},
"reading": {
"sex": datasets.Value("int8"),
"ethnicity": datasets.Value("string"),
"parental_level_of_education": datasets.Value("int8"),
"has_standard_lunch": datasets.Value("int8"),
"test_preparation_course": datasets.Value("string"),
"writing_score": datasets.Value("int64"),
"math_score": datasets.Value("int64"),
"has_passed_reading_exam": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
}
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
class StudentPerformanceConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(StudentPerformanceConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class StudentPerformance(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "math"
BUILDER_CONFIGS = [
StudentPerformanceConfig(name="encoding",
description="Encoding dictionaries."),
StudentPerformanceConfig(name="math",
description="Binary classification, predict if the student has passed the math exam."),
StudentPerformanceConfig(name="reading",
description="Binary classification, predict if the student has passed the reading exam."),
StudentPerformanceConfig(name="writing",
description="Binary classification, predict if the student has passed the writing exam."),
]
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"]}),
]
def _generate_examples(self, filepath: str):
data = pandas.read_csv(filepath)
data = self.preprocess(data, config=self.config.name)
for row_id, row in data.iterrows():
data_row = dict(row)
yield row_id, data_row
def preprocess(self, data: pandas.DataFrame, config: str = "cut") -> pandas.DataFrame:
if config == "encoding":
return self.encoding_dics()
data.columns = [c.replace("\"", "") for c in data.columns]
data.loc[:, "race/ethnicity"] = data["race/ethnicity"].apply(lambda x: x.replace("group ", ""))
for feature in _ENCODING_DICS:
encoding_function = partial(self.encode, feature)
data.loc[:, feature] = data[feature].apply(encoding_function)
data.columns = _BASE_FEATURE_NAMES
if config == "math":
data = data.rename(colums={"math_score", "has_passed_math_exam"})
return data[list(features_types_per_config["math"].keys())]
elif config == "reading":
data = data.rename(colums={"reading_score", "has_passed_reading_exam"})
return data[list(features_types_per_config["reading"].keys())]
elif config == "writing":
data = data.rename(colums={"writing_score", "has_passed_writing_exam"})
return data[list(features_types_per_config["writing"].keys())]
else:
raise ValueError(f"Unknown config: {config}")
def encode(self, feature, value):
return _ENCODING_DICS[feature][value]
def encoding_dics(self):
data = [pandas.Dataframe([(feature, original, encoded) for original, encoded in d.items()])
for feature, d in _ENCODING_DICS.items()]
data = pandas.concat(data, axis="rows")
return data
|