JobPlus知识库 IT 大数据 文章
Ubuntu16.04环境下Python下xlearn机器学习库的配置

一、xlearn的简介

        在机器学习里,除了深度学习和树模型 (GBDT, RF) 之外,如何高效地处理高维稀疏数据也是非常重要的课题,Sparse LR, FM, FFM 这些算法被广泛运用在实际生产和kaggle比赛中。现有的开源软件例如 liblinear, libfm, libffm 都只能针对特定的算法,并且可扩展性、灵活性、易用性都不够友好。

    相比于已有的软件,这款一款针对于海量数据处理任务的分布式机器学习系统xlearn的优势主要有:

    1、通用性好,我们用统一的架构将主流的算法(lr, fm, ffm 等)全部囊括,用户不用再切换于不同软件之间。

    2、性能好。xlearn由高性能c++开发,提供 cache-aware 和 lock-free learning,并且经过手工 SSE/AVX 指令优化。 在单机MacBook Pro上测试 xlearn 可以比 libfm 快13倍,比 libffm 和 liblinear 快5倍(基于Criteo CTR数据 bechmark)。

    3、易用性和灵活性,xlearn 提供简单的 python 接口,并且集合了机器学习比赛中许多有用的功能,例如:cross-validation,early-stopping 等。除此之外,用户可以灵活选择优化算法(例如,SGD,AdaGrad, FTRL 等)。

    4、可扩展性好。xlearn 提供 out-of-core 计算,利用外存计算可以在单机处理 1TB 数据。

二、xlearn的安装与配置(pip)


    使用pip安装xlearn库:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ sudo pip install xlearn  
  2. [sudo] password for yuhuiliu:  
  3. Collecting xlearn  
  4.   Downloading https://files.pythonhosted.org/packages/1a/20/d2762ecfd0da63bf2f0ee95429c7cf8ad44ab8ad4adc48b405fa67a09848/xlearn-0.31a1.tar.gz (1.8MB)  
  5.     100% |████████████████████████████████| 1.9MB 4.2MB/s  
  6. Building wheels for collected packages: xlearn  
  7.   Running setup.py bdist_wheel for xlearn ... done  
  8.   Stored in directory: /home/yuhuiliu/.cache/pip/wheels/9c/46/1a/e7682af4ef3320ad6e106c633aea0ee46ffb353aaf31723bab  
  9. Successfully built xlearn  
  10. tensorflow-gpu 1.7.0 requires numpy>=1.13.3, which is not installed.  
  11. h5py 2.7.1 requires numpy>=1.7, which is not installed.  
  12. tensorflow-tensorboard 1.5.0 requires numpy>=1.12.0, which is not installed.  
  13. torchvision 0.2.1 requires numpy, which is not installed.  
  14. pandas 0.22.0 requires numpy>=1.9.0, which is not installed.  
  15. patsy 0.5.0 requires numpy>=1.4, which is not installed.  
  16. matplotlib 2.1.2 requires numpy>=1.7.1, which is not installed.  
  17. keras 2.1.3 requires numpy>=1.9.1, which is not installed.  
  18. opencv-python 3.4.0.12 requires numpy>=1.11.1, which is not installed.  
  19. tensorflow 1.5.0 requires numpy>=1.12.1, which is not installed.  
  20. tensorboard 1.7.0 requires numpy>=1.12.0, which is not installed.  
  21. scipy 1.0.0 requires numpy>=1.8.2, which is not installed.  
  22. Installing collected packages: xlearn  
  23. Successfully installed xlearn-0.31a1  

    显示有下面的库没有安装或者需要更新:


