科学技術計算やデータ分析、機械学習の分野では必須のライブラリ「NumPy」の環境構築を解説します。

Pythonの環境構築にはpipと呼ばれるパッケージ管理システムがよく利用されます。この記事では、pipを使った方法と使わない方法の両方を解説しています。

NumPyとは

NumPyは、Pythonの数値計算のためのモジュールで、高速に数値計算ができることが特徴です。

NumPyで使われる主なクラスはnp.ndarrayと呼ばれる多次元を扱う配列です。NumPy配列は、公式ドキュメントでは単に配列と称されることが多いです。

Pythonは動的型付き言語(データ型が柔軟に帰ることができる)で、柔軟に素早く書くことができる言語として知られていますが、その一方で処理速度がJavaやCに比べて一般に遅いというデメリットがあります。そこで、CやFortranで書かれた型を固定して演算を行うことができるコードをPythonから呼び出すことを出来るようにしたライブラリがNumPyです。NumPyを導入することで高速な数値演算ができます。

このNumPyの特殊なクラスnp.ndarrayは最初は慣れない概念かと思います。Pythonのリストで扱わせて欲しいという初学者の意見も理解できますが、最初のうちはndarrayクラスは型や整列のされ方が統一されている数値(文字列やオブジェクトも可)の容れ物であるという程度の理解で問題ありません。

詳しくは以下の記事を参考にしてください。

NumPyの多次元配列データ構造ndarrayの基礎 /features/numpy-ndarray.html

このライブラリがあることで、コンピュータサイエンスの分野でPythonが頻繁に使われるようになっていると言っても過言ではありません。

NumPyのインストール

pipを使ったインストール方法

NumPyのインストールの方法は単純です。以下のコマンドを打ち込むだけで終了です。

$ pip install numpy

あとは勝手にインストールしてくれます。

pipが使えない時

pipコマンドは

  • Python2.7.9以降
  • Python3.4以降

のバージョンにはデフォルトで付属しているパッケージ管理システムですが、もし使えない場合はpipをインストールするとNumPyのインストールだけでなく、他のパッケージも気軽にインストールすることができます。

$ sudo easy_install pip
Password:
Searching for pip
Best match: pip 9.0.1
Adding pip 9.0.1 to easy-install.pth file
Installing pip3.5 script to /Users/(ユーザー名)/.pyenv/versions/3.5.3/bin
Installing pip3 script to /Users/(ユーザー名)/.pyenv/versions/3.5.3/bin
Installing pip script to /Users/(ユーザー名)/.pyenv/versions/3.5.3/bin

Using /Users/(ユーザー名)/.pyenv/versions/3.5.3/lib/python3.5/site-packages
Processing dependencies for pip
Finished processing dependencies for pip

pipでインストールできているか確かめます。

$ pip

Usage:   
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be
                              used up to 3 times (corresponding to WARNING,
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host as trusted, even though it does
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.

このような表示が出てきたら、正しくインストールできていることになります。

この他にpyenvを利用することで、Pythonのバージョンやプロジェクト毎パッケージを管理することができるので、そちらも興味があれば調べてみてください。

pipを使わない方法

Ubuntu や Debianの場合、以下のコードでインストールできます。

sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose

Macなら、Homebrewを使ってインストール可能です。以下のコマンドだとNumPy以外にもSciPyやmatplotlibもインストールしています。

brew tap homebrew/science && brew install python numpy scipy matplotlib

その他の方法も公式サイトで扱われています。