2018年1月14日 星期日

Windows 安裝 Python 機器學習工具

這兩天在讀剛從金石堂買來的 "Deep Learning from scratch (歐萊禮出版, 齋藤康毅著)", 對 Python 的興趣又燃起來了. 自從 MicroPython 告一段落後已四個月沒碰 Python 了. 這一個月分心去學太陽光電安裝, 隨著課程進入尾聲, 該回到 Python 機器學習的學習軌道了.


Source : 金石堂


這本書是從歐萊禮日文原文書 "ゼロから作る Deep Learning" 翻譯來的, 非常適合 ML 初學者 (日本人不愧是知識整理的佼佼者), 我覺得是所有 ML 入門書中寫得最好的一本, 我才看一小時就已攻到第三章. 因作者強調要從基礎學起才能真正了解機器學習奧義, 因此整本書只用了 Numpy 與 Matplolib 這兩個套件而已. 此書範例可從 Github 下載 :

# https://github.com/oreilly-japan/deep-learning-from-scratch


昨晚回到鄉下想測試 matplotlib, 發現我的 Win10 竟然沒有安裝 Python 機器學習套件, 於是參考之前在樹莓派安裝的步驟, 在命令列下達五個指令很快即安裝完畢 :
  1. pip3 install numpy (直接安裝 scipy 就會同時安裝 Numpy)
  2. pip3 install scipy
  3. pip3 install matplotlib
  4. pip3 install pandas
  5. pip3 install scikit-learn
參考 :

在 Windows 中安裝 Python 機器學習套件
樹莓派安裝 Python3 scikit-learn 函式庫

另外我在上奇出版的 "科學運算 Python 程式理論與應用" 一書中看到 IPython 的介紹, 這是 Python IDLE 互動式介面的改良版, 支援變數自動補全, 自動縮排, 指令回溯, 並內建許多有用函數與功能. 這麼好我也來安裝看看. 參考 :

Installing IPython

只要用 pip install ipython 即可 :

C:\Users\user>pip3 install ipython   
Collecting ipython
  Downloading ipython-6.2.1-py3-none-any.whl (745kB)
  ......
  ......
Requirement already satisfied: six in c:\python36\lib\site-packages (from traitlets>=4.2->ipython)
Collecting ipython-genutils (from traitlets>=4.2->ipython)
  Downloading ipython_genutils-0.2.0-py2.py3-none-any.whl