[plain] 

  1. tensorflow-gpu 1.7.0 requires numpy>=1.13.3, which is not installed.  
  2. h5py 2.7.1 requires numpy>=1.7, which is not installed.  
  3. tensorflow-tensorboard 1.5.0 requires numpy>=1.12.0, which is not installed.  
  4. torchvision 0.2.1 requires numpy, which is not installed.  
  5. pandas 0.22.0 requires numpy>=1.9.0, which is not installed.  
  6. patsy 0.5.0 requires numpy>=1.4, which is not installed.  
  7. matplotlib 2.1.2 requires numpy>=1.7.1, which is not installed.  
  8. keras 2.1.3 requires numpy>=1.9.1, which is not installed.  
  9. opencv-python 3.4.0.12 requires numpy>=1.11.1, which is not installed.  
  10. tensorflow 1.5.0 requires numpy>=1.12.1, which is not installed.  
  11. tensorboard 1.7.0 requires numpy>=1.12.0, which is not installed.  
  12. scipy 1.0.0 requires numpy>=1.8.2, which is not installed.  

    这里显示的tensorflow-gpu等库依赖的numpy版本过低,需要更新一下numpy:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ sudo pip install tensorflow-gpu h5py  numpy scipy scikit-learn -U  

    这里附带更新了一下tensorflow-gpu h5py scipy scikit-learn 等库:

    

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ sudo pip install tensorflow-gpu h5py  numpy scipy scikit-learn -U  
  2. Collecting tensorflow-gpu  
  3.   Downloading https://files.pythonhosted.org/packages/f2/fa/01883fee1cdb4682bbd188edc26da5982c459e681543bb7f99299fca8800/tensorflow_gpu-1.8.0-cp35-cp35m-manylinux1_x86_64.whl (216.3MB)  
  4.     100% |████████████████████████████████| 216.3MB 219kB/s  
  5. Requirement already up-to-date: h5py in /usr/local/lib/python3.5/dist-packages (2.7.1)  
  6. Requirement already up-to-date: numpy in /usr/local/lib/python3.5/dist-packages (1.14.3)  
  7. Requirement already up-to-date: scipy in /usr/local/lib/python3.5/dist-packages (1.1.0)  
  8. Requirement already up-to-date: scikit-learn in /usr/local/lib/python3.5/dist-packages (0.19.1)  
  9. Requirement not upgraded as not directly required: wheel>=0.26 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (0.31.0)  
  10. Requirement not upgraded as not directly required: termcolor>=1.1.0 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (1.1.0)  
  11. Requirement not upgraded as not directly required: six>=1.10.0 in /usr/lib/python3/dist-packages (from tensorflow-gpu) (1.10.0)  
  12. Requirement not upgraded as not directly required: astor>=0.6.0 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (0.6.2)  
  13. Requirement not upgraded as not directly required: protobuf>=3.4.0 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (3.5.1)  
  14. Collecting tensorboard<1.9.0,>=1.8.0 (from tensorflow-gpu)  
  15.   Downloading https://files.pythonhosted.org/packages/59/a6/0ae6092b7542cfedba6b2a1c9b8dceaf278238c39484f3ba03b03f07803c/tensorboard-1.8.0-py3-none-any.whl (3.1MB)  
  16.     100% |████████████████████████████████| 3.1MB 1.8MB/s  
  17. Requirement not upgraded as not directly required: gast>=0.2.0 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (0.2.0)  
  18. Requirement not upgraded as not directly required: grpcio>=1.8.6 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (1.11.0)  
  19. Requirement not upgraded as not directly required: absl-py>=0.1.6 in /usr/local/lib/python3.5/dist-packages (from tensorflow-gpu) (0.1.9)  
  20. Requirement not upgraded as not directly required: setuptools in /usr/local/lib/python3.5/dist-packages (from protobuf>=3.4.0->tensorflow-gpu) (39.1.0)  
  21. Requirement not upgraded as not directly required: werkzeug>=0.11.10 in /usr/local/lib/python3.5/dist-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (0.14.1)  
  22. Requirement not upgraded as not directly required: markdown>=2.6.8 in /usr/local/lib/python3.5/dist-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (2.6.11)  
  23. Requirement not upgraded as not directly required: bleach==1.5.0 in /usr/local/lib/python3.5/dist-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (1.5.0)  
  24. Requirement not upgraded as not directly required: html5lib==0.9999999 in /usr/local/lib/python3.5/dist-packages (from tensorboard<1.9.0,>=1.8.0->tensorflow-gpu) (0.9999999)  
  25. Installing collected packages: tensorboard, tensorflow-gpu  
  26.   Found existing installation: tensorboard 1.7.0  
  27.     Uninstalling tensorboard-1.7.0:  
  28.       Successfully uninstalled tensorboard-1.7.0  
  29.   Found existing installation: tensorflow-gpu 1.7.0  
  30.     Uninstalling tensorflow-gpu-1.7.0:  
  31.       Successfully uninstalled tensorflow-gpu-1.7.0  
  32. Successfully installed tensorboard-1.8.0 tensorflow-gpu-1.8.0  

    显示xlearn成功安装:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ sudo pip install xlearn  
  2. [sudo] password for yuhuiliu:  
  3. Requirement already satisfied: xlearn in /usr/local/lib/python3.5/dist-packages (0.31a1)  

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ sudo pip list |grep xle  
  2. xlearn                        0.31a1  

