<目次>
(1) Kaggleのデータセットをダウンロードする方法(API)をご紹介
(1-0) STEP0:(事前準備)Kaggleアカウント作成
(1-1) STEP1:(事前準備)パッケージのインストール
(1-2) STEP2:(事前準備)APIトークンの取得
(1-3) STEP3:サンプルプログラム(データセットダウンロード)
(1) Kaggleのデータセットをダウンロードする方法(API)をご紹介
本記事ではKaggleのデータセットをPythonプログラムからAPI経由でダウンロードする手順についてご紹介します。
(1-0) STEP0:(事前準備)Kaggleアカウント作成




↓



(1-1) STEP1:(事前準備)パッケージのインストール
> pip install kaggle --user

Running setup.py install for kaggle ... done Successfully installed kaggle-1.5.12
(1-2) STEP2:(事前準備)APIトークンの取得
OSError: Could not find kaggle.json. Make sure it's located in C:\Users\Rainbow\.kaggle. Or use the environment method.

●STEP2-1:APIトークンの発行
(図123)

↓

↓

●STEP2-2:kaggle.jsonの配備

(1-3) STEP3:サンプルプログラム(データセットダウンロード)
from kaggle.api.kaggle_api_extended import KaggleApi import zipfile api = KaggleApi() api.authenticate() output_path = './kaggle_download_dataset/' # kaggle.com/c/dogs-vs-catsからダウンロード # train.zip / test1.zipの2つのファイルがある # './'はカレントディレクトリの意味。 api.competition_download_file('sentiment-analysis-on-movie-reviews', 'train.tsv.zip', path=output_path) api.competition_download_file('sentiment-analysis-on-movie-reviews', 'test.tsv.zip', path=output_path) # zipファイルの解凍 with zipfile.ZipFile(output_path+'train.tsv.zip', 'r') as zipref: zipref.extractall(output_path) with zipfile.ZipFile(output_path+'test.tsv.zip', 'r') as zipref: zipref.extractall(output_path)

(結果例)
Downloading train.tsv.zip to ./kaggle_download_dataset 100%|█████████████████████████████████████████████████████████████████████████████████████████| 1.28M/1.28M [00:01<00:00, 1.25MB/s] Downloading test.tsv.zip to ./kaggle_download_dataset 100%|███████████████████████████████████████████████████████████████████████████████████████████| 494k/494k [00:00<00:00, 5.65MB/s]

↓

# tsvファイルの読込み with open(output_path+'train.tsv', encoding='utf-8', newline='') as f: for cols in csv.reader(f, delimiter='\t'): print(cols)

(1-4) エラー対処:HTTP 403エラーが出た時の対処方法について
●エラー
HTTP response body: b'{"code":403,"message":"Permission \\u0027competitions.downloadData\\u0027 was denied"}'

●原因

●対策

