2017年3月22日 星期三

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

最近在樹莓派運算平台的使用上日漸上手之後開始研究機器學習, 這是人工智慧最重要的分支, 相關的工具與技術已日趨成熟 (但仍在高速發展中), 也早已實作於例如資訊安全, 資料採礦, 機器人, 自駕車, 無人機, 智慧家居, 語音識別, 影像識別, 甚至財務工程等許多大數據應用中. 因為這門資料科學牽涉機率與統計, 微積分, 線性代數等數學, 而我的數學底子淺, 沒辦法從理論著手, 所以只能從實作切入後再徐圖之.

目前機器學習主流的程式工具是 Python 與 R 語言, 現在是 R 語言佔上風, 但我覺得 Python 潛力較大, 因為它的應用範圍比 R 要廣多了. 由於樹莓派是可 24 小時開機的低功耗 (約 1.5W, Pi 3 約 3W, A+ 最低僅 0.85W) 嵌入式運算器, 很適合作為不眠不休的資料採礦機器, 所以我選擇樹莓派 Type B 跑 Raspbian 作業系統作為測試環境, 除了之前已安裝的 R 語言外, 還需要安裝 Python 的機器學習函式庫 Scikit-learn. 我的最終目的是要以樹莓派作為物聯網的運算平台, 利用機器學習為物聯網設備裝上智慧的大腦.

Python 部分我主要參考下面這幾本書 :

Python Machine Learning (Sebastian Raschka, 2015)
# Introduction to Machine Learning with Python (Andreas C. Müller & Sarah Guido, 2016)
Learning scikit-learn Machine Learning in Python (Raúl Garreta; Guillermo Moncecchi, PACKT 2013 )

Python 的機器學習函式庫 scikit-learn 是建立在 numpy 與 scipy 這兩個函式庫上, 因此必須先安裝這兩個函式庫後才能安裝 scikit-learn. 其次為了要將資料視覺化 (Visualization) 以繪製各種統計圖形, 也需要安裝 matplotlib 函式庫; 另外還要安裝可處理 Table 的 Pandas 函式庫. 書裡面也建議安裝 Jupyter (又稱為 IPython Notebook) 互動式執行環境來取代 Python IDLE, 但我的樹莓派 Type B 跑瀏覽器效能差強人意, 所以不一定要裝 (若是 Pi 2 或 Pi 3 應該沒問題). 以上與 scikit-learn 套件相關的函式庫表列如下 :

 函式庫 用途說明
 numpy Python 基本科學計算套件 (線性代數, 多維矩陣, 傅立葉轉換, 偽隨機數產生等)
 scipy Python 科學計算套件集 (高等線性代數, 函數優化, 信號處理, 統計分布等)
 matplotlib Python 科學繪圖套件 (各式統計圖表)
 Jupyter Python 網頁式互動執行環境 (IPython Notebook)
 pandas Python 資料角力套件 (處理 Table 資料)

以下紀錄這兩天安裝上列函式庫的過程, 並參考 "Introduction to Machine Learning with Python" 這本書第一章的範例程式測試所安裝的函式庫是否正常運作. 上列函式庫都使用 apt-get 指令安裝, 唯有 scikit-learn 是使用 Python 3 版的 pip3 安裝 :
  1. sudo apt-get install python3-numpy
  2. sudo apt-get install python3-scipy
  3. sudo apt-get install python3-matplotlib
  4. sudo apt-get install python3-pandas
  5. sudo pip3 install scikit-learn -U
1. 安裝 numpy :

pi@raspberrypi:~ $ sudo apt-get install python3-numpy
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-numpy is already the newest version.
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.

因為此 Raspbian 為 2017 年 Jessie(pixel), 已經內建最新的 Numpy, 所以不用再安裝.

測試程式 : numpy_test.py
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
print("x:\n{}".format(x))

執行結果 :

$ numpy_test.py
x:
[[1 2 3]
 [4 5 6]]


2. 安裝 scipy :

pi@raspberrypi:~ $ sudo apt-get install python3-scipy
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  python3-decorator
The following NEW packages will be installed:
  python3-decorator python3-scipy
0 upgraded, 2 newly installed, 0 to remove and 17 not upgraded.
Need to get 7,014 kB of archives.
After this operation, 24.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
WARNING: The following packages cannot be authenticated!
  python3-decorator python3-scipy