三、使用conda创建新的环境

    在上面可以看见使用pip install了xlearn,但是只能在系统自带的python环境下使用,如下图使用conda list|grep xlear 命令查看xlearn库的信息,就发现没有该库的信息:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ conda list |grep xlea  
  2. yuhuiliu@sinclab-desktop:~$  

    参照https://www.zhihu.com/question/58033789对conda这一工具的介绍和https://www.jianshu.com/p/7e4c29a26f29的解决方法,这时为了预防配置第三方库不慎导致的崩溃,一般创建新的环境进行配置。

    1、先列出当前的环境列表:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ conda env list  
  2. # conda environments:  
  3. #  
  4. base                  *  /home/yuhuiliu/anaconda3  

    2、创建一个名字叫做ffm_baseline的环境,指定python版本为3.x(如果要安装特定的版本,如3.6,应为:python=3.6):

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ conda create -n ffm_baseline python=3  
  2. Solving environment: done  
  3.   
  4. ## Package Plan ##  
  5.   
  6.   environment location: /home/yuhuiliu/anaconda3/envs/ffm_baseline  
  7.   
  8.   added / updated specs:  
  9.     - python=3  
  10.   
  11.   
  12. The following NEW packages will be INSTALLED:  
  13.   
  14.     ca-certificates: 2018.03.07-0            defaults  
  15.     certifi:         2018.4.16-py36_0        defaults  
  16.     libedit:         3.1.20170329-h6b74fdf_2 defaults  
  17.     libffi:          3.2.1-hd88cf55_4        defaults  
  18.     libgcc-ng:       7.2.0-hdf63c60_3        defaults  
  19.     libstdcxx-ng:    7.2.0-hdf63c60_3        defaults  
  20.     ncurses:         6.1-hf484d3e_0          defaults  
  21.     openssl:         1.0.2o-h20670df_0       defaults  
  22.     pip:             10.0.1-py36_0           defaults  
  23.     python:          3.6.5-hc3d631a_2        defaults  
  24.     readline:        7.0-ha6073c6_4          defaults  
  25.     setuptools:      39.1.0-py36_0           defaults  
  26.     sqlite:          3.23.1-he433501_0       defaults  
  27.     tk:              8.6.7-hc745277_3        defaults  
  28.     wheel:           0.31.0-py36_0           defaults  
  29.     xz:              5.2.3-h5e939de_4        defaults  
  30.     zlib:            1.2.11-ha838bed_2       defaults  
  31.   
  32. Proceed ([y]/n)? Y  
  33.   
  34. Preparing transaction: done  
  35. Verifying transaction: done  
  36. Executing transaction: done  
  37. #  
  38. # To activate this environment, use:  
  39. # > source activate ffm_baseline  
  40. #  
  41. # To deactivate an active environment, use:  
  42. # > source deactivate  
  43. #  
  44.   
  45. yuhuiliu@sinclab-desktop:~$  

    3、此时使用conda list命令查看如下,会发现多了一个ffm_baseline的环境:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ conda env list  
  2. # conda environments:  
  3. #  
  4. base                  *  /home/yuhuiliu/anaconda3  
  5. ffm_baseline             /home/yuhuiliu/anaconda3/envs/ffm_baseline  

     4、source activate ffm_baseline一下进入ffm_baseline环境,使用conda list查看该环境的库:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ source activate ffm_baseline  
  2. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ conda list  
  3. # packages in environment at /home/yuhuiliu/anaconda3/envs/ffm_baseline:  
  4. #  
  5. # Name                    Version                   Build  Channel  
  6. ca-certificates           2018.03.07                    0    defaults  
  7. certifi                   2018.4.16                py36_0    defaults  
  8. libedit                   3.1.20170329         h6b74fdf_2    defaults  
  9. libffi                    3.2.1                hd88cf55_4    defaults  
  10. libgcc-ng                 7.2.0                hdf63c60_3    defaults  
  11. libstdcxx-ng              7.2.0                hdf63c60_3    defaults  
  12. ncurses                   6.1                  hf484d3e_0    defaults  
  13. openssl                   1.0.2o               h20670df_0    defaults  
  14. <span style="background-color:rgb(255,0,0);">pip                       10.0.1                   py36_0    defaults</span>  
  15. python                    3.6.5                hc3d631a_2    defaults  
  16. readline                  7.0                  ha6073c6_4    defaults  
  17. setuptools                39.1.0                   py36_0    defaults  
  18. sqlite                    3.23.1               he433501_0    defaults  
  19. tk                        8.6.7                hc745277_3    defaults  
  20. wheel                     0.31.0                   py36_0    defaults  
  21. xz                        5.2.3                h5e939de_4    defaults  
  22. zlib                      1.2.11               ha838bed_2    defaults  

     这里的lib列表里面展示了conda默认环境下的一些基本库,与默认安装就有的base环境下的库相比,也小巧的多,方便我们配置,因为anacond的功能如jupyter等默认还需要其他库的支持。

