CUBE SUGAR CONTAINER

技術系のこと書きます。

Kaggle をコマンドラインで操作する

最近、データ分析コンペサイトの Kaggle に公式のコマンドラインツールができた。 今回はそれを使ってみる。

環境は次の通り。

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.13.4
BuildVersion:   17E202
$ python -V                
Python 3.6.5

インストール

ツールは Python で書かれているので pip を使ってインストールする。

$ pip install kaggle

設定

ダウンロードしただけではユーザ固有の操作ができない。 なので、まずはその設定から始めよう。

まずはブラウザで自分のアカウントの設定画面を開こう。 <your-username> のところにはユーザ名が入る。

$ open https://www.kaggle.com/<your-username>/account

もちろん公式のトップページから辿っても問題ない。

開いたら、ページ内の API という項目に Create New API Token というボタンがあるのでそれを押す。 すると、自分用の設定ファイル (kaggle.json) がダウンロードされる。

あとはダウンロードした設定ファイルを所定の場所においてパーミッションを自分だけ読み書きできるようにすれば完了。

$ mkdir -p ~/.kaggle
$ mv ~/Downloads/kaggle.json ~/.kaggle
$ chmod 600 ~/.kaggle/kaggle.json

使ってみる

これで kaggle コマンドが使えるようになった。

試しにアクティブなコンペの一覧を表示させてみよう。 これには competitions list サブコマンドを使う。

$ kaggle competitions list
ref                                             deadline             category            reward  teamCount  userHasEntered  
----------------------------------------------  -------------------  ---------------  ---------  ---------  --------------  
imagenet-object-detection-challenge             2029-12-31 07:00:00  Research         Knowledge          0           False  
imagenet-object-detection-from-video-challenge  2029-12-31 07:00:00  Research         Knowledge          0           False  
imagenet-object-localization-challenge          2029-12-31 07:00:00  Research         Knowledge          8           False  
titanic                                         2020-04-07 00:00:00  Getting Started  Knowledge      11370           False  
house-prices-advanced-regression-techniques     2020-03-01 23:59:00  Getting Started  Knowledge       5219           False  
digit-recognizer                                2020-01-07 00:00:00  Getting Started  Knowledge       2347           False  
competitive-data-science-predict-future-sales   2019-01-01 23:59:00  Playground           Kudos        397           False  
trackml-particle-identification                 2018-08-13 23:59:00  Featured           $25,000         36           False  
freesound-audio-tagging                         2018-07-31 23:59:00  Research         Knowledge         92           False  
whale-categorization-playground                 2018-07-09 23:59:00  Playground           Kudos        234           False  
avito-demand-prediction                         2018-06-27 23:59:00  Featured           $25,000        416           False  
cvpr-2018-autonomous-driving                    2018-06-11 23:59:00  Research            $2,500         48           False  
inaturalist-2018                                2018-06-04 23:59:00  Research             Kudos         37           False  
imaterialist-challenge-fashion-2018             2018-05-30 23:59:00  Research            $2,500         92           False  
imaterialist-challenge-furniture-2018           2018-05-30 23:59:00  Research            $2,500        273           False  
landmark-retrieval-challenge                    2018-05-22 23:59:00  Research            $2,500        137           False  
landmark-recognition-challenge                  2018-05-22 23:59:00  Research            $2,500        327           False  
talkingdata-adtracking-fraud-detection          2018-05-07 23:59:00  Featured           $25,000       3777            True  
donorschoose-application-screening              2018-04-25 23:59:00  Playground            Swag        581           False  
data-science-bowl-2018                          2018-04-16 23:59:00  Featured          $100,000       3634           False  

他にも例えば competitions download コマンドを使うと、コンペで使われているデータセットがダウンロードできたりする。 ただし、この操作の前にはあらかじめ Web ブラウザからコンペに参加しておく必要がある。

$ kaggle competitions download -c <competition-name>

その他の使い方については -h オプションをつけてコマンドを実行することで確認できる。

$ kaggle -h                      
usage: kaggle [-h] {competitions,datasets,config} ...

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

commands:
  {competitions,datasets,config}
                        Use one of:
                        competitions {list, files, download, submit, submissions}
                        datasets {list, files, download, create, version, init}
                        config {view, set, unset}
    competitions        Commands related to Kaggle competitions
    datasets            Commands related to Kaggle datasets
    config              Configuration settings

そんなかんじ。