Collecting wcwidth (from prompt-toolkit<2 .0.0="" gt="" ipython="" p="">  Downloading wcwidth-0.1.7-py2.py3-none-any.whl
Installing collected packages: pickleshare, simplegeneric, pygments, parso, jedi, decorator, ipython-genutils, traitlets, wcwidth, prompt-toolkit, ipython
  Running setup.py install for simplegeneric ... done
Successfully installed decorator-4.1.2 ipython-6.2.1 ipython-genutils-0.2.0 jedi-0.11.1 parso-0.1.1 pickleshare-0.7.4 prompt-toolkit-1.0.15 pygments-2.2.0 simplegeneric-0.8.1 traitlets-4.3.2 wcwidth-0.1.7

如果被防火牆阻擋無法用 pip install ipython 下載安裝, 可先從 PyPi 網站下載 whl 檔 :

https://pypi.python.org/pypi/ipython

但是 ipython 有下面幾個相依函式庫, 需先下載安裝後才能安裝 ipython :

https://pypi.python.org/pypi/decorator
https://pypi.python.org/pypi/ipython_genutils
https://pypi.python.org/pypi/parso (須比 jedi 先安裝)
https://pypi.python.org/pypi/jedi
# https://pypi.python.org/pypi/pickleshare
https://pypi.python.org/pypi/prompt_toolkit (選 py3)
https://pypi.python.org/pypi/Pygments
https://pypi.python.org/pypi/simplegeneric (ZIP 檔)
https://pypi.python.org/pypi/traitlets
https://pypi.python.org/pypi/wcwidth

安裝過程如下 :

D:\Python>pip3 install decorator-4.2.1-py2.py3-none-any.whl
Processing d:\python\decorator-4.2.1-py2.py3-none-any.whl
Installing collected packages: decorator
Successfully installed decorator-4.2.1

D:\Python>pip3 install ipython_genutils-0.2.0-py2.py3-none-any.whl
Processing d:\python\ipython_genutils-0.2.0-py2.py3-none-any.whl
Installing collected packages: ipython-genutils
Successfully installed ipython-genutils-0.2.0

D:\Python>pip3 install parso-0.1.1-py2.py3-none-any.whl
Processing d:\python\parso-0.1.1-py2.py3-none-any.whl
Installing collected packages: parso
Successfully installed parso-0.1.1

D:\Python>pip3 install jedi-0.11.1-py2.py3-none-any.whl
Processing d:\python\jedi-0.11.1-py2.py3-none-any.whl
Requirement already satisfied: parso==0.1.1 in c:\python36\lib\site-packages (fr
om jedi==0.11.1)
Installing collected packages: jedi
Successfully installed jedi-0.11.1

D:\Python>pip3 install pickleshare-0.7.4-py2.py3-none-any.whl
Processing d:\python\pickleshare-0.7.4-py2.py3-none-any.whl
Installing collected packages: pickleshare
Successfully installed pickleshare-0.7.4

D:\Python>pip3 install prompt_toolkit-1.0.15-py3-none-any.whl
Processing d:\python\prompt_toolkit-1.0.15-py3-none-any.whl
Requirement already satisfied: six>=1.9.0 in c:\python36\lib\site-packages (from
 prompt-toolkit==1.0.15)
Requirement already satisfied: wcwidth in c:\python36\lib\site-packages (from pr
ompt-toolkit==1.0.15)
Installing collected packages: prompt-toolkit
Successfully installed prompt-toolkit-1.0.15

D:\Python>pip3 install Pygments-2.2.0-py2.py3-none-any.whl
Processing d:\python\pygments-2.2.0-py2.py3-none-any.whl
Installing collected packages: Pygments
Successfully installed Pygments-2.2.0

D:\Python>pip3 install simplegeneric-0.8.1.zip
Processing d:\python\simplegeneric-0.8.1.zip
Installing collected packages: simplegeneric
  Running setup.py install for simplegeneric ... done
Successfully installed simplegeneric-0.8.1

D:\Python>pip3 install traitlets-4.3.2-py2.py3-none-any.whl
Processing d:\python\traitlets-4.3.2-py2.py3-none-any.whl
Requirement already satisfied: ipython-genutils in c:\python36\lib\site-packages
 (from traitlets==4.3.2)
Requirement already satisfied: decorator in c:\python36\lib\site-packages (from
traitlets==4.3.2)
Requirement already satisfied: six in c:\python36\lib\site-packages (from traitl
ets==4.3.2)
Installing collected packages: traitlets
Successfully installed traitlets-4.3.2

D:\Python>pip3 install wcwidth-0.1.7-py2.py3-none-any.whl
Processing d:\python\wcwidth-0.1.7-py2.py3-none-any.whl
Installing collected packages: wcwidth
Successfully installed wcwidth-0.1.7

D:\Python>pip3 install ipython-6.2.1-py3-none-any.whl
Processing d:\python\ipython-6.2.1-py3-none-any.whl
Requirement already satisfied: traitlets>=4.2 in c:\python36\lib\site-packages (
from ipython==6.2.1)
Requirement already satisfied: pickleshare in c:\python36\lib\site-packages (fro
m ipython==6.2.1)
Requirement already satisfied: jedi>=0.10 in c:\python36\lib\site-packages (from
 ipython==6.2.1)
Requirement already satisfied: decorator in c:\python36\lib\site-packages (from
ipython==6.2.1)
Requirement already satisfied: pygments in c:\python36\lib\site-packages (from i
python==6.2.1)
Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in c:\python36\lib\s
ite-packages (from ipython==6.2.1)
Requirement already satisfied: simplegeneric>0.8 in c:\python36\lib\site-package
s (from ipython==6.2.1)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python36\
lib\site-packages (from ipython==6.2.1)
Requirement already satisfied: setuptools>=18.5 in c:\python36\lib\site-packages
 (from ipython==6.2.1)
Requirement already satisfied: ipython-genutils in c:\python36\lib\site-packages
 (from traitlets>=4.2->ipython==6.2.1)
Requirement already satisfied: six in c:\python36\lib\site-packages (from traitl
ets>=4.2->ipython==6.2.1)
Requirement already satisfied: parso==0.1.1 in c:\python36\lib\site-packages (fr
om jedi>=0.10->ipython==6.2.1)
Requirement already satisfied: wcwidth in c:\python36\lib\site-packages (from pr
ompt-toolkit<2.0.0,>=1.0.4->ipython==6.2.1)
Installing collected packages: ipython
Successfully installed ipython-6.2.1


在命令列輸入 ipython 即進入互動式介面, 它會對每一次的輸入進行編號, 例如 In[1], In[2], .... 如果忘記冪次函數 pow() 的拼法, 只要輸入 p 再按 tab 鍵, 下方就會列出所有 p 開頭的函數 :

C:\Users\user>ipython 
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

In [1]: print("ok")
ok

In [2]: p     (按 tab 鍵)
          pass                      print()
          PendingDeprecationWarning ProcessLookupError
          PermissionError           property                  >
          pow()                     Pictures/

In [3]: exit      (離開 Ipython)






跳出 Ipython 可輸入 exit 回到命令列,

嗯, 真的比 IDLE 介面好用, 光是指令回溯就可以省掉不少敲指令時間. 接下來就用 ipython 來玩一下 Matplotlib 繪圖功能, 輸入下列指令 :

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0, 2, 0.05)
y=np.sin(2*np.pi*x)
plt.plot(x, y)
plt.show()

結果如下 :

C:\Users\user>ipython
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: x=np.arange(0, 2, 0.05)

In [4]: y=np.sin(2*np.pi*x)

In [5]: plt.plot(x,y)
Out[5]: [<matplotlib.lines.Line2D at 0x2a52305fb38>]

In [6]: plt.show()





圓周率在 Numpy 中為一屬性 Numpy.pi, 注意, plot() 只是將建立的圖存在一個 Line2D 物件而已, 必須呼叫 show() 才會顯示圖形.

2018-02-25 補充 :

在 Python 3.6.4 版直接安裝 Scipy 就會同時安裝 Numpy 了, 因此程序只要 4 個即可 :
  1. pip3 install scipy
  2. pip3 install matplotlib
  3. pip3 install pandas
  4. pip3 install scikit-learn

沒有留言 :