Download create_dataset.py from SetFit/20_newsgroups: direct link, hf CLI and curl.
- Browser
- Download file 720 Bytes
-
https://huggingface.co/datasets/SetFit/20_newsgroups/resolve/main/create_dataset.py
- Command line
-
hf download hf://datasets/SetFit/20_newsgroups/create_dataset.py
-
curl -L -o create_dataset.py https://huggingface.co/datasets/SetFit/20_newsgroups/resolve/main/create_dataset.py
720 Bytes
| 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() | |