2017年4月6日 星期四

在 Windows 中安裝 Python 機器學習套件

今天將辦公室的 Win7 電腦中較舊的 Python 3.5 移除了, 改下載安裝最新版的 Python 3.6.1, 然後試試看在 Windows 下可否像樹莓派那樣安裝機器學習函式庫 scikit-learn 以及相關套件. 結論是可以的, 而且比在樹莓派上安裝速度還快, 參考 :

# 樹莓派安裝 Python3 scikit-learn 函式庫
# Installing scikit-learn

Python 3.6.1 的安裝過程如下 (Python for Windows 下載), 注意, 機器學習實驗應安裝 64-bit 為宜, 否則無法安裝 TensorFlow, Keras 等框架 :





主要就是安裝時選擇 Customized 安裝, Optional Features 全部勾選, Advanced Options 除底下兩個 Debugging 不選外都勾選, 並將安裝路徑修改為 C:\Python36, 這樣方便尋找路徑, 詳細參考 :

# Python 學習筆記 : 安裝執行環境與 IDLE 基本操作

在 Windows 上安裝 Python 3 後, 內建套件只有 pip 與 setuptools 而已 :

C:\Users\tony>pip list
DEPRECATION: The default format will switch to columns in the future. You can us
e --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.con
f under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (28.8.0)

以下就以 pip 來安裝機器學習所需之套件. 因為 scikit-learn 套件主要是建立在 numpy 與 scipy 套件的基礎上, 所以我們要依序先安裝 numpy, 再安裝 scipy, 然後是繪圖的 matplotlib, 以及處理表格的 pandas, 最後是安裝 scikit-learn.

我以為只要依序輸入下列指令即可 :
  1. pip3 install numpy
  2. pip3 install scipy
  3. pip3 install matplotlib
  4. pip3 install pandas
  5. pip3 install scikit-learn -U
安裝 numpy 沒問題, 但是安裝 scipy 時卻出現錯誤 :

C:\Users\tony>pip3 install scipy
Collecting scipy
  Downloading scipy-0.19.0.zip (15.3MB)
Installing collected packages: scipy
  Running setup.py install for scipy ... error
    lapack_opt_info:
    lapack_mkl_info:
      libraries mkl_rt not found in ['c:\\python35\\lib', 'C:\\', 'c:\\python35\
\libs']
      NOT AVAILABLE

    openblas_lapack_info:
      libraries openblas not found in ['c:\\python35\\lib', 'C:\\', 'c:\\python3
5\\libs']
      NOT AVAILABLE

    atlas_3_10_threads_info:
    Setting PTATLAS=ATLAS

似乎缺了甚麼函式庫的樣子, 這種錯誤訊息有看沒有懂, 一點建設性都沒有, 還是谷歌大神好用, 我在下面這篇文章中找到了一些線索 :

# Trouble installing SciPy on windows

編號 12 的回答提到安裝 Scipy 需要 FORTRAN 編譯器, 建議先去安裝 MinGW 編譯器套件 :

"To install SciPy on Windows you have to have a fortran compiler installed. The SciPy project recommends MinGW."

還要安裝 Fortran 編譯器? 感覺好麻煩, 所幸往下看到編號 6 的回覆所提供的辦法比較簡單 :


內容如下 :

"Try installing using Scipy wheel file. Download it from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
Make sure to download the one that's compatible with your Python version and your laptop bit. Then install it like this: pip install "path\to\your\wheel\file\scipy-0.18.1-cp27-cp27m-win_amd64.whl""

此解決方案建議到下列網站去下載 Python wheel 檔來用 pip 安裝比較快, 因為這些 whl 檔都是已編譯好的可執行檔.

http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy

此網站提供各種 Python 套件的 wheel 安裝檔, 在 scipy 與 scikit-learn 項目下有特別提到這兩個套件需要 numpy 與 mkl 這兩個套件, 意味著只用 pip3 install numpy 是不夠的, 還需要安裝 mkl 函式庫. MKL 是 INTEL 公司開發的數學核心函式庫 (Math Kernel Library), 參見 :

# Numpy/Scipy with Intel® MKL and Intel® Compilers

而在上面 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 網站上就有現成的 numpy+mkl 套件可下載, 其他像 matplotlib, scipy, pandas, scikit-learn 也都有, 但我只下載了 numpy+mkl, scipy, 與 scikit-learn 這三個 wheel 檔來安裝 (我的電腦是 Win7 64 位元, 所以下載 amd64 的檔案), pandas 與 matplotlib 直接安裝即可.




安裝指令如下 :
  1. pip3 install d:\numpy-1.12.1+mkl-cp36-cp36m-win_amd64.whl
  2. pip3 install d:\scipy-0.19.0-cp36-cp36m-win_amd64.whl
  3. pip3 install matplotlib (也可使用 wheel 檔)
  4. pip3 install pandas (也可使用 wheel 檔)
  5. pip3 install d:\scikit_learn-0.18.1-cp36-cp36m-win_amd64.whl
此外為了使用 Python 建置網站, 我也安裝了虛擬環境 virtualenv, Django, requests, BeautifulSoup4 等套件 :
  1. pip3 install virtualenv
  2. pip3 install "django<1.9"
  3. pip3 install requests
  4. pip3 install BeautifulSoup4
注意喔, Django 1.9 (含) 以上在處理中文部分還有些問題, 因此先安裝 1.8 版來用. 這種限制版本的 pip 指令, 最後的參數必須用單或雙引號括起來.

安裝完畢用 pip list 指令檢視套件清單 :

C:\Users\tony>pip list
DEPRECATION: The default format will switch to columns in the future. You can us
e --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.con
f under the [list] section) to disable this warning.
beautifulsoup4 (4.5.3)
cycler (0.10.0)
Django (1.8.18)
matplotlib (2.0.0)
numpy (1.12.1)
pandas (0.19.2)
pip (9.0.1)
pyparsing (2.2.0)
python-dateutil (2.6.0)
pytz (2017.2)
scikit-learn (0.18.1)
scipy (0.19.0)
setuptools (28.8.0)
six (1.10.0)
virtualenv (15.1.0)

這樣就大功告成了.

參考 :

# How to install numpy+mkl for python 2.7 on windows 64 bit?

沒有留言 :