Install these packages without verification? [y/N] y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-decorator all 3.4.0-2 [22.5 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-scipy armhf 0.14.0-2 [6,992 kB]
Fetched 7,014 kB in 34s (202 kB/s)
Selecting previously unselected package python3-decorator.
(Reading database ... 134211 files and directories currently installed.)
Preparing to unpack .../python3-decorator_3.4.0-2_all.deb ...
Unpacking python3-decorator (3.4.0-2) ...
Selecting previously unselected package python3-scipy.
Preparing to unpack .../python3-scipy_0.14.0-2_armhf.deb ...
Unpacking python3-scipy (0.14.0-2) ...
Setting up python3-decorator (3.4.0-2) ...
Setting up python3-scipy (0.14.0-2) ...

測試程式 : scipy_test.py
import numpy as np
from scipy import sparse
# Create a 2D NumPy array with a diagonal of ones, and zeros everywhere else
eye = np.eye(4)
print("NumPy array:\n{}".format(eye))

執行結果 :
$python scipy_test.py
NumPy array:
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

3. 安裝 matplotlib : 

pi@raspberrypi:~ $ sudo apt-get install python3-matplotlib
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  fonts-lyx libjs-jquery-ui python-matplotlib-data python3-dateutil python3-nose python3-pyparsing python3-tz
Suggested packages:
  libjs-jquery-ui-docs dvipng ghostscript inkscape ipython3 python-matplotlib-doc python3-cairocffi python3-gi python3-gi-cairo
  python3-gobject python3-pyqt4 python3-sip python3-tornado texlive-extra-utils texlive-latex-extra ttf-staypuft python-nose-doc
The following NEW packages will be installed:
  fonts-lyx libjs-jquery-ui python-matplotlib-data python3-dateutil python3-matplotlib python3-nose python3-pyparsing python3-tz
0 upgraded, 8 newly installed, 0 to remove and 17 not upgraded.
Need to get 7,601 kB of archives.
After this operation, 22.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
WARNING: The following packages cannot be authenticated!
  fonts-lyx libjs-jquery-ui python-matplotlib-data python3-dateutil python3-pyparsing python3-tz python3-nose python3-matplotlib
Install these packages without verification? [y/N] y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main fonts-lyx all 2.1.2-2 [176 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjs-jquery-ui all 1.10.1+dfsg-1 [499 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-matplotlib-data all 1.4.2-3.1 [3,041 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-dateutil all 2.2-2 [33.2 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-pyparsing all 2.0.3+dfsg1-1 [64.1 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-tz all 2012c+dfsg-0.1 [25.4 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-nose all 1.3.4-1 [131 kB]
Get:8 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-matplotlib armhf 1.4.2-3.1 [3,631 kB]
Fetched 7,601 kB in 15s (500 kB/s)
Selecting previously unselected package fonts-lyx.
(Reading database ... 135005 files and directories currently installed.)
Preparing to unpack .../fonts-lyx_2.1.2-2_all.deb ...
Unpacking fonts-lyx (2.1.2-2) ...
Selecting previously unselected package libjs-jquery-ui.
Preparing to unpack .../libjs-jquery-ui_1.10.1+dfsg-1_all.deb ...
Unpacking libjs-jquery-ui (1.10.1+dfsg-1) ...
Selecting previously unselected package python-matplotlib-data.
Preparing to unpack .../python-matplotlib-data_1.4.2-3.1_all.deb ...
Unpacking python-matplotlib-data (1.4.2-3.1) ...
Selecting previously unselected package python3-dateutil.
Preparing to unpack .../python3-dateutil_2.2-2_all.deb ...
Unpacking python3-dateutil (2.2-2) ...
Selecting previously unselected package python3-pyparsing.
Preparing to unpack .../python3-pyparsing_2.0.3+dfsg1-1_all.deb ...
Unpacking python3-pyparsing (2.0.3+dfsg1-1) ...
Selecting previously unselected package python3-tz.
Preparing to unpack .../python3-tz_2012c+dfsg-0.1_all.deb ...
Unpacking python3-tz (2012c+dfsg-0.1) ...
Selecting previously unselected package python3-nose.
Preparing to unpack .../python3-nose_1.3.4-1_all.deb ...
Unpacking python3-nose (1.3.4-1) ...
Selecting previously unselected package python3-matplotlib.
Preparing to unpack .../python3-matplotlib_1.4.2-3.1_armhf.deb ...
Unpacking python3-matplotlib (1.4.2-3.1) ...
Processing triggers for fontconfig (2.11.0-6.3+deb8u1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up fonts-lyx (2.1.2-2) ...
Setting up libjs-jquery-ui (1.10.1+dfsg-1) ...
Setting up python-matplotlib-data (1.4.2-3.1) ...
Setting up python3-dateutil (2.2-2) ...
Setting up python3-pyparsing (2.0.3+dfsg1-1) ...
Setting up python3-tz (2012c+dfsg-0.1) ...
Setting up python3-nose (1.3.4-1) ...
Setting up python3-matplotlib (1.4.2-3.1) ...

測試程式 : matplotlib_test.py
import numpy as np
import matplotlib.pyplot as plt
# Generate a sequence of numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)
# Create a second array using sine
y = np.sin(x)
# The plot function makes a line chart of one array against another
plt.plot(x, y, marker="x")
plt.show()

$python3 matplotlib_test.py


注意, matplotlib 一定要在桌面開啟 LXTerminal, 然後執行 python3 matplotlib_test.py 才會顯示, 若用 Putty 以 SSH 遠端存取執行不會顯示結果, 因為 Putty 是文字模式. 我是利用 VNC 遠端桌面執行. 另外, 最後一行要加上 plt.show() 才會在螢幕上顯示圖形, 原書中無此指令, 結果執行後也是沒有動靜.


4. 安裝 pandas : 

pi@raspberrypi:~ $ sudo apt-get install python3-pandas
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  python3-bs4 python3-lxml python3-numexpr python3-pandas-lib python3-tables python3-tables-lib
Suggested packages:
  python3-lxml-dbg python-pandas-doc python-tables-doc python-netcdf vitables
The following NEW packages will be installed:
  python3-bs4 python3-lxml python3-numexpr python3-pandas python3-pandas-lib python3-tables python3-tables-lib
0 upgraded, 7 newly installed, 0 to remove and 18 not upgraded.
Need to get 3,960 kB of archives.
After this operation, 21.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-bs4 all 4.3.2-2 [77.6 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-lxml armhf 3.4.0-1 [680 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-numexpr armhf 2.4-1 [143 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-pandas-lib armhf 0.14.1-2 [1,176 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-pandas all 0.14.1-2 [1,249 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-tables-lib armhf 3.1.1-3+b1 [305 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main python3-tables all 3.1.1-3 [329 kB]
Fetched 3,960 kB in 9s (407 kB/s)
Selecting previously unselected package python3-bs4.
(Reading database ... 144728 files and directories currently installed.)
Preparing to unpack .../python3-bs4_4.3.2-2_all.deb ...
Unpacking python3-bs4 (4.3.2-2) ...
Selecting previously unselected package python3-lxml.
Preparing to unpack .../python3-lxml_3.4.0-1_armhf.deb ...
Unpacking python3-lxml (3.4.0-1) ...
Selecting previously unselected package python3-numexpr.
Preparing to unpack .../python3-numexpr_2.4-1_armhf.deb ...
Unpacking python3-numexpr (2.4-1) ...
Selecting previously unselected package python3-pandas-lib.
Preparing to unpack .../python3-pandas-lib_0.14.1-2_armhf.deb ...
Unpacking python3-pandas-lib (0.14.1-2) ...
Selecting previously unselected package python3-pandas.
Preparing to unpack .../python3-pandas_0.14.1-2_all.deb ...
Unpacking python3-pandas (0.14.1-2) ...
Selecting previously unselected package python3-tables-lib.
Preparing to unpack .../python3-tables-lib_3.1.1-3+b1_armhf.deb ...
Unpacking python3-tables-lib (3.1.1-3+b1) ...
Selecting previously unselected package python3-tables.
Preparing to unpack .../python3-tables_3.1.1-3_all.deb ...
Unpacking python3-tables (3.1.1-3) ...
Setting up python3-bs4 (4.3.2-2) ...
Setting up python3-lxml (3.4.0-1) ...
Setting up python3-numexpr (2.4-1) ...
Setting up python3-pandas-lib (0.14.1-2) ...
Setting up python3-pandas (0.14.1-2) ...
Setting up python3-tables-lib (3.1.1-3+b1) ...
Setting up python3-tables (3.1.1-3) ...

測試程式 : pandas_test.py
import pandas as pd
# create a simple dataset of people
data = {'Name': ["John", "Anna", "Peter", "Linda"],
'Location' : ["New York", "Paris", "Berlin", "London"],
'Age' : [24, 13, 53, 33]
}
data_pandas = pd.DataFrame(data)
# IPython.display allows "pretty printing" of dataframes
# in the Jupyter notebook
display(data_pandas)

輸出 :

pi@raspberrypi:~ $ python pandas_test.py
Traceback (most recent call last):
  File "pandas_test.py", line 10, in
    display(data_pandas)
NameError: name 'display' is not defined

不知為何 display() 有問題. 


5. 安裝 scikit-learn :

pi@raspberrypi:~ $ sudo pip3 install scikit-learn -U
Downloading/unpacking scikit-learn
  Downloading scikit-learn-0.18.1.tar.gz (8.9MB): 8.9MB downloaded
  Running setup.py (path:/tmp/pip-build-y7_c22sq/scikit-learn/setup.py) egg_info for package scikit-learn
    Partial import of sklearn during the build process.

Installing collected packages: scikit-learn
  Running setup.py install for scikit-learn
    blas_opt_info:
    blas_mkl_info:
      libraries mkl,vml,guide not found in ['/usr/local/lib', '/usr/lib', '/usr/lib/arm-linux-gnueabihf']
      NOT AVAILABLE

    openblas_info:
      libraries  not found in ['/usr/local/lib', '/usr/lib', '/usr/lib/arm-linux-gnueabihf']
      NOT AVAILABLE

    atlas_blas_threads_info:
    Setting PTATLAS=ATLAS
      libraries ptf77blas,ptcblas,atlas not found in ['/usr/local/lib', '/usr/lib', '/usr/lib/arm-linux-gnueabihf']
      NOT AVAILABLE

    atlas_blas_info:
      libraries f77blas,cblas,atlas not found in ['/usr/local/lib', '/usr/lib', '/usr/lib/arm-linux-gnueabihf']
      NOT AVAILABLE

    blas_info:
      FOUND:
        language = f77
        library_dirs = ['/usr/lib']
        libraries = ['blas']

      FOUND:
        define_macros = [('NO_ATLAS_INFO', 1)]
        language = f77
        library_dirs = ['/usr/lib']
        libraries = ['blas']

    unifing config_cc, config, build_clib, build_ext, build commands --compiler options
    unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
    build_src
    building library "libsvm-skl" sources
    building library "cblas" sources
    building extension "sklearn.__check_build._check_build" sources
    building extension "sklearn.cluster._dbscan_inner" sources
    building extension "sklearn.cluster._hierarchical" sources
    building extension "sklearn.cluster._k_means_elkan" sources
    building extension "sklearn.cluster._k_means" sources
    building extension "sklearn.datasets._svmlight_format" sources
    building extension "sklearn.decomposition._online_lda" sources
    building extension "sklearn.decomposition.cdnmf_fast" sources
    building extension "sklearn.ensemble._gradient_boosting" sources
    building extension "sklearn.feature_extraction._hashing" sources
    building extension "sklearn.manifold._utils" sources
    building extension "sklearn.manifold._barnes_hut_tsne" sources
    building extension "sklearn.metrics.pairwise_fast" sources
    building extension "sklearn.metrics/cluster.expected_mutual_info_fast" sources
    building extension "sklearn.neighbors.ball_tree" sources
    building extension "sklearn.neighbors.kd_tree" sources
    building extension "sklearn.neighbors.dist_metrics" sources
    building extension "sklearn.neighbors.typedefs" sources
    building extension "sklearn.tree._tree" sources
    building extension "sklearn.tree._splitter" sources
    building extension "sklearn.tree._criterion" sources
    building extension "sklearn.tree._utils" sources
    building extension "sklearn.svm.libsvm" sources
    building extension "sklearn.svm.liblinear" sources
    building extension "sklearn.svm.libsvm_sparse" sources
    building extension "sklearn._isotonic" sources
    building extension "sklearn.linear_model.cd_fast" sources
    building extension "sklearn.linear_model.sgd_fast" sources
    building extension "sklearn.linear_model.sag_fast" sources
    building extension "sklearn.utils.sparsetools._traversal" sources
    building extension "sklearn.utils.sparsetools._graph_tools" sources
    building extension "sklearn.utils.sparsefuncs_fast" sources
    building extension "sklearn.utils.arrayfuncs" sources
    building extension "sklearn.utils.murmurhash" sources
    building extension "sklearn.utils.lgamma" sources
    building extension "sklearn.utils.graph_shortest_path" sources
    building extension "sklearn.utils.fast_dict" sources
    building extension "sklearn.utils.seq_dataset" sources
    building extension "sklearn.utils.weight_vector" sources
    building extension "sklearn.utils._random" sources
    building extension "sklearn.utils._logistic_sigmoid" sources
    building data_files sources
    build_src: building npy-pkg config files
    customize UnixCCompiler
    customize UnixCCompiler using build_clib
    building 'libsvm-skl' library
    compiling C++ sources
    C compiler: arm-linux-gnueabihf-g++ -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC

    compile options: '-I/usr/lib/python3/dist-packages/numpy/core/include -c'
    arm-linux-gnueabihf-g++: sklearn/svm/src/libsvm/libsvm_template.cpp
    arm-linux-gnueabihf-gcc-ar: adding 1 object files to build/temp.linux-armv6l-3.4/liblibsvm-skl.a
    building 'cblas' library
    compiling C sources
    C compiler: arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC

    compile options: '-I/usr/lib/python3/dist-packages/numpy/core/include -c'
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_daxpy.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_drefasum.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_srefrotg.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_ddot.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_drefcopy.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_errprn.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_srefrot.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_drefrotg.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_srot.c
    In file included from sklearn/src/cblas/cblas_srot.c:32:0:
    sklearn/src/cblas/atlas_misc.h:393:12: warning: ‘ATL_AlignOffset’ defined but not used [-Wunused-function]
     static int ATL_AlignOffset
                ^
    sklearn/src/cblas/atlas_misc.h:409:14: warning: ‘ATL_Align2Ptr’ defined but not used [-Wunused-function]
     static void *ATL_Align2Ptr(const void *pu, const void *pA)
                  ^
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_drefgemvT.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_drefrot.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_xerbla.c
    sklearn/src/cblas/cblas_xerbla.c: In function ‘cblas_xerbla’:
    sklearn/src/cblas/cblas_xerbla.c:52:4: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
        exit(-1);
        ^
    sklearn/src/cblas/cblas_xerbla.c:52:4: warning: incompatible implicit declaration of built-in function ‘exit’
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_srefcopy.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/ATL_dsrefdot.c
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_dger.c
    sklearn/src/cblas/cblas_dger.c: In function ‘cblas_dger’:
    sklearn/src/cblas/cblas_dger.c:73:7: warning: implicit declaration of function ‘cblas_xerbla’ [-Wimplicit-function-declaration]
           cblas_xerbla(info, "cblas_dger", "");
           ^
    In file included from sklearn/src/cblas/cblas_dger.c:32:0:
    sklearn/src/cblas/cblas_dger.c: At top level:
    sklearn/src/cblas/atlas_misc.h:393:12: warning: ‘ATL_AlignOffset’ defined but not used [-Wunused-function]
     static int ATL_AlignOffset
                ^
    sklearn/src/cblas/atlas_misc.h:409:14: warning: ‘ATL_Align2Ptr’ defined but not used [-Wunused-function]
     static void *ATL_Align2Ptr(const void *pu, const void *pA)
                  ^
    arm-linux-gnueabihf-gcc: sklearn/src/cblas/cblas_sasum.c
    In file included from sklearn/src/cblas/cblas_sasum.c:32:0:
    sklearn/src/cblas/atlas_misc.h:393:12: warning: ‘ATL_AlignOffset’ defined but not used [-Wunused-function]
     static int ATL_AlignOffset
                ^
......
(很多 warning)
......

    /usr/lib/python3/dist-packages/numpy/core/include/numpy/__multiarray_api.h:1629:1: warning: ‘_import_array’ defined but not used [-Wunused-function]
     _import_array(void)
     ^
    In file included from /usr/lib/python3/dist-packages/numpy/core/include/numpy/ufuncobject.h:327:0,
                     from sklearn/utils/_logistic_sigmoid.c:294:
    /usr/lib/python3/dist-packages/numpy/core/include/numpy/__ufunc_api.h:241:1: warning: ‘_import_umath’ defined but not used [-Wunused-function]
     _import_umath(void)
     ^
    arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 build/temp.linux-armv6l-3.4/sklearn/utils/_logistic_sigmoid.o -Lbuild/temp.linux-armv6l-3.4 -lm -o build/lib.linux-armv6l-3.4/sklearn/utils/_logistic_sigmoid.cpython-34m.so

    Partial import of sklearn during the build process.
    /usr/lib/python3/dist-packages/numpy/distutils/system_info.py:1521: UserWarning:
        Atlas (http://math-atlas.sourceforge.net/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [atlas]) or by setting
        the ATLAS environment variable.
      warnings.warn(AtlasNotFoundError.__doc__)
    sklearn/setup.py:72: UserWarning:
        Blas (http://www.netlib.org/blas/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [blas]) or by setting
        the BLAS environment variable.
      warnings.warn(BlasNotFoundError.__doc__)
Successfully installed scikit-learn
Cleaning up...

安裝了好久 (在 Type B 大約跑了 2 小時) 終於完成了. 

測試程式 : scikit_learn_test.py
from sklearn.datasets import load_iris
iris_dataset = load_iris()
print("Keys of iris_dataset: \n{}".format(iris_dataset.keys()))

輸出 :

Keys of iris_dataset:
['target_names', 'data', 'target', 'DESCR', 'feature_names']

6. 安裝 ipython3 :

pi@raspberrypi:~ $ sudo apt-get install ipython3
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  python-decorator python-pexpect python-simplegeneric
Suggested packages:
  ipython-doc ipython-notebook ipython-qtconsole python-matplotlib python-zmq python-pexpect-doc
The following NEW packages will be installed:
  ipython python-decorator python-pexpect python-simplegeneric
0 upgraded, 4 newly installed, 0 to remove and 17 not upgraded.
Need to get 689 kB of archives.
After this operation, 3,499 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
WARNING: The following packages cannot be authenticated!
  python-decorator python-pexpect python-simplegeneric ipython
Install these packages without verification? [y/N] y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-decorator all 3.4.0-2 [22.3 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-pexpect all 3.2-1 [38.4 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-simplegeneric all 0.8.1-1 [11.9 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main ipython all 2.3.0-2 [616 kB]
Fetched 689 kB in 3s (185 kB/s)
Selecting previously unselected package python-decorator.
(Reading database ... 135931 files and directories currently installed.)
Preparing to unpack .../python-decorator_3.4.0-2_all.deb ...
Unpacking python-decorator (3.4.0-2) ...
Selecting previously unselected package python-pexpect.
Preparing to unpack .../python-pexpect_3.2-1_all.deb ...
Unpacking python-pexpect (3.2-1) ...
Selecting previously unselected package python-simplegeneric.
Preparing to unpack .../python-simplegeneric_0.8.1-1_all.deb ...
Unpacking python-simplegeneric (0.8.1-1) ...
Selecting previously unselected package ipython.
Preparing to unpack .../ipython_2.3.0-2_all.deb ...
Unpacking ipython (2.3.0-2) ...
Processing triggers for gnome-menus (3.13.3-6) ...
Processing triggers for desktop-file-utils (0.22-1) ...
Processing triggers for mime-support (3.58) ...
Processing triggers for hicolor-icon-theme (0.13-1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up python-decorator (3.4.0-2) ...
Setting up python-pexpect (3.2-1) ...
Setting up python-simplegeneric (0.8.1-1) ...
Setting up ipython (2.3.0-2) ...

7. 安裝 ipython3-notebook :

pi@raspberrypi:~ $ sudo apt-get install ipython3-notebook
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  fonts-font-awesome fonts-mathjax ipython-notebook-common libjs-highlight libjs-highlight.js libjs-marked libjs-mathjax
  libjs-underscore libpgm-5.1-0 libsodium13 libzmq3 python-jinja2 python-markupsafe python-mysqldb python-pycurl python-tornado
  python-zmq
Suggested packages:
  python-pygments pandoc pgf fonts-stix fonts-mathjax-extras libjs-mathjax-doc python-jinja2-doc python-egenix-mxdatetime
  python-mysqldb-dbg libcurl4-gnutls-dev python-pycurl-dbg python-pycurl-doc
The following NEW packages will be installed:
  fonts-font-awesome fonts-mathjax ipython-notebook ipython-notebook-common libjs-highlight libjs-highlight.js libjs-marked
  libjs-mathjax libjs-underscore libpgm-5.1-0 libsodium13 libzmq3 python-jinja2 python-markupsafe python-mysqldb python-pycurl
  python-tornado python-zmq
0 upgraded, 18 newly installed, 0 to remove and 18 not upgraded.
Need to get 9,068 kB of archives.
After this operation, 52.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libsodium13 armhf 1.0.0-1 [143 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpgm-5.1-0 armhf 5.1.118-1~dfsg-1 [153 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main libzmq3 armhf 4.0.5+dfsg-2+deb8u1 [418 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main fonts-font-awesome all 4.2.0~dfsg-1 [346 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main fonts-mathjax all 2.4-2 [960 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjs-highlight.js all 8.2+ds-4 [165 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjs-highlight all 8.2+ds-4 [22.3 kB]
Get:8 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjs-marked all 0.3.2+dfsg-1 [12.6 kB]
Get:9 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjs-mathjax all 2.4-2 [5,333 kB]
Get:10 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjs-underscore all 1.7.0~dfsg-1 [49.9 kB]
Get:11 http://mirrordirector.raspbian.org/raspbian/ jessie/main ipython-notebook-common all 2.3.0-2 [724 kB]
Get:12 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-markupsafe armhf 0.23-1 [15.7 kB]
Get:13 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-jinja2 all 2.7.3-1 [170 kB]
Get:14 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-pycurl armhf 7.19.5-3 [48.7 kB]
Get:15 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-tornado armhf 3.2.2-1.1 [227 kB]
Get:16 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-zmq armhf 14.4.0-1 [173 kB]
Get:17 http://mirrordirector.raspbian.org/raspbian/ jessie/main ipython-notebook all 2.3.0-2 [48.1 kB]
Get:18 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-mysqldb armhf 1.2.3-2.1 [59.2 kB]
Fetched 9,068 kB in 32s (279 kB/s)
Selecting previously unselected package libsodium13:armhf.
(Reading database ... 139736 files and directories currently installed.)
Preparing to unpack .../libsodium13_1.0.0-1_armhf.deb ...
Unpacking libsodium13:armhf (1.0.0-1) ...
Selecting previously unselected package libpgm-5.1-0.
Preparing to unpack .../libpgm-5.1-0_5.1.118-1~dfsg-1_armhf.deb ...
Unpacking libpgm-5.1-0 (5.1.118-1~dfsg-1) ...
Selecting previously unselected package libzmq3:armhf.
Preparing to unpack .../libzmq3_4.0.5+dfsg-2+deb8u1_armhf.deb ...
Unpacking libzmq3:armhf (4.0.5+dfsg-2+deb8u1) ...
Selecting previously unselected package fonts-font-awesome.
Preparing to unpack .../fonts-font-awesome_4.2.0~dfsg-1_all.deb ...
Unpacking fonts-font-awesome (4.2.0~dfsg-1) ...
Selecting previously unselected package fonts-mathjax.
Preparing to unpack .../fonts-mathjax_2.4-2_all.deb ...
Unpacking fonts-mathjax (2.4-2) ...
Selecting previously unselected package libjs-highlight.js.
Preparing to unpack .../libjs-highlight.js_8.2+ds-4_all.deb ...
Unpacking libjs-highlight.js (8.2+ds-4) ...
Selecting previously unselected package libjs-highlight.
Preparing to unpack .../libjs-highlight_8.2+ds-4_all.deb ...
Unpacking libjs-highlight (8.2+ds-4) ...
Selecting previously unselected package libjs-marked.
Preparing to unpack .../libjs-marked_0.3.2+dfsg-1_all.deb ...
Unpacking libjs-marked (0.3.2+dfsg-1) ...
Selecting previously unselected package libjs-mathjax.
Preparing to unpack .../libjs-mathjax_2.4-2_all.deb ...
Unpacking libjs-mathjax (2.4-2) ...
Selecting previously unselected package libjs-underscore.
Preparing to unpack .../libjs-underscore_1.7.0~dfsg-1_all.deb ...
Unpacking libjs-underscore (1.7.0~dfsg-1) ...
Selecting previously unselected package ipython-notebook-common.
Preparing to unpack .../ipython-notebook-common_2.3.0-2_all.deb ...
Unpacking ipython-notebook-common (2.3.0-2) ...
Selecting previously unselected package python-markupsafe.
Preparing to unpack .../python-markupsafe_0.23-1_armhf.deb ...
Unpacking python-markupsafe (0.23-1) ...
Selecting previously unselected package python-jinja2.
Preparing to unpack .../python-jinja2_2.7.3-1_all.deb ...
Unpacking python-jinja2 (2.7.3-1) ...
Selecting previously unselected package python-pycurl.
Preparing to unpack .../python-pycurl_7.19.5-3_armhf.deb ...
Unpacking python-pycurl (7.19.5-3) ...
Selecting previously unselected package python-tornado.
Preparing to unpack .../python-tornado_3.2.2-1.1_armhf.deb ...
Unpacking python-tornado (3.2.2-1.1) ...
Selecting previously unselected package python-zmq.
Preparing to unpack .../python-zmq_14.4.0-1_armhf.deb ...
Unpacking python-zmq (14.4.0-1) ...
Selecting previously unselected package ipython-notebook.
Preparing to unpack .../ipython-notebook_2.3.0-2_all.deb ...
Unpacking ipython-notebook (2.3.0-2) ...
Selecting previously unselected package python-mysqldb.
Preparing to unpack .../python-mysqldb_1.2.3-2.1_armhf.deb ...
Unpacking python-mysqldb (1.2.3-2.1) ...
Processing triggers for fontconfig (2.11.0-6.3+deb8u1) ...
Setting up libsodium13:armhf (1.0.0-1) ...
Setting up libpgm-5.1-0 (5.1.118-1~dfsg-1) ...
Setting up libzmq3:armhf (4.0.5+dfsg-2+deb8u1) ...
Setting up fonts-font-awesome (4.2.0~dfsg-1) ...
Setting up fonts-mathjax (2.4-2) ...
Setting up libjs-highlight.js (8.2+ds-4) ...
Setting up libjs-highlight (8.2+ds-4) ...
Setting up libjs-marked (0.3.2+dfsg-1) ...
Setting up libjs-mathjax (2.4-2) ...
Setting up libjs-underscore (1.7.0~dfsg-1) ...
Setting up ipython-notebook-common (2.3.0-2) ...
Setting up python-markupsafe (0.23-1) ...
Setting up python-jinja2 (2.7.3-1) ...
Setting up python-pycurl (7.19.5-3) ...
Setting up python-tornado (3.2.2-1.1) ...
Setting up python-zmq (14.4.0-1) ...
Setting up ipython-notebook (2.3.0-2) ...
Setting up python-mysqldb (1.2.3-2.1) ...
Processing triggers for libc-bin (2.19-18+deb8u7) ...

這樣終於完成 scikit-learn 機器學習相關套件的安裝了.

值得一提的是, 由於我的樹莓派同時安裝了 python 2 與 3 版, 位置分別在 /usr/bin/python2 與 /usr/bin/python3, 要如何切換實際執行的 Python 解譯器版本呢? 這可以用 alias 或 link 來切換 :

pi@raspberrypi:~ $ python --version
Python 2.7.9
pi@raspberrypi:~ $ alias python='/usr/bin/python3'
pi@raspberrypi:~ $ python --version
Python 3.4.2
pi@raspberrypi:~ $ alias python='/usr/bin/python2'
pi@raspberrypi:~ $ python --version
Python 2.7.9

或者使用下列指令也可以 :

pi@raspberrypi:~ $ ln -s python /usr/bin/python3
pi@raspberrypi:~ $ ln -s python /usr/bin/python2

如上所述, 由於我不打算在樹莓派中使用 IPython, 所以我只安裝到 scikit-learn 而已, 馬上用 Win32DiskImager 製作映像檔, 免得萬一系統崩潰又要花很多時間重來一遍.

參考 :

# How can I run python scikit-learn on Raspberry Pi?
# What is the difference between installing a package using pip vs. apt-get?
# apt-get install vs pip install
Linux下切换Python2和Python3的4种方法
IPython :一個交互式計算和開發環境
# IPython Notebook 初探
# Copy current SD image to larger SD card
# Installing Scipy
# scikit-learn的安装和基本使用教程
different ways to get scikit-learn installed
# IPython Notebook 初探
# Install numpy on python3.3 - Install pip for python3
# How to install numpy on windows using pip install?

沒有留言:

張貼留言