from datasets import Dataset | |
from sklearn.datasets import fetch_20newsgroups | |
def main(): | |
for split in ["train", "test"]: | |
# Follow recommendation to strip newsgroup metadata | |
# https://scikit-learn.org/0.19/datasets/twenty_newsgroups.html#filtering-text-for-more-realistic-training | |
data = fetch_20newsgroups(subset=split, remove=("headers", "footers", "quotes")) | |
id2label = {idx: label for idx, label in enumerate(data["target_names"])} | |
d = {"text": data["data"], "label": data["target"]} | |
dset = Dataset.from_dict(d) | |
dset = dset.map(lambda x: {"label_text": id2label[x["label"]]}) | |
dset.to_json(f"{split}.jsonl") | |
if __name__ == "__main__": | |
main() | |