mstz commited on
Commit
daf4c03
·
1 Parent(s): 803974e

Upload student_performance.py

Browse files
Files changed (1) hide show
  1. student_performance.py +175 -0
student_performance.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """StudentPerformance Dataset"""
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
+ "sex",
14
+ "ethnicity",
15
+ "parental_level_of_education",
16
+ "has_standard_lunch",
17
+ "has_completed_preparation_test",
18
+ "math_score",
19
+ "reading_score",
20
+ "writing_score"
21
+ ]
22
+
23
+ _ENCODING_DICS = {
24
+ "gender": {
25
+ "\"female\"": 0,
26
+ "\"male\"": 1
27
+ },
28
+ "parental_level_of_education": {
29
+ "some high school": 0,
30
+ "high school": 1,
31
+ "some college": 2,
32
+ "bachelor's degree": 3,
33
+ "master's degree": 4,
34
+ "associate's degree": 5,
35
+ },
36
+ "has_standard_lunch" : {
37
+ "free/reduced": 0,
38
+ "standard": 1
39
+ },
40
+ "has_completed_preparation_test": {
41
+ "none": 0,
42
+ "completed": 1
43
+ }
44
+ }
45
+
46
+ DESCRIPTION = "StudentPerformance dataset."
47
+ _HOMEPAGE = "https://www.kaggle.com/datasets/ulrikthygepedersen/student_performances"
48
+ _URLS = ("https://www.kaggle.com/datasets/ulrikthygepedersen/student_performances")
49
+ _CITATION = """"""
50
+
51
+ # Dataset info
52
+ urls_per_split = {
53
+ "train": "https://huggingface.co/datasets/mstz/student_performances/raw/main/student_performances.csv",
54
+ }
55
+ features_types_per_config = {
56
+ "encoding": {
57
+ "feature": datasets.Value("string"),
58
+ "original_value": datasets.Value("string"),
59
+ "encoded_value": datasets.Value("int64")
60
+ },
61
+ "math": {
62
+ "sex": datasets.Value("int8"),
63
+ "ethnicity": datasets.Value("string"),
64
+ "parental_level_of_education": datasets.Value("int8"),
65
+ "has_standard_lunch": datasets.Value("int8"),
66
+ "test_preparation_course": datasets.Value("string"),
67
+ "reading_score": datasets.Value("int64"),
68
+ "writing_score": datasets.Value("int64"),
69
+ "has_passed_math_exam": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
70
+ },
71
+ "writing": {
72
+ "sex": datasets.Value("int8"),
73
+ "ethnicity": datasets.Value("string"),
74
+ "parental_level_of_education": datasets.Value("int8"),
75
+ "has_standard_lunch": datasets.Value("int8"),
76
+ "test_preparation_course": datasets.Value("string"),
77
+ "reading_score": datasets.Value("int64"),
78
+ "math_score": datasets.Value("int64"),
79
+ "has_passed_writing_exam": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
80
+ },
81
+ "reading": {
82
+ "sex": datasets.Value("int8"),
83
+ "ethnicity": datasets.Value("string"),
84
+ "parental_level_of_education": datasets.Value("int8"),
85
+ "has_standard_lunch": datasets.Value("int8"),
86
+ "test_preparation_course": datasets.Value("string"),
87
+ "writing_score": datasets.Value("int64"),
88
+ "math_score": datasets.Value("int64"),
89
+ "has_passed_reading_exam": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
90
+ }
91
+ }
92
+ features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
93
+
94
+
95
+ class StudentPerformanceConfig(datasets.BuilderConfig):
96
+ def __init__(self, **kwargs):
97
+ super(StudentPerformanceConfig, self).__init__(version=VERSION, **kwargs)
98
+ self.features = features_per_config[kwargs["name"]]
99
+
100
+
101
+ class StudentPerformance(datasets.GeneratorBasedBuilder):
102
+ # dataset versions
103
+ DEFAULT_CONFIG = "math"
104
+ BUILDER_CONFIGS = [
105
+ StudentPerformanceConfig(name="encoding",
106
+ description="Encoding dictionaries."),
107
+ StudentPerformanceConfig(name="math",
108
+ description="Binary classification, predict if the student has passed the math exam."),
109
+ StudentPerformanceConfig(name="reading",
110
+ description="Binary classification, predict if the student has passed the reading exam."),
111
+ StudentPerformanceConfig(name="writing",
112
+ description="Binary classification, predict if the student has passed the writing exam."),
113
+ ]
114
+
115
+
116
+ def _info(self):
117
+ if self.config.name not in features_per_config:
118
+ raise ValueError(f"Unknown configuration: {self.config.name}")
119
+
120
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
121
+ features=features_per_config[self.config.name])
122
+
123
+ return info
124
+
125
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
126
+ downloads = dl_manager.download_and_extract(urls_per_split)
127
+
128
+ return [
129
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
130
+ ]
131
+
132
+ def _generate_examples(self, filepath: str):
133
+ data = pandas.read_csv(filepath)
134
+ data = self.preprocess(data, config=self.config.name)
135
+
136
+ for row_id, row in data.iterrows():
137
+ data_row = dict(row)
138
+
139
+ yield row_id, data_row
140
+
141
+ def preprocess(self, data: pandas.DataFrame, config: str = "cut") -> pandas.DataFrame:
142
+ if config == "encoding":
143
+ return self.encoding_dics()
144
+
145
+ data.columns = [c.replace("\"", "") for c in data.columns]
146
+
147
+ data.loc[:, "race/ethnicity"] = data["race/ethnicity"].apply(lambda x: x.replace("group ", ""))
148
+
149
+ for feature in _ENCODING_DICS:
150
+ encoding_function = partial(self.encode, feature)
151
+ data.loc[:, feature] = data[feature].apply(encoding_function)
152
+
153
+ data.columns = _BASE_FEATURE_NAMES
154
+
155
+ if config == "math":
156
+ data = data.rename(colums={"math_score", "has_passed_math_exam"})
157
+ return data[list(features_types_per_config["math"].keys())]
158
+ elif config == "reading":
159
+ data = data.rename(colums={"reading_score", "has_passed_reading_exam"})
160
+ return data[list(features_types_per_config["reading"].keys())]
161
+ elif config == "writing":
162
+ data = data.rename(colums={"writing_score", "has_passed_writing_exam"})
163
+ return data[list(features_types_per_config["writing"].keys())]
164
+ else:
165
+ raise ValueError(f"Unknown config: {config}")
166
+
167
+ def encode(self, feature, value):
168
+ return _ENCODING_DICS[feature][value]
169
+
170
+ def encoding_dics(self):
171
+ data = [pandas.Dataframe([(feature, original, encoded) for original, encoded in d.items()])
172
+ for feature, d in _ENCODING_DICS.items()]
173
+ data = pandas.concat(data, axis="rows")
174
+
175
+ return data