m-zhafran commited on
Commit
0e0b906
·
verified ·
1 Parent(s): 09a27c4

Upload datasaur-MmRmNmMwYzE-ODFhMDk2ZTI.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. datasaur-MmRmNmMwYzE-ODFhMDk2ZTI.py +49 -0
datasaur-MmRmNmMwYzE-ODFhMDk2ZTI.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import csv
3
+ import pandas as pd
4
+ import datasets
5
+
6
+ from sklearn.model_selection import train_test_split
7
+ from datasets.tasks import TextClassification
8
+
9
+ _DATASET_LABELS = ['tech', 'politics', 'business', 'entertainment', 'sport']
10
+
11
+ class Custom(datasets.GeneratorBasedBuilder):
12
+
13
+ def _info(self):
14
+ return datasets.DatasetInfo(
15
+ description='',
16
+ features=datasets.Features(
17
+ {
18
+ 'text': datasets.Value('string'),
19
+ 'label': datasets.features.ClassLabel(
20
+ names=_DATASET_LABELS
21
+ ),
22
+ }
23
+ ),
24
+ homepage='',
25
+ citation='',
26
+ task_templates=[
27
+ TextClassification(text_column='text', label_column='label')
28
+ ],
29
+ )
30
+
31
+ def _split_generators(self, dl_manager):
32
+ data_path = dl_manager.download_and_extract('data.csv')
33
+ records = pd.read_csv(data_path)
34
+ train_df, val_df = train_test_split(records, test_size=0.2, random_state=42)
35
+
36
+ return [
37
+ datasets.SplitGenerator(
38
+ name=datasets.Split.TRAIN, gen_kwargs={'df': train_df}
39
+ ),
40
+ datasets.SplitGenerator(
41
+ name=datasets.Split.VALIDATION, gen_kwargs={'df': val_df}
42
+ ),
43
+ ]
44
+
45
+ def _generate_examples(self, df):
46
+ for id_, row in df.iterrows():
47
+ text, label = row['text'], row['label'],
48
+ label = _DATASET_LABELS.index(label)
49
+ yield id_, {'text': text, 'label': label}