LoginSignup
14
14

More than 5 years have passed since last update.

Mac OS X に IPython (Python2) を導入する方法 〜機械学習理論入門下準備〜

Last updated at Posted at 2015-10-31

先日、ITエンジニアのための機械学習理論入門を購入してみました。

Python サンプルコードの実行環境を準備する必要があるのですが、書籍内では Enthought Canopy を使っての効率的な環境構築が紹介されています。

しかし、なんとなくこういうツールは初めは避けたい派なので、とりあえず自前で必要なライブラリをインストールすることにしました。

対象読者

  • Python2 を既に導入済み
  • Python2 で機械学習関係ライブラリ導入未経験
  • Enthought Canopy は使わず CLI で戦う

必要なライブラリ

必要なライブラリは以下のとおりです。

  • NumPy
  • SciPy
  • matplotlib
  • pandas
  • PIL
  • scikit-learn
  • IPython

簡単に調べた所、PIL は Python2 系でしか使えない気がしたので、Python2 で環境構築をすることにしました。

前準備 (Virtualenv)

IPython を 動かす際に、Virtualenv を入れてないとやたらと警告を発してくるということがわかったので、まずは Virtualenv を入れておきます。

正直、Virtualenv については IPython が発してくる警告で初めて存在を知りました。Python って TopCoder で以外使ったことないんですよ実は。とりあえず、何も考えずに以下の2つの記事をなんとなく読みました。ホントになんとなく。

ま、とりあえず入れていきます。

$ pip install virtualenv
$ pip install virtualenvwrapper

次に、仮想環境用ディレクトリを作成します。名前は何でも良いかなと思ったので、machine_learningという名前にしました。

$ virtualenv --no-site-packages ~/work/machine_learning

仮想環境ができたので、中に入ります。

$ source ~/work/machine_learning

プロンプトが(machine_learning)$みたいになってたら成功です。

以下、仮想環境下で作業を進めます。

ライブラリのインストール

以下のライブラリはpip install numpyのように、ライブラリ名を小文字にして pip でインストールするだけです。

  • NumPy
  • SciPy
  • matplotlib
  • pandas
  • scikit-learn

PIL

ちょっとだけクセモノです、こいつは以下のように叩く必要があります。

$ pip install PIL --allow-external PIL --allow-unverified PIL

IPython を入れていきます

pip で叩けば一応さくっと入ります。

$ pip install ipython

インストール完了です。テストがあるのでテストを叩いてみます。

$ iptest

絶望を感じる程に動きませんでした。

順番に足りないライブラリを入れていきます。

  • requests
  • mock
  • testpath
  • ipykernel

この辺を足していったらテストが通るようになりました。

実は、初めの方に何も考えずに最新じゃなさそうなドキュメントをほとんど読みもせずに勢いで入れていたライブラリもあります。もし、まだテストが通っていないのであれば、入れてみると良いかもしれません。

  • jinja2, needed for the notebook
  • sphinx, needed for nbconvert
  • pyzmq, needed for IPython’s parallel computing features, qt console and notebook
  • pygments, used by nbconvert and the Qt console for syntax highlighting
  • tornado, needed by the web-based notebook
  • nose, used by the test suite
  • readline (on OS X) or pyreadline (on Windows), needed for the terminal

雑にコマンド叩いててすみません。

RuntimeError

テストが通ったので喜んでipythonと叩いて起動してみると、RuntimeError が湯水の如く流れ出します。泣きそうになります。

エラーメッセージを見てみると matplotlib がどうとか、mac os x がどうとか、framework がどうとか書いてあります。ググッて適当に記事を読みます。なるほど。

~/.matplotlib/matplotlibrcbackend: TkAggを指定すれば良さそうです。

~/.matplotlib/matplotlibrc
backend : TkAgg

では、実行!

$ ipython
Python 2.7.9 (default, May  9 2015, 19:43:55)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: TkAgg

In [1]:

動いたよ!やったね!

14
14
1

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
14
14