0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kaggleをgoogle colabで実装するためにAPIを利用するまで

Last updated at Posted at 2025-02-23

序論

今日はgoogle colaboでKaggleを行うために学習データをダウンロードするための設定を行います.ほんとはローカルのPCでやりたいけど,まだDockerContainerの設定について理解し切れてない...

理想

コードを,実行したらランタイムのファイル場所に自動的に格納されれるようにしたい.理由としてはドライブに保存するとなると,学習データセットによっては容量が大きすぎて管理できないから.

内容

Step1

Kaggleホームページより.jsonファイルをダウンロード.
アカウントのマイページ設定のところ下の方のcreateAPIをクリック.
image.png

Step2

driveの任意のフォルダに.jsonファイルを置く.
私の場合はColab NotebooksフォルダのKaggleフォルダに置いた.

Step3

コードを記載する.

import os
from google.colab import drive
drive.mount('/content/drive')

!cp "/content/drive/MyDrive/Colab Notebooks/Kaggle/kaggle.json" /content/
!chmod 600 /content/kaggle.json

os.environ['KAGGLE_CONFIG_DIR'] = '/content/'

上記のコードはドライブにマウントして,Step2に置いたファイルを/content/にコピーし,!chmodで権限を与えています.権限に関してはこれがないと,データセットをダウンロードできないです.

Step4

!pip install kaggle
Kaggle用のコマンドを使えるようになるためにインストール

Step5

あとはコンペッションのdatasetのところのAPIを取得して下記を記載して実行するだけ.

# Kaggleのdataset用のapiを設定.ここではタイタニック
!kaggle competitions download -c titanic

下記コードは別で実行..zipファイルがランタイムにないと実行エラーとなっていしまいます.

# 以下でファイルを解凍
%%bash
dir
for file in *.zip; do 
    folder="${file%.zip}"
    mkdir -p "$folder"
    unzip -o "$file" -d "$folder"
done
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?