*在Python内的Kaggle API文档?

时间:2019-05-01 10:19:31

标签: python kaggle

我想编写一个python脚本,该脚本从Kaggle.com下载公共数据集。

Kaggle API用python编写,但是我能找到的几乎所有文档和资源都是关于如何在命令行中使用API​​的,而很少涉及如何使用kaggle库中的{ {1}}。

某些用户似乎知道如何执行此操作,例如参见several answers to this question,但是这些提示不足以解决我的特定问题。

即,我有一个如下所示的脚本:

python

我通过查看方法的签名来解决这个问题:
from kaggle.api.kaggle_api_extended import KaggleApi api = KaggleApi('content of my json metadata file') file = api.datasets_download_file( owner_slug='the-owner-slug', dataset_slug='the-dataset-slug', file_name='the-file-name.csv', )

我收到以下错误:

api.datasets_download_file(owner_slug, dataset_slug, file_name, **kwargs)

除了解决该特定问题的方法外,我非常高兴知道如何通过Kaggle库解决错误,而不是通过代码本身来解决。实际上,也许这个问题与utf编码无关,但是我不知道如何解决。如果仅仅是文件名错误,或者像这样愚蠢的东西怎么办?

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 12: invalid start byte文件没什么特别的:三列,第一列是时间戳,另外两列是整数。

2 个答案:

答案 0 :(得分:17)

我发表了blog post,解释了竞争,数据集和内核交互的大多数常见用例。

以下是使用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')

# downoad 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')

答案 1 :(得分:0)

您可以检查Kaggle API

的实施情况

但是,如果您很懒,可以在服务器上安装kaggle pip install kaggle

要下载整个比赛,您可以从python调用它。

import os
os.system('kaggle competitions download -c "dogs-vs-cats-redux-kernels-edition"')

进一步,检查docs