四、第三方库xlearn在conda环境下的使用

    在网上搜索之后,发现的原因是anaconda在托管了系统的python环境后,pip命令默认指向的是系统原始python环境里面的pip命令,因此使用pip install xlearn只能安装在系统环境的pip list中;而如果pip命令指向的是anconda托管环境中的pip命令时,可以pip install xlearn到conda的list中,作为对比,在另外一台电脑sinc-server上试验如下:

[plain] 

  1. yuhuiliu@sinc-server:~$ pip install xlearn  
  2. Collecting xlearn  
  3.   Downloading https://files.pythonhosted.org/packages/1a/20/d2762ecfd0da63bf2f0ee95429c7cf8ad44ab8ad4adc48b405fa67a09848/xlearn-0.31a1.tar.gz (1.8MB)  
  4.     100% |████████████████████████████████| 1.9MB 620kB/s  
  5. Building wheels for collected packages: xlearn  
  6.   Running setup.py bdist_wheel for xlearn ... done  
  7.   Stored in directory: /home/yuhuiliu/.cache/pip/wheels/9c/46/1a/e7682af4ef3320ad6e106c633aea0ee46ffb353aaf31723bab  
  8. Successfully built xlearn  
  9. Installing collected packages: xlearn  
  10. Successfully installed xlearn-0.31a1  
  11. You are using pip version 9.0.1, however version 10.0.1 is available.  
  12. You should consider upgrading via the 'pip install --upgrade pip' command.  
  13. yuhuiliu@sinc-server:~$ conda list |grep xlearn  
  14. xlearn                    0.31a1                    <pip>  

[plain] 

  1. yuhuiliu@sinc-server:~$ python  
  2. Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)  
  3. [GCC 7.2.0] on linux  
  4. Type "help", "copyright", "credits" or "license" for more information.  
  5. >>> import xlearn  
  6. >>>  

这时,参考网上的说明,如果想在conda的环境中使用第三方库xlearn的话,附录网上的几种建议如下:

  

[plain] 

  1. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ sudo pip install xlearn  
  2. [sudo] password for yuhuiliu:  
  3. Requirement already satisfied: xlearn in /usr/local/lib/python3.5/dist-packages (0.31a1)  

    将原始安装于/usr/local/lib/python3.5/dist-packages/的库文件,

    #此处的文件大都是用pip安装的,也就是刚刚安装的xlearn也位于此处,但是还有一些依赖项#


    将这个文件夹下的所有文件复制到 对应的~/anaconda3/lib/python3.5/site-packages/目录下,有重复的选择替换


    经过查看:

[plain] 

  1. yuhuiliu@sinclab-desktop:~$ ls ~/anaconda3/lib/p  
  2. pkgconfig/ python3.6/  

    anaconda3/lib的目录下面环境为python3.6的库,强行合并python3.5下的库到python3.6的库下面感觉有点不靠谱~

    2 、参照https://segmentfault.com/q/1010000012539647和https://www.zhihu.com/question/41974592中“史密斯”同学的解答,再根据上面的https://www.jianshu.com/p/7e4c29a26f29,先sudo pip uninstall xlearn卸载掉xlearn,直接执行/home/yuhuiliu/anaconda3/bin下的pip命令:

[plain] 

  1. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ sudo pip uninstall xlearn  
  2. [sudo] password for yuhuiliu:  
  3. Uninstalling xlearn-0.31a1:  
  4.   Would remove:  
  5.     /usr/local/lib/python3.5/dist-packages/xlearn-0.31a1.dist-info/*  
  6.     /usr/local/lib/python3.5/dist-packages/xlearn/*  
  7. Proceed (y/n)? Y  
  8.   Successfully uninstalled xlearn-0.31a1  
  9. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ sudo /home/yuhuiliu/anaconda3/bin/pip install xlearn  
  10. Collecting xlearn  
  11.   Using cached https://files.pythonhosted.org/packages/1a/20/d2762ecfd0da63bf2f0ee95429c7cf8ad44ab8ad4adc48b405fa67a09848/xlearn-0.31a1.tar.gz  
  12. Building wheels for collected packages: xlearn  
  13.   Running setup.py bdist_wheel for xlearn ... done  
  14.   Stored in directory: /home/yuhuiliu/.cache/pip/wheels/9c/46/1a/e7682af4ef3320ad6e106c633aea0ee46ffb353aaf31723bab  
  15. Successfully built xlearn  
  16. <span style="background-color:rgb(255,0,0);">distributed 1.21.8 requires msgpack, which is not installed.</span>  
  17. Installing collected packages: xlearn  
  18. Successfully installed xlearn-0.31a1  

    显示有依赖问题,这里先不管,来conda list |grep xlear看一下xlearn的信息:

[plain] 

  1. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ conda list |grep xlear  
  2. (ffm_baseline) yuhuiliu@sinclab-desktop:~$   

    额,好像没有,直接执行/home/yuhuiliu/anaconda3/bin下的pip命令安装到的是默认的base环境下,切换到base环境,conda list |grep xlear看一下xlearn的信息:

[plain] 

  1. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ conda env list  
  2. # conda environments:  
  3. #  
  4. base                     /home/yuhuiliu/anaconda3  
  5. ffm_baseline          *  /home/yuhuiliu/anaconda3/envs/ffm_baseline  
  6.   
  7. (ffm_baseline) yuhuiliu@sinclab-desktop:~$ source activate base  
  8. (base) yuhuiliu@sinclab-desktop:~$ conda list |grep xlear  
  9. xlearn                    0.31a1                    <pip>  
  10. (base) yuhuiliu@sinclab-desktop:~$ python  
  11. Python 3.6.5 |Anaconda custom (64-bit)| (default, Apr 29 2018, 16:14:56)  
  12. [GCC 7.2.0] on linux  
  13. Type "help", "copyright", "credits" or "license" for more information.  
  14. >>> import xlearn as xl  
  15. >>> xl.hello()  
  16. ----------------------------------------------------------------------------------------------  
  17.            _  
  18.           | |  
  19.      __  _| |     ___  __ _ _ __ _ __  
  20.      \ \/ / |    / _ \/ _` | '__| '_ \  
  21.       >  <| |___|  __/ (_| | |  | | | |  
  22.      /_/\_\_____/\___|\__,_|_|  |_| |_|  
  23.   
  24.         xLearn   -- 0.31 Version --  
  25. ----------------------------------------------------------------------------------------------  
  26.   
  27. >>>  

   到这里,xlearn可以在conda创建的base环境下使用了。

    3、解决xlearn的依赖问题:

[plain] 

  1. (base) yuhuiliu@sinclab-desktop:~$ sudo /home/yuhuiliu/anaconda3/bin/pip install msgpack  
  2. [sudo] password for yuhuiliu:  
  3. Collecting msgpack  
  4.   Downloading https://files.pythonhosted.org/packages/22/4e/dcf124fd97e5f5611123d6ad9f40ffd6eb979d1efdc1049e28a795672fcd/msgpack-0.5.6-cp36-cp36m-manylinux1_x86_64.whl (315kB)  
  5.     100% |████████████████████████████████| 317kB 11kB/s  
  6. Installing collected packages: msgpack  
  7. Successfully installed msgpack-0.5.6  
  8. (base) yuhuiliu@sinclab-desktop:~$  



如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
281人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序