How To Setup a Kaggle API

Continuing my Data Science journey with Kaggle
kaggle
datascience
coding
Author

Tony Phung

Published

January 27, 2024

I’m planning to learn and test myself with competitions on Kaggle.

Kaggle is a place with real-world problems where Data Scientists and alike can go against each other to solve problems with Machine Learning.

From my understanding:
- There is a validation set where your model is tested against and a public leader board to see how you’re going.
- At the end of the competition, there is an unseen test set where everyones models is tested against and where the final rankings are determined.
- This is quite reflective of real world where preparing a representative validation set is vital, thus will perform well on the test set.
- A common mistake for newbs is over-fitting to the validation set. I’m ready to make that mistake 🤣.

As for the Kaggle API, you can download the kernel which is the necessary datasets and source files to do the competitions.

Alternatively, I can use the notebooks on their website. I plan to try doing competitions both ways.

This is how I set up my Kaggle API

1. Install Library

pip install python

2. Create API token (.json file)

  1. Go to Kaggle
  2. Go to Settings
  3. Create New Token

3. Save to your local .kaggle folder (Windows)

Location: C:\Users\<Windows-username>\.kaggle\kaggle.json
Kaggle Github Reference

3.1 Pasted into the wrong folder?

If you did something wrong then ran kaggle in the terminal, you’ll an error (telling you where to put it):

4 Start Kaggling

import kaggle
??kaggle
Type:        module
String form: <module 'kaggle' from 'c:\\Users\\tonyp\\miniconda3\\envs\\fastai\\Lib\\site-packages\\kaggle\\__init__.py'>
File:        c:\users\tonyp\miniconda3\envs\fastai\lib\site-packages\kaggle\__init__.py
Source:     
#!/usr/bin/python
#
# Copyright 2024 Kaggle Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# coding=utf-8
from __future__ import absolute_import
from kaggle.api.kaggle_api_extended import KaggleApi
from kaggle.api_client import ApiClient

api = KaggleApi(ApiClient())
api.authenticate()