7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで自然言語処理を行うための環境構築

Last updated at Posted at 2020-01-13

自分用のメモですが、ご参考になれば。

1. 環境構築後のバージョン

  • Python 3.6.5
  • Homebrew 2.2.2
  • pyenv 1.2.13
  • Numpy 1.15.0
  • SciPy 1.1.0
  • Pandas 0.23.3
  • Scikit-learn 0.19.2
  • MeCab-python3 0.996.2
  • neologdn 0.3.2
  • TensorFlow 1.14.0
  • Keras 2.1.6
  • Gensim 3.5.0
  • Hyperopt 0.1.1
  • Matplotlib 3.1.2

2-1. 【Macの場合】 HomebrewによるPythonのインストール

「Homebrew」をインストール
  ↓
「Homebrew」を用いて「pyenv」をインストール
  ↓
「pyenv」を用いて「Python」をインストール

(1) Homebrewのインストール

Homebrewの公式ドキュメント:https://brew.sh/index_ja

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

途中でreturn/enterキーの押下を求められるので、指示に従って押下。
また、途中でPCにログインする際のPWの入力も求められる。指示に従ってパスワードを入力し(ただし、画面上は入力内容が表示されない)、return/enterキーを押下。

インストール完了後、下記コマンドでバージョンを確認。

$ brew -v
Homebrew 2.2.2
Homebrew/homebrew-core (git revision d7286b; last commit 2020-01-01)

(2) pyenvのインストール

Homebrewを用いてpyenv(同じ端末内で複数バージョンのPython環境を構築・管理するためのツール)をインストール。
pyenvのGitHubリポジトリ:https://github.com/pyenv

$ brew install pyenv

インストール完了後、下記コマンドでバージョンを確認。

$ pyenv -v
pyenv 1.2.13

pyenvの設定:.bash_profileに設定用のコードを追加。
以下のコマンドを一行ずつ実行(何も起きていないように見えるが、それでOK)。

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

(3) Pythonのインストール

pyenvを用いてPythonをインストールする。

まず、下記コマンドを実行してpyenvでインストール可能なPythonのバージョンを確認する。

$ pyenv install --list
# 実行結果のうち、先頭にアルファベットのついていないものがインストール可能なPythonのバージョン
 2.1.3
 2.2.3
 2.3.7
 ・・・
 3.6.5
 3.6.6
 3.6.7
 ・・・

このうち、比較的新しいPython3.6.5をインストール。

$ pyenv install 3.6.5

インストール完了後、下記コマンドでインストールされたPythonのバージョンを確認。

$ pyenv versions
system
* 3.6.5 (set by /Users/hoge/.pyenv/version)

以下のコマンドを実行し、上記でインストールしたバージョンのPythonを使用するように設定を変更(この処理をしないと、Macに標準でインストールされているバージョンのPythonが継続使用される)。

$ pyenv global 3.6.5

下記コマンドを実行して、使用中のPythonのバージョンを確認。

$ Python --version
Python 3.6.5

2-2. 【Windowsの場合】 公式サイトからPythonをインストール

下記サイトからRelease version = Python 3.6.5を選択してダウンロードし、指示に従ってインストール。

Python3.6.5のダウンロードページ(公式):https://www.python.org/downloads/release/python-365/

ダウンロードするファイルは、Windows用であればどれでもよいと思われるが、特にこだわりがなければWindows x86-64 executable installerを選択。

ダウンロードしたファイルを開くと、表示された画面の一番下にAdd Python 3.6 to PATHというチェックボックスがある。これをチェックしてInstall Nowをクリック。

インストール完了後、コマンドプロンプト(いわゆる黒画面)で下記コマンドを実行し、バージョンを確認。

$ Python --version
Python 3.6.5

※ 「‘python’ は . . . 認識されていません。」と表示される場合、Pythonをインストールする際にAdd Python 3.6 to PATHにチェックを入れ忘れた可能性がある。この場合は最初にダウンロードしたファイルを再度開き、Uninstallをクリックしていったん削除し、再度インストールする。

※ Visual Studio Codeからコマンドプロンプトを使用する方法
Terminal > Integrated > Automation Shell: Windowssettings.jsonに以下の一行を追加。

settings.json
{
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe"
}

3. 必要なライブラリのインストール【Mac/windows共通】

(1) Numpyのインストール

$ pip install numpy===1.15.0

(2) SciPyのインストール

$ pip install scipy===1.1.0

(3) Pandasのインストール

$ pip install pandas===0.23.3

(4) Scikit-Learnのインストール

$ pip install scikit-learn===0.19.2

(5) MeCabのインストール

$ pip install mecab-python3===0.996.2

※Windowsの場合
参考:https://qiita.com/menon/items/f041b7c46543f38f78f7

$ pip install ipykernel
$ pip install mecab-python-windows

(6) neplogdnのインストール

$ pip install neologdn===0.3.2
$ git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git
$ cd mecab-ipadic-neologd
$ ./bin/install-mecab-ipadic-neologd -n -a

[install-mecab-ipadic-NEologd] : Start..
[install-mecab-ipadic-NEologd] : Check the existance of libraries
・・・
[test-mecab-ipadic-NEologd] : Please check difference between default system dictionary and mecab-ipadic-NEologd

default system dictionary        |     mecab-ipadic-NEologd
佐野 史郎                         |     佐野史郎 
警察 学校                         |     警察学校 
みぞ れ                           |     みぞれ 
ころ ん                           |     ころん 
たら こ ー ち                      |    たら こーち 
一 乗 谷 城                      |     一乗谷城 
ら じ ら ー                        |    らじらー 
林 遣 都                          |    林遣都 
マツコ 会議                        |    マツコ会議 
アド 街                            |     アド街 

[test-mecab-ipadic-NEologd] : Finish..
[install-mecab-ipadic-NEologd] : Please check the list of differences in the upper part.
[install-mecab-ipadic-NEologd] : Do you want to install mecab-ipadic-NEologd? Type yes or no.
yes # ここで「yesを入力」
[install-mecab-ipadic-NEologd] : OK. Let's install mecab-ipadic-NEologd.
[install-mecab-ipadic-NEologd] : Start..
[install-mecab-ipadic-NEologd] : /usr/local/lib/mecab/dic is current user's directory
・・・
[install-mecab-ipadic-NEologd] : Install completed.
[install-mecab-ipadic-NEologd] : When you use MeCab, you can set '/usr/local/lib/mecab/dic/mecab-ipadic-neologd' as a value of '-d' option of MeCab.
[install-mecab-ipadic-NEologd] : Usage of mecab-ipadic-NEologd is here.
Usage:
    $ mecab -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd ...

[install-mecab-ipadic-NEologd] : Finish..
[install-mecab-ipadic-NEologd] : Finish..

# 以上でインストール完了。以下のコマンドでmecabで使用する辞書にNEologdを指定
$ mecab -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd
近年は自然言語処理の研究が盛んです
近年    名詞,副詞可能,*,*,*,*,近年,キンネン,キンネン
は      助詞,係助詞,*,*,*,*,は,ハ,ワ
自然言語処理    名詞,固有名詞,一般,*,*,*,自然言語処理,シゼンゲンゴショリ,シゼンゲンゴショリ
の      助詞,連体化,*,*,*,*,の,ノ,ノ
研究    名詞,サ変接続,*,*,*,*,研究,ケンキュウ,ケンキュー
が      助詞,格助詞,一般,*,*,*,が,ガ,ガ
盛ん    名詞,形容動詞語幹,*,*,*,*,盛ん,サカン,サカン
です    助動詞,*,*,*,特殊・デス,基本形,です,デス,デス
EOS
# デフォルトのIPAdicでは「自然」「言語」「処理」の3語に分かれてしまうが、NEologdは一語で処理。
# Pythonのコードから利用する場合は、MeCab.Tagger()の引数に'-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd'を指定。

※Windowsの場合
参考:https://www.pytry3g.com/entry/MeCab-NEologd-Windows

(7) TensorFlowのインストール

$ pip install tensorflow===1.14.0

(8) Kerasのインストール

$ pip install Keras===2.1.6

(9) Gensimのインストール

$ pip install gensim===3.5.0

(10) Hyperoptのインストール

$ pip install hyperopt===0.1.1

(11) Matplotlibのインストール

$ pip install matplotlib===3.1.2

インストール済みのライブラリを確認

以下のコマンドで、インストール済みのライブラリとそのバージョンを確認。

$ pip list

捕捉: pipのSSLエラー

pipコマンドで各ライブラリをインストールしようとする際、以下のようなエラーメッセージが出ることがある。

# pip3 install hoge
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting hoge
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/hoge/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/hoge/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/hoge/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/hoge/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/hoge/
  Could not fetch URL https://hoge/hoge/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='hoge', port=hoge): Max retries exceeded with url: /hoge/hoge/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
  Could not find a version that satisfies the requirement hoge (from versions: )
No matching distribution found for hoge

これは、opensslがインストールされていないことが原因。
この場合、以下のようにopensslをインストールした上で再度Pythonをインストールするとうまくいく(Macの場合のみ。Windowsは未確認)。

# 1. opensslをインストール。
$ brew install openssl

# 2. 優先適用されるSSLをLibreSSLから上記でインストールしたOpenSSLに変更。
$ echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

# 3. ターミナルを一旦終了し再起動する。その後、バージョンを確認しOpneSSLが表示されたらOK。
$ openssl version
OpenSSL 1.1.1d  10 Sep 2019

# 4. Pythonを再インストールする。
$ pyenv install 3.6.5
7
4
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?