Mojave に機械学習とデータ解析の anaconda 環境を構築
Macbook Air を購入したので、何もインストールされていない真っさらな状態から、機械学習とデータ解析の環境を構築しました。 日々構築方法が様々に洗練され、前回のやり方が通用しない昨今ですが、今回は anaconda で全て構築しました。
brew
- App Storeに行ってXcodeを探しインストールをします。
xcode-select --install
- Xcodeを一度起動してライセンスに同意しておく
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
でチェック
emacs
以下を参考にしました。 https://qiita.com/kokorinosoba/items/ecceaabe07d91c6f2c66
$ brew cask install emacs
Ricty
emacs では Ricty をフォントとして使っているので、そのインストールです。 https://qiita.com/segur/items/50ae2697212a7bdb7c7f
brew tap sanemat/font
brew install ricty
- 以下のファイルをコピーし、フォントキャッシュを更新します。
To install Ricty: $ cp -f /usr/local/opt/ricty/share/fonts/Ricty*.ttf ~/Library/Fonts/ $ fc-cache -vf
Anaconda
anaconda で全部構築する
Anaconda インストール
https://www.anaconda.com/distribution/ で Python 3.7 version
Anaconda3-2019.03-MacOSX-x86_64.pkg
Anaconda 仮想環境構築
# tensorflow が python3.6 環境なので 3.6 で構築します。 $ conda create -n py36_base python=3.6 $ conda info -e # conda environments: # base * /anaconda3 py36_base /anaconda3/envs/py36_base
Anaconda Navigator でやってもスムーズです。
tensorflow/keras/sklearn のインストール
conda で全部いれてしまいます。
# 環境をアクティベートする $ source activate py36_base $ conda install tensorflow $ conda install keras $ conda install scikit-learn $ conda install matplotlib $ conda install seaborn $ conda list matplotlib 3.0.3 py36h54f8f79_0 ... numpy 1.16.3 py36hacdab7b_0 numpy-base 1.16.3 py36h6575580_0 ... pandas 0.24.2 py36h0a44026_0 ... scikit-image 0.15.0 py36h0a44026_0 scikit-learn 0.20.3 py36h27c97d8_0 scipy 1.2.1 py36h1410ff5_0 seaborn 0.9.0 py36_0 ... tensorboard 1.13.1 py36haf313ee_0 tensorflow 1.13.1 mkl_py36haf07a9b_0 tensorflow-base 1.13.1 mkl_py36hc36dc97_0 tensorflow-estimator 1.13.0 py_0
jupyter, jupyterlab, orange, glueviz, も navigator から入れます。
環境設定
bash
.bash_profile に anaconda インストーラによってスニペットが挿入されているので、その後にデフォルト起動スクリプトを挿入します。
# added by Anaconda3 2019.03 installer # >>> conda init >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$(CONDA_REPORT_ERRORS=false '/anaconda3/bin/conda' shell.bash hook 2> /dev/null)" if [ $? -eq 0 ]; then \eval "$__conda_setup" else if [ -f "/anaconda3/etc/profile.d/conda.sh" ]; then . "/anaconda3/etc/profile.d/conda.sh" CONDA_CHANGEPS1=false conda activate base else \export PATH="/anaconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda init <<< # 追加:デフォルトの環境を起動します。 conda activate p36_base
matplotlib
matplotlib の import 時にエラーが発生した場合、バックエンドを変更します。
# /anaconda3/envs/py36_base//lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc #backend : macosx backend : TkAgg
動作確認
以下のサンプルを実行できることを確認します。
sklearn + matplotlib https://scikit-learn.org/stable/auto_examples/plot_multilabel.html#sphx-glr-auto-examples-plot-multilabel-py
tensorflow, keras
import os import tensorflow as tf os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello))