資料集文件

從 Hub 載入資料集

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

從 Hub 載入資料集

要找到可重現且易於存取的高品質資料集可能很困難。🤗 Datasets 的主要目標之一,就是提供一種簡單的方法來載入任何格式或類型的資料集。入門最簡單的方法是在 Hugging Face Hub 上探索現有的資料集——這是一個由社群驅動的資料集集合,涵蓋自然語言處理(NLP)、電腦視覺和音訊等任務——並使用 🤗 Datasets 來下載和產生這些資料集。

本教學使用 rotten_tomatoesMInDS-14 資料集,但您可以隨意載入任何您想要的資料集並跟著操作。現在就前往 Hub,為您的任務找到合適的資料集吧!

載入資料集

在花時間下載資料集之前,快速獲取有關該資料集的一些基本資訊通常很有幫助。資料集的資訊儲存在 DatasetInfo 中,其中可能包含資料集描述、特徵和資料集大小等資訊。

使用 load_dataset_builder() 函式來載入資料集建立器 (dataset builder),以便在不實際下載的情況下檢查資料集的屬性。

>>> from datasets import load_dataset_builder
>>> ds_builder = load_dataset_builder("cornell-movie-review-data/rotten_tomatoes")

# Inspect dataset description
>>> ds_builder.info.description
Movie Review Dataset. This is a dataset of containing 5,331 positive and 5,331 negative processed sentences from Rotten Tomatoes movie reviews. This data was first used in Bo Pang and Lillian Lee, ``Seeing stars: Exploiting class relationships for sentiment categorization with respect to rating scales.'', Proceedings of the ACL, 2005.

# Inspect dataset features
>>> ds_builder.info.features
{'label': ClassLabel(names=['neg', 'pos']),
 'text': Value('string')}

如果您對該資料集感到滿意,請使用 load_dataset() 進行載入。

>>> from datasets import load_dataset

>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")

分割集

分割 (split) 是資料集的特定子集,例如 train (訓練集) 和 test (測試集)。使用 get_dataset_split_names() 函式列出資料集的分割名稱。

>>> from datasets import get_dataset_split_names

>>> get_dataset_split_names("cornell-movie-review-data/rotten_tomatoes")
['train', 'validation', 'test']

然後,您可以使用 split 參數載入特定的分割。載入資料集的 split 會回傳一個 Dataset 物件。

>>> from datasets import load_dataset

>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")
>>> dataset
Dataset({
    features: ['text', 'label'],
    num_rows: 8530
})

如果您沒有指定 split,🤗 Datasets 將會改為回傳一個 DatasetDict 物件。

>>> from datasets import load_dataset

>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes")
DatasetDict({
    train: Dataset({
        features: ['text', 'label'],
        num_rows: 8530
    })
    validation: Dataset({
        features: ['text', 'label'],
        num_rows: 1066
    })
    test: Dataset({
        features: ['text', 'label'],
        num_rows: 1066
    })
})

配置 (Configurations)

有些資料集包含多個子資料集。例如,MInDS-14 資料集具有多個子資料集,每個子資料集都包含不同語言的音訊資料。這些子資料集稱為「配置」(configurations) 或「子集」(subsets),在載入資料集時,您必須明確選擇其中一個。如果您沒有提供配置名稱,🤗 Datasets 將會引發 ValueError 並提醒您選擇一個配置。

使用 get_dataset_config_names() 函式來檢索該資料集所有可用配置的清單。

>>> from datasets import get_dataset_config_names

>>> configs = get_dataset_config_names("PolyAI/minds14")
>>> print(configs)
['cs-CZ', 'de-DE', 'en-AU', 'en-GB', 'en-US', 'es-ES', 'fr-FR', 'it-IT', 'ko-KR', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'zh-CN', 'all']

然後載入您想要的配置。

>>> from datasets import load_dataset

>>> mindsFR = load_dataset("PolyAI/minds14", "fr-FR", split="train")
在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.