1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】Kaggle APIで環境の準備

Last updated at Posted at 2024-02-24

はじめに

おじさんが前から興味のあるKaggleについて、重い腰を上げてユーザ登録し、さらにエントリーしてデータをダウンロードしてみたので、それをメモしておきます。データはブラウザからZIPファイルをクリックしてダウンロードできるのですが、API経由でも取得できます。

  • APIを使うためのツールはPIPでインストールできる
  • ユーザ認証のためにKAGGLEで発行してもらったキーを指定する

いずれもtutorial に書いてある通りにすればよいです。
よく見たらQiitaでも多くの方が説明してくれていました。

内容

自分のノートPC(Widnows 11/ WSL2)

インストール

下記で問題ありませんでした。

$ poetry add kaggle

API token の設定

profile (https://www.kaggle.com//account) ページを開いて "Create API Token"のボタンを押すと、kaggle.json が自動的にダウンロードされました。中身を見ると、単純にusernameとkey が書かれているだけです。

これを$HOME/.kaggle/kaggle.json に配置するか、あるいは、$KAGGLE_CONFIG_DIR/kaggle.json に配置するように言われています。(KAGGLE_CONFIG_DIRは自分で定義する環境変数です。)自分はKaggle アカウントを切り替えて使うこともないので、~/.kaggle に配置しました。

$ cat ~/.kaggle/kaggle.json
{"username":"x77","key":"xxxx99f3de2d6a8edda504abcde12345"}
$ poetry run kaggle -v
Kaggle API 1.6.6
$ poetry run kaggle competitions list
ref                                                                              deadline             category            reward  teamCount  userHasEntered
-------------------------------------------------------------------------------  -------------------  ---------------  ---------  ---------  --------------
https://www.kaggle.com/competitions/pii-detection-removal-from-educational-data  2024-04-23 23:59:00  Featured           $60,000       1097           False
https://www.kaggle.com/competitions/hms-harmful-brain-activity-classification    2024-04-08 23:59:00  Research           $50,000       1785           False
https://www.kaggle.com/competitions/data-assistants-with-gemma                   2024-04-14 23:59:00  Analytics          $50,000          0           False
https://www.kaggle.com/competitions/playground-series-s4e2                       2024-02-29 23:59:00  Playground            Swag       2711           False
https://www.kaggle.com/competitions/planttraits2024                              2024-06-15 22:00:00  Research         Knowledge         80           False
https://www.kaggle.com/competitions/titanic                                      2030-01-01 00:00:00  Getting Started  Knowledge      15278           False
https://www.kaggle.com/competitions/house-prices-advanced-regression-techniques  2030-01-01 00:00:00  Getting Started  Knowledge       3895           False
https://www.kaggle.com/competitions/spaceship-titanic                            2030-01-01 00:00:00  Getting Started  Knowledge       2659           False
https://www.kaggle.com/competitions/digit-recognizer                             2030-01-01 00:00:00  Getting Started  Knowledge       1609           False
https://www.kaggle.com/competitions/nlp-getting-started                          2030-01-01 00:00:00  Getting Started  Knowledge        801           False
https://www.kaggle.com/competitions/connectx                                     2030-01-01 00:00:00  Getting Started  Knowledge        203           False
https://www.kaggle.com/competitions/tpu-getting-started                          2030-06-03 23:59:00  Getting Started  Knowledge         69           False
https://www.kaggle.com/competitions/store-sales-time-series-forecasting          2030-06-30 23:59:00  Getting Started  Knowledge        703           False
https://www.kaggle.com/competitions/gan-getting-started                          2030-07-01 23:59:00  Getting Started  Knowledge         77           False
https://www.kaggle.com/competitions/contradictory-my-dear-watson                 2030-07-01 23:59:00  Getting Started  Knowledge         42           False
https://www.kaggle.com/competitions/home-credit-credit-risk-model-stability      2024-05-06 23:59:00  Featured          $105,000       1187           False
https://www.kaggle.com/competitions/optiver-trading-at-the-close                 2024-03-20 23:59:00  Featured          $100,000       4436           False
https://www.kaggle.com/competitions/predict-energy-behavior-of-prosumers         2024-04-30 23:59:00  Featured           $50,000       2731           False

データのダウンロード

下記で、zipファイルがダウンロードされました。下記でcompetition の名前はダミーです。

$ kaggle competitions download -c smartphone-xxxxxxxx-2023
$ file *.zip
 smartphone-xxxxxxxx-2023.zip

Google Colab

さきほどの kaggle.json/root/.kaggle/kaggle.json に配置すればOKです。作業をしていてエラーが出たときにヒントとして提案されていたのでそれに従いました。

下記を実行するとファイル選択のボックスが出るので、kaggle.json を指定します。

from google.colab import files
# Upload the Kaggle API key JSON file
uploaded = files.upload()

import sys, os
os.rename("kaggle.json", "/root/.kaggle/kaggle.json")
!chmod 600 /root/.kaggle/kaggle.json
!kaggle competitions download -c competiosn-name

まとめ

特に問題なく、便利なツールが使えるようになりました。こういうの用意してくれる人々の活動は素晴らしい。良い感じだ。まずはおじさんは賞金めざして参加します。
(2024/2/24)

  • Google Colab について追記(2024/2/25)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?