🎭 How to Connect Kaggle with Google Colab and Download Datasets! πŸ“₯

Nischal BaidarNischal Baidar
2 min read

Connecting Kaggle with Google Colab is super easy! By following the steps below, you can download datasets directly into your Colab environment and start working on your machine learning projects right away. πŸš€

Step 1: Create a Kaggle Account and Generate API Token πŸ”‘

  1. Sign in to Kaggle: If you don't already have a Kaggle account, head over to Kaggle and create one.

  2. Generate API Token: Go to your Kaggle account settings by clicking on your profile picture. Scroll down to the "API" section and click "Create New API Token." This will download a file named kaggle.json containing your credentials.

Step 2: Upload Kaggle API Key to Colab πŸ“

  1. Open Google Colab: Start a new notebook in Google Colab.

  2. Upload kaggle.json: Use the code snippet below to create a Kaggle directory and upload the API key file.

     !mkdir -p ~/.kaggle
     !cp kaggle.json ~/.kaggle/
    

Step 3: Set Permissions to Secure Your API Key πŸ”’

Set the permissions to make sure your API key is safe and not readable by others.

!chmod 600 ~/.kaggle/kaggle.json

Step 4: Download Dataset from Kaggle to Colab πŸ’Ύ

With everything set up, you can now download any Kaggle dataset using the Kaggle API! πŸŽ‰

Here’s how to download a dataset, for example, the Face Mask Detection dataset:

!kaggle datasets download -d wobotintelligence/face-mask-detection-dataset

Step 5: Extract the Downloaded Dataset πŸ“¦

Most datasets come in compressed format. Use the following code to unzip the dataset:

import zipfile
zip_ref = zipfile.ZipFile('face-mask-detection-dataset.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()

Step 6: Verify Your Dataset 🎯

Finally, check if the dataset has been extracted correctly by listing the files in your working directory.

!ls /content

And That’s It! You're All Set! 🎊

Now you can start exploring the dataset and building amazing machine learning models. Connecting Kaggle to Google Colab is this easy and powerful, letting you utilize the cloud resources for your projects.

Happy Coding! πŸ₯³πŸ’»

0
Subscribe to my newsletter

Read articles from Nischal Baidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nischal Baidar
Nischal Baidar