How to Download Datasets from Hugging Face

By Tammy

Are you looking to work with a specific dataset for your machine learning or data analysis project? Hugging Face, a leading provider of machine learning tools and resources, hosts a vast collection of datasets that can greatly aid your work. Let's dive into how you can efficiently download a dataset from Hugging Face.

an image of a baby being hugged by an older person with blue skin and hands on his chest
an image of a baby being hugged by an older person with blue skin and hands on his chest

Before we begin, make sure that you have Python and pip installed on your system. Hugging Face datasets are accessed using the datasets library, so we'll need to install that first. You can do this by running the following command in your terminal:

Set Up Hugging Face API for Open Source LLM in Copilot Studio
Set Up Hugging Face API for Open Source LLM in Copilot Studio

Setting Up

Before you start, ensure your environment is ready.

a drawing of a man holding a woman's face in front of the camera
a drawing of a man holding a woman's face in front of the camera

Installing Required Libraries

Open your terminal and install the necessary libraries with these commands:

the stages of hugs in different positions and sizes, with instructions for how to use them
the stages of hugs in different positions and sizes, with instructions for how to use them

pip install datasets

This will ensure you have the datasets library, which is the primary tool for downloading and interacting with Hugging Face datasets.

authenticate for Accessing Private Datasets

It's an AI picture that is not a real person
It's an AI picture that is not a real person

Some datasets on Hugging Face are private and require authentication. If you're downloading such a dataset, you'll need to log in to your Hugging Face account and obtain an access token:

exército auth login

Follow the instructions provided to complete the login process. Once authenticated, you can proceed to download your dataset.

a drawing of two people kissing each other
a drawing of two people kissing each other

Downloading a Dataset

Now that your environment is set up, let's learn how to download a dataset.

three people hugging each other with the words performance - enhancing hugs above them
three people hugging each other with the words performance - enhancing hugs above them
Hug Day 🫂
Hug Day 🫂
a drawing of a person with the words hugs written on it's face and arms
a drawing of a person with the words hugs written on it's face and arms
a black and white drawing of a person with the words hugs
a black and white drawing of a person with the words hugs
two blue monsters hugging each other with their arms around one another and eyes open to the side
two blue monsters hugging each other with their arms around one another and eyes open to the side
Hug's?
Hug's?
a man and woman are hugging each other
a man and woman are hugging each other
16 Different Types Of Hugs And What They Mean
16 Different Types Of Hugs And What They Mean
Hugging Face Launches New ‘State-Of-The-Art’ Visual Language Model – IDEFICS
Hugging Face Launches New ‘State-Of-The-Art’ Visual Language Model – IDEFICS

Finding the Dataset

Visit the Hugging Face dataset hub (huggingface.co/datasets) and search for the dataset you're interested in. Once you've found it, note down the dataset's name. It usually follows this format: 'username/dataset-name' (e.g., ' LymphomaCrowdataset').

Downloading the Dataset

With the dataset name noted, you can download it using the following Python code:

from datasets import load_dataset dataset = load_dataset('username/dataset-name')

Replace 'username/dataset-name' with the actual name of the dataset you found earlier. This code will download the dataset and load it into a DatasetDict object, providing you with easy access to the data.

Exploring and Using the Dataset

Now that you have the dataset downloaded, let's explore and use it.

Accessing the Dataset

You can access the raw data using the following command:

print(dataset['train'][0])

This will print the first example from the training set. You can replace 'train' with 'test' or 'validation' to access those splits.

Transforming the Dataset

Before using the dataset in a machine learning model, you might need to transform or preprocess the data. Hugging Face datasets provide utilities for common transformations. Here's an example of tokenizing a text dataset using the Hugging Face Tokenizer:

from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") def tokenize_function(examples): return tokenizer(examples['text'], padding="max_length", truncation=True) tokenized_dataset = dataset.map(tokenize_function, batched=True)

This code will tokenize the 'text' field of each example in the dataset using the BERT tokenizer.

That's it! You've successfully downloaded a dataset from Hugging Face and are ready to start working with it. Happy data exploring!