我发表了一个博客文章 https://technowhisp.com/kaggle-api-python-documentation/这解释了竞争、数据集和内核交互的大多数常见用例。
以下是在 Python 中使用 Kaggle API 所涉及的步骤。
设置 API 密钥
转到您的 Kaggle 帐户选项卡:https://www.kaggle.com/<username>/account
然后点击“创建 API 令牌”。将下载名为 kaggle.json 的文件。将此文件移至 Mac 和 Linux 中的 ~/.kaggle/ 文件夹中,或移至 Windows 上的 C:\Users.kaggle\ 中。
或者,您可以使用 kaggle.json 中的值填充 KAGGLE_USERNAME 和 KAGGLE_KEY 环境变量,以使 api 进行身份验证。
使用 API 服务器进行身份验证
from kaggle.api.kaggle_api_extended import KaggleApi
api = KaggleApi()
api.authenticate()
下载数据集
# Download all files of a dataset
# Signature: dataset_download_files(dataset, path=None, force=False, quiet=True, unzip=False)
api.dataset_download_files('avenn98/world-of-warcraft-demographics')
# download single file
#Signature: dataset_download_file(dataset, file_name, path=None, force=False, quiet=True)
api.dataset_download_file('avenn98/world-of-warcraft-demographics','WoW Demographics.csv')
下载竞赛文件
# Download all files for a competition
# Signature: competition_download_files(competition, path=None, force=False, quiet=True)
api.competition_download_files('titanic')
# Download single file for a competition
# Signature: competition_download_file(competition, file_name, path=None, force=False, quiet=False)
api.competition_download_file('titanic','gender_submission.csv')
提交比赛
# Signature: competition_submit(file_name, message, competition,quiet=False)
api.competition_submit('gender_submission.csv','API Submission','titanic')
检索排行榜
# Signature: competition_view_leaderboard(id, **kwargs)
leaderboard = api.competition_view_leaderboard('titanic')