2018.8.7 kaggle API

kaggle API

安裝

確保安裝了Python和軟件包管理pip器。

運行以下命令以使用命令行訪問Kaggle API:

pip install kaggle(您可能需要pip install –user kaggle在Mac / Linux上進行。如果在安裝過程中出現問題,建議使用此方法。)通過root用戶(即sudo pip install kaggle)完成的安裝將無法正常工作,除非您瞭解自己在做什麼。即便如此,他們仍然可能無法工作。強烈建議在權限錯誤的情況下安裝用戶。

您現在可以使用kaggle以下示例中所示的命令。

如果遇到kaggle: command not found錯誤,請確保您的python二進制文件在您的路徑上。您可以kaggle通過執行pip uninstall kaggle和查看二進制文件的位置來查看安裝位置。對於Linux上的本地用戶安裝,默認位置爲~/.local/bin。在Windows上,默認位置爲$PYTHON_HOME/Scripts。

API憑據

要使用Kaggle API,請在https://www.kaggle.com上註冊Kaggle帳戶。然後轉到用戶個人資料的“帳戶”標籤(https://www.kaggle.com//account),然後選擇“創建API令牌”。這將觸發下載kaggle.json包含您的API憑據的文件。將此文件放在該位置~/.kaggle/kaggle.json(在該位置的Windows上C:\Users\.kaggle\kaggle.json- 您可以檢查確切的位置,沒有驅動器,有echo %HOMEPATH%)。您可以定義一個shell環境變量KAGGLE_CONFIG_DIR來將此位置更改爲$KAGGLE_CONFIG_DIR/kaggle.json(在Windows上它將是%KAGGLE_CONFIG_DIR%\kaggle.json)。

爲了您的安全,請確保您計算機的其他用戶沒有您的憑據的讀取權限。在基於Unix的系統上,您可以使用以下命令執行此操作:

chmod 600 ~/.kaggle/kaggle.json

您還可以選擇將Kaggle用戶名和令牌導出到環境中:

export KAGGLE_USER = datadinosaur
export KAGGLE_TOKEN = xxxxxxxxxxxxxx
此外,您可以導出通常以$HOME/.kaggle/kaggle.json“KAGGLE_”格式(注意大寫)的任何其他配置值。
例如,如果文件具有變量“proxy”,則您將導出KAGGLE_PROXY 並由客戶端發現它。

命令

命令行工具支持以下命令:

kaggle competitions {list,files,download,submit,submissions,leaderboard}
kaggle datasets {list, files, download, create, version, init}
kaggle config {view, set, unset}
有關使用其中每個命令的詳細信息,請參閱

比賽

API支持Kaggle Competitions的以下命令。

usage: kaggle competitions [-h]
{list,files,download,submit,submissions,leaderboard}

optional arguments:
-h, –help show this help message and exit

commands:
{list,files,download,submit,submissions,leaderboard}
list List available competitions
files List competition files
download Download competition files
submit Make a new competition submission
submissions Show your competition submissions
leaderboard Get competition leaderboard information
列出比賽

usage: kaggle competitions list [-h] [-p PAGE] [-s SEARCH] [-v]

optional arguments:
-h, –help show this help message and exit
-p PAGE, –page PAGE page number
-s SEARCH, –search SEARCH
term(s) to search for
-v, –csv print in CSV format
(if not set print in table format)
例:

kaggle competitions list -s health

列出競爭文件

usage: kaggle competitions files [-h] [-v] [-q] [competition]

optional arguments:
-h, –help show this help message and exit
competition Competition URL suffix (use “kaggle competitions list” to show options)
If empty, the default competition will be used (use “kaggle config set competition”)”
-v, –csv Print results in CSV format (if not set print in table format)
-q, –quiet Suppress printing information about the upload/download progress
例:

kaggle competitions files favorita-grocery-sales-forecasting

下載競賽文件

usage: kaggle competitions download [-h] [-f FILE_NAME] [-p PATH] [-w] [-o]
[-q]
[competition]

optional arguments:
-h, –help show this help message and exit
competition Competition URL suffix (use “kaggle competitions list” to show options)
If empty, the default competition will be used (use “kaggle config set competition”)”
-f FILE_NAME, –file FILE_NAME
File name, all files downloaded if not provided
(use “kaggle competitions files -c ” to show options)
-p PATH, –path PATH Folder where file(s) will be downloaded, defaults to current working directory
-w, –wp Download files to current working path
-o, –force Skip check whether local version of file is up to date, force file download
-q, –quiet Suppress printing information about the upload/download progress
例子:

kaggle competitions download favorita-grocery-sales-forecasting

kaggle competitions download favorita-grocery-sales-forecasting -f test.csv.7z

注意:您需要接受競爭規則https://www.kaggle.com/c//rules。

提交比賽

usage: kaggle competitions submit [-h] -f FILE_NAME -m MESSAGE [-q]
[competition]

required arguments:
-f FILE_NAME, –file FILE_NAME
File for upload (full path)
-m MESSAGE, –message MESSAGE
Message describing this submission

optional arguments:
-h, –help show this help message and exit
competition Competition URL suffix (use “kaggle competitions list” to show options)
If empty, the default competition will be used (use “kaggle config set competition”)”
-q, –quiet Suppress printing information about the upload/download progress
例:

kaggle competitions submit favorita-grocery-sales-forecasting -f sample_submission_favorita.csv.7z -m “My submission message”

注意:您需要接受競爭規則https://www.kaggle.com/c//rules。

列出競爭提交

usage: kaggle competitions submissions [-h] [-v] [-q] [competition]

optional arguments:
-h, –help show this help message and exit
competition Competition URL suffix (use “kaggle competitions list” to show options)
If empty, the default competition will be used (use “kaggle config set competition”)”
-v, –csv Print results in CSV format (if not set print in table format)
-q, –quiet Suppress printing information about the upload/download progress
例:

kaggle competitions submissions favorita-grocery-sales-forecasting

注意:您需要接受競爭規則https://www.kaggle.com/c//rules。

獲得競爭排行榜

usage: kaggle competitions leaderboard [-h] [-s] [-d] [-p PATH] [-v] [-q]
[competition]

optional arguments:
-h, –help show this help message and exit
competition Competition URL suffix (use “kaggle competitions list” to show options)
If empty, the default competition will be used (use “kaggle config set competition”)”
-s, –show Show the top of the leaderboard
-d, –download Download entire leaderboard
-p PATH, –path PATH Folder where file(s) will be downloaded, defaults to current working directory
-v, –csv Print results in CSV format (if not set print in table format)
-q, –quiet Suppress printing information about the upload/download progress
例:

kaggle competitions leaderboard favorita-grocery-sales-forecasting -s

數據集

API支持以下用於Kaggle數據集的命令。

usage: kaggle datasets [-h]
{list,files,download,create,version,init,metadata} …

optional arguments:
-h, –help show this help message and exit

commands:
{list,files,download,create,version,init,metadata}
list List available datasets
files List dataset files
download Download dataset files
create Create a new dataset
version Create a new dataset version
init Initialize metadata file for dataset creation
metadata Download metadata about a dataset
列出數據集

usage: kaggle datasets list [-h] [-p PAGE] [-s SEARCH] [-v]

optional arguments:
-h, –help show this help message and exit
-p PAGE, –page PAGE Page number for results paging
-s SEARCH, –search SEARCH
Term(s) to search for
-v, –csv Print results in CSV format (if not set print in table format)
例:

kaggle datasets list -s demographics

列出數據集的文件

usage: kaggle datasets files [-h] [-v] [dataset]

optional arguments:
-h, –help show this help message and exit
dataset Dataset URL suffix in format / (use “kaggle datasets list” to show options)
-v, –csv Print results in CSV format (if not set print in table format)
例:

kaggle datasets files zillow/zecon

下載數據集文件

usage: kaggle datasets download [-h] [-f FILE_NAME] [-p PATH] [-w] [-o] [-q]
[dataset]

optional arguments:
-h, –help show this help message and exit
dataset Dataset URL suffix in format / (use “kaggle datasets list” to show options)
-f FILE_NAME, –file FILE_NAME
File name, all files downloaded if not provided
(use “kaggle datasets files -d ” to show options)
-p PATH, –path PATH Folder where file(s) will be downloaded, defaults to current working directory
-w, –wp Download files to current working path
-o, –force Skip check whether local version of file is up to date, force file download
-q, –quiet Suppress printing information about the upload/download progress
例子:

kaggle datasets download zillow/zecon

kaggle datasets download zillow/zecon -f State_time_series.csv

初始化元數據文件以創建數據集

usage: kaggle datasets init [-h] [-p FOLDER]

optional arguments:
-h, –help show this help message and exit
-p FOLDER, –path FOLDER
Folder for upload, containing data files and a special dataset-metadata.json file (https://github.com/Kaggle/kaggle-api/wiki/Dataset-Metadata). Defaults to current working directory
例:

kaggle datasets init -p /path/to/dataset

創建一個新數據集

usage: kaggle datasets create [-h] [-p FOLDER] [-u] [-q] [-t]

optional arguments:
-h, –help show this help message and exit
-p FOLDER, –path FOLDER
Folder for upload, containing data files and a special dataset-metadata.json file (https://github.com/Kaggle/kaggle-api/wiki/Dataset-Metadata). Defaults to current working directory
-u, –public Create publicly (default is private)
-q, –quiet Suppress printing information about the upload/download progress
-t, –keep-tabular Do not convert tabular files to CSV (default is to convert)
例:

kaggle datasets create -p /path/to/dataset

創建新的數據集版本

usage: kaggle datasets version [-h] -m VERSION_NOTES [-p FOLDER] [-q] [-t]
[-d]

required arguments:
-m VERSION_NOTES, –message VERSION_NOTES
Message describing the new version

optional arguments:
-h, –help show this help message and exit
-p FOLDER, –path FOLDER
Folder for upload, containing data files and a special dataset-metadata.json file (https://github.com/Kaggle/kaggle-api/wiki/Dataset-Metadata). Defaults to current working directory
-q, –quiet Suppress printing information about the upload/download progress
-t, –keep-tabular Do not convert tabular files to CSV (default is to convert)
-d, –delete-old-versions
Delete old versions of this dataset
例:

kaggle datasets version -p /path/to/dataset -m “Updated data”

下載現有數據集的元數據

usage: kaggle datasets metadata [-h] [-p PATH] [dataset]

optional arguments:
-h, –help show this help message and exit
dataset Dataset URL suffix in format / (use “kaggle datasets list” to show options)
-p PATH, –path PATH Location to download dataset metadata to. Defaults to current working directory
例:

kaggle datasets metadata -p /path/to/download zillow/zecon

獲取數據集創建狀態

usage: kaggle datasets status [-h] [dataset]

optional arguments:
-h, –help show this help message and exit
dataset Dataset URL suffix in format / (use “kaggle datasets list” to show options)
例:

kaggle datasets status zillow/zecon

API支持Kaggle內核的以下命令。

usage: kaggle kernels [-h] {list,init,push,pull,output,status} …

optional arguments:
-h, –help show this help message and exit

commands:
{list,init,push,pull,output,status}
list List available kernels
init Initialize metadata file for a kernel
push Push new code to a kernel and run the kernel
pull Pull down code from a kernel
output Get data output from the latest kernel run
status Display the status of the latest kernel run
列出內核

usage: kaggle kernels list [-h] [-m] [-p PAGE] [-s SEARCH] [-v]
[–parent PARENT] [–competition COMPETITION]
[–dataset DATASET] [–parent-kernel PARENT_KERNEL]
[–user USER] [–language LANGUAGE]
[–kernel-type KERNEL_TYPE]
[–output-type OUTPUT_TYPE] [–sort-by SORT_BY]

optional arguments:
-h, –help show this help message and exit
-m, –mine Display only my items
-p PAGE, –page PAGE Page number for results paging
-s SEARCH, –search SEARCH
Term(s) to search for
-v, –csv Print results in CSV format (if not set print in table format)
–parent PARENT Find children of the specified parent kernel
–competition COMPETITION
Find kernels for a given competition
–dataset DATASET Find kernels for a given dataset
–user USER Find kernels created by a given user
–language LANGUAGE Specify the language the kernel is written in. Valid options are ‘all’, ‘python’, ‘r’, ‘sqlite’, and ‘julia’
–kernel-type KERNEL_TYPE
Specify the type of kernel. Valid options are ‘all’, ‘script’, and ‘notebook’
–output-type OUTPUT_TYPE
Search for specific kernel output types. Valid options are ‘all’, ‘visualizations’, and ‘data’
–sort-by SORT_BY Sort list results. Valid options are ‘hotness’, ‘commentCount’, ‘dateCreated’, ‘dateRun’, ‘relevance’, ‘scoreAscending’, ‘scoreDescending’, ‘viewCount’, and ‘voteCount’. ‘relevance’ is only applicable ifa search term is specified.
例:

kaggle kernels list -s titanic

初始化內核的元數據文件

usage: kaggle kernels init [-h] [-p FOLDER]

optional arguments:
-h, –help show this help message and exit
-p FOLDER, –path FOLDER
Folder for upload, containing data files and a special kernel-metadata.json file (https://github.com/Kaggle/kaggle-api/wiki/Kernel-Metadata). Defaults to current working directory
例:

kaggle kernels init -p /path/to/kernel

推送內核

usage: kaggle kernels push [-h] -p FOLDER

optional arguments:
-h, –help show this help message and exit
-p FOLDER, –path FOLDER
Folder for upload, containing data files and a special kernel-metadata.json file (https://github.com/Kaggle/kaggle-api/wiki/Kernel-Metadata). Defaults to current working directory
例:

kaggle kernels push -p /path/to/kernel

拉核心

usage: kaggle kernels pull [-h] [-p PATH] [-w] [-m] [kernel]

optional arguments:
-h, –help show this help message and exit
kernel Kernel URL suffix in format / (use “kaggle kernels list” to show options)
-p PATH, –path PATH Folder where file(s) will be downloaded, defaults to current working directory
-w, –wp Download files to current working path
-m, –metadata Generate metadata when pulling kernel
例:

kaggle kernels pull rtatman/list-of-5-day-challenges -p /path/to/dest

檢索內核的輸出

usage: kaggle kernels output [-h] [-p PATH] [-w] [-o] [-q] [kernel]

optional arguments:
-h, –help show this help message and exit
kernel Kernel URL suffix in format / (use “kaggle kernels list” to show options)
-p PATH, –path PATH Folder where file(s) will be downloaded, defaults to current working directory
-w, –wp Download files to current working path
-o, –force Skip check whether local version of file is up to date, force file download
-q, –quiet Suppress printing information about the upload/download progress
例:

kaggle kernels output mrisdal/exploring-survival-on-the-titanic -p /path/to/dest

獲取最新內核運行的狀態

usage: kaggle kernels status [-h] [kernel]

optional arguments:
-h, –help show this help message and exit
kernel Kernel URL suffix in format / (use “kaggle kernels list” to show options)
例:

kaggle kernels status mrisdal/exploring-survival-on-the-titanic

配置

API支持以下配置命令。

usage: kaggle config [-h] {view,set,unset} …

optional arguments:
-h, –help show this help message and exit

commands:
{view,set,unset}
view View current config values
set Set a configuration value
unset Clear a configuration value
查看當前配置值

usage: kaggle config path [-h] [-p PATH]

optional arguments:
-h, –help show this help message and exit
-p PATH, –path PATH folder where file(s) will be downloaded, defaults to ~/.kaggle
例:

kaggle config path -p C:\

查看當前配置值

usage: kaggle config view [-h]

optional arguments:
-h, –help show this help message and exit
例:

kaggle config view

設置配置值

usage: kaggle config set [-h] -n NAME -v VALUE

required arguments:
-n NAME, –name NAME Name of the configuration parameter
(one of competition, path, proxy)
-v VALUE, –value VALUE
Value of the configuration parameter, valid values depending on name
- competition: Competition URL suffix (use “kaggle competitions list” to show options)
- path: Folder where file(s) will be downloaded, defaults to ~/.kaggle
- proxy: Proxy for HTTP requests
例:

kaggle config set -n competition -v titanic

清除配置值

usage: kaggle config unset [-h] -n NAME

required arguments:
-n NAME, –name NAME Name of the configuration parameter
(one of competition, path, proxy)
例:

kaggle config unset -n competition

限制

內核支持是初步的,可能存在一些問題。

執照

Kaggle API是在Apache 2.0許可下發布的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章