テンプレートを使って爆速で機械学習プロジェクトを作成する

  • 16
    いいね
  • 0
    コメント

あなたは機械学習のプロジェクトを毎回違う構成で作っていませんか?
何をどこに配置するかで悩んで時間がかかっていませんか?

そんな方に朗報です。
機械学習のプロジェクトはコマンド一発で作れます。
以下のようなプロジェクトを数秒で作れます。

ディレクトリ構造

├── LICENSE
├── Makefile           <- Makefile with commands like `make data` or `make train`
├── README.md          <- The top-level README for developers using this project.
├── data
│   ├── external       <- Data from third party sources.
│   ├── interim        <- Intermediate data that has been transformed.
│   ├── processed      <- The final, canonical data sets for modeling.
│   └── raw            <- The original, immutable data dump.

├── docs               <- A default Sphinx project; see sphinx-doc.org for details

├── models             <- Trained and serialized models, model predictions, or model summaries

├── notebooks          <- Jupyter notebooks. Naming convention is a number (for ordering),
│                         the creator's initials, and a short `-` delimited description, e.g.
│                         `1.0-jqp-initial-data-exploration`.

├── references         <- Data dictionaries, manuals, and all other explanatory materials.

├── reports            <- Generated analysis as HTML, PDF, LaTeX, etc.
│   └── figures        <- Generated graphics and figures to be used in reporting

├── requirements.txt   <- The requirements file for reproducing the analysis environment, e.g.
│                         generated with `pip freeze > requirements.txt`

├── src                <- Source code for use in this project.
│   ├── __init__.py    <- Makes src a Python module
│   │
│   ├── data           <- Scripts to download or generate data
│   │   └── make_dataset.py
│   │
│   ├── features       <- Scripts to turn raw data into features for modeling
│   │   └── build_features.py
│   │
│   ├── models         <- Scripts to train models and then use trained models to make
│   │   │                 predictions
│   │   ├── predict_model.py
│   │   └── train_model.py
│   │
│   └── visualization  <- Scripts to create exploratory and results oriented visualizations
│       └── visualize.py

└── tox.ini            <- tox file with settings for running tox; see tox.testrun.org

このような標準的なプロジェクト構成を使うことにはいくつかのメリットがあります。新しい人にとっては何がどこにあるのかわかるので素早くJoinできます。また自分にとっても、数ヶ月後にプロジェクトを見直した時にどこに何があるかで悩まずに済むので素早く仕事に取り組めます。

このようなプロジェクト構成は Django や Rails のプロジェクトを作る時のように簡単に作ることができます。
この記事ではそのための方法を紹介します。

Getting started

まずはじめにディレクトリ構成を作るためのライブラリである Cookiecutter をインストールします。そのあと実際にプロジェクトを作ってみます。

Cookiecutterのインストール

はじめに Cookiecutter について説明します。

Cookiecutterというのはプロジェクトのテンプレートからプロジェクトを作るためのPythonライブラリです。Cookiecutterを使うことで既存のプロジェクトテンプレートから簡単にプロジェクトを作ることができます。今回は機械学習用のテンプレートを使用しますが、作成したいプロジェクトに応じてテンプレートを選択できます。

Cookiecutter のインストールは以下のように pip を使って行えます:

$ pip install cookiecutter

インストールが終了したら実際にプロジェクトを作ってみましょう。

プロジェクトの作成

新しいプロジェクトを作るためにインストールした Cookiecutter を使ってコマンドを叩きます。その際、コマンドの引数として既存のプロジェクトテンプレートを指定してあげる必要があります。今回は機械学習用のテンプレートである Cookiecutter Data Science を指定します。以下のコマンドを実行してみましょう。

$ cookiecutter https://github.com/drivendata/cookiecutter-data-science

上記のコマンドを実行後、プロジェクト名や作成者の名前を聞かれるので答えてあげます。すべての質問に答えると新しいプロジェクトが出来上がります。

project_name [project_name]: machine-learning
repo_name [machine-learning]: 
author_name [Your name (or your organization/company/team)]: Hironsan
description [A short description of the project.]: Machine learning project
Select open_source_license:
1 - MIT
2 - BSD
3 - Not open source
Choose from 1, 2, 3 [1]: 1
s3_bucket [[OPTIONAL] your-bucket-for-syncing-data (do not include 's3://')]: 
Select python_interpreter:
1 - python
2 - python3
Choose from 1, 2 [1]: 2

おわりに

プロジェクト構成を決めるのは意外と手間がかかる仕事です。この記事が皆様がプロジェクト作成する際に役立てば幸いです。

私のアカウントでも機械学習や自然言語処理に関する情報をつぶやいているので、フォローお待ちしています。
@Hironsan

References