Google Colab is an online tool that allows you to run Python notebooks with free GPU acceleration.
Why is that useful?
Some machine learning models take a long time to compute and your local machine might not be able to run them.
The Colab notebooks are similar to Jupyter Notebooks, but they use the Google Drive environment.
You can always upload your dataset to Google Drive and connect your Drive to Colab.
How to Use The Kaggle API With Google Colab
Kaggle is a competition website for data scientists. The site offers tons of challenges and data to flex your data science skills.
You can download a dataset to your local machine, then you can upload the files to Google Drive.
Or you can use the Kaggle API.
- Get your Kaggle API key
Sign into your Kaggle account at kaggle.com: image from StackOverflow by Bob Smith
- Scroll to the API access section and “Create New API Token”:
image from StackOverflow by Bob Smith
Save the token (kaggle.json
) on your machine.
- Go to Google Colab and use the native Files API to mount your drive.
Now you can upload files:
image from StackOverflow by Bob Smith
- Upload the
kaggle.json
file
You can choose the location, but it will most likely begin with /content/drive/My Drive
for Google Drive.
The default location for the Colab Notebooks is /content/drive/My Drive/Colab Notebooks
.
I uploaded my file to /content/drive/My Drive/Colab Notebooks/.kaggle/kaggle.json
.
- Inside your Colab Notebook, run the following code cell:
import os
os.environ['KAGGLE_CONFIG_DIR'] = "/content/drive/My Drive/Colab Notebooks/.kaggle/" # put path for wherever you put it
Now the notebook has access to the API key.
- Use the Kaggle API to download files.
Inside the notebook, you can now use the Kaggle API. You have to run a code cell. Here is the command for the Otto Group Product Classification Challenge:
!kaggle competitions download -c otto-group-product-classification-challenge -d '/content/drive/My Drive/Colab Notebooks'
The -d
flag is for the destination folder. Adjust it to your liking.