python_需要安装的相关库

机器学习 Python 相关环境


win

Python2.7

安装pip

如果在 Python27\Scripts 文件夹中有 pip.exe 就不需要再安装pip了,3.5版本的默认就自带了pip

  1. 安装好Python2.7后,将 F:\Python27F:\Python27\Scripts 都加入环境变量

  2. 下载 pip-9.0.1.tar.gz ,https://pypi.python.org/pypi/pip,解压后cd该目录 执行 python setup.py install 安装

  3. 然后就可以安装 .whl 文件了,执行 pip install xxx.whl

    1
    2
    3
    4
    C:\Users\yangxuan\Desktop>pip install pyparsing-2.2.0-py2.py3-none-any.whl

    # 成功后显示
    Successfully installed pyparsing-2.2.0

pip 的 安装、更新、移除、 查看 相关库的命令

以 Python3.5,库为 numpy 为例

  • 安装: pip3 install numpy-xxx.whl

  • 更新: pip3 install -U numpy or pip3 install --upgrade numpy

  • 检查升级: pip3 list --outdated

  • 移除: pip3 uninstall numpy

  • 查看: pip3 show protobuf or ``pip3 show –files protobuf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    C:\Users\yangxuan>pip3 show protobuf
    Name: protobuf
    Version: 3.3.0
    Summary: Protocol Buffers
    Home-page: https://developers.google.com/protocol-buffers/
    Author: protobuf@googlegroups.com
    Author-email: protobuf@googlegroups.com
    License: 3-Clause BSD License
    Location: f:\python35\lib\site-packages
    Requires: six, setuptools

安装matplotlib2d图形库

  1. 安装 matplotlib-1.5.0-cp27-none-win_amd64.whl

  2. 相关依赖库

    • pyparsing-2.2.0-py2.py3-none-any.whl
    • python_dateutil-2.6.0-py2.py3-none-any.whl
    • cycler-0.10.0-py2.py3-none-any.whl
    • numpy-1.11.3+mkl-cp27-cp27m-win32.whl
  3. 测试一下就可以绘制2d图形

    这里写图片描述


安装 scikit-learn

  1. 安装 scikit_learn-0.18.1-cp27-cp27m-win32.whl

  2. 相关依赖库

    • scipy-0.19.0-cp27-cp27m-win32.whl
    • numpy-1.11.3+mkl-cp27-cp27m-win32.whl
  3. 测试一下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    def scikitTest1():
    iris = datasets.load_iris()
    digits = datasets.load_digits()
    print digits.data
    pass

    结果
    [[ 0. 0. 5. ..., 0. 0. 0.]
    [ 0. 0. 0. ..., 10. 0. 0.]
    [ 0. 0. 0. ..., 16. 9. 0.]
    ...,
    [ 0. 0. 1. ..., 6. 0. 0.]
    [ 0. 0. 2. ..., 12. 0. 0.]
    [ 0. 0. 10. ..., 12. 1. 0.]]
    [Finished in 0.9s]

sublime text 3 指定 Python 路径

  • 新建一个编译,Tools -> Build System -> New Build System,加入以下代码指定Python解释器路径

    1
    2
    3
    4
    5
    6
    {
    "path":"F:/Python36",
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
    }

Python3.5

  1. 下载地址:https://www.python.org/ftp/python/3.5.3/python-3.5.3-amd64.exe

  2. 安装 matplotlib2d 图形库,会自动下载相关的依赖,下载地址:matplotlib-2.0.2-cp35-cp35m-win_amd64.whl ,然后使用pip安装

    1
    2
    3
    4
    5
    C:\Users\yangxuan\Desktop>pip install matplotlib-2.0.2-cp35-cp35m-win_amd64.whl
    ...
    Installing collected packages: pytz, six, numpy, pyparsing, cycler, python-dateutil, matplotlib
    Successfully installed cycler-0.10.0 matplotlib-2.0.2 numpy-1.13.0 pyparsing-2.2.0 python-dateutil-2
    .6.0 pytz-2017.2 six-1.10.0


mac

Python3.5

下载地址


sublime配置环境路径

  • 系统默认会添加到环境路径下,输入命令 python3,再 /usr/bin 路径下可以找到

    1
    2
    3
    4
    MacBook-Pro:~ wilker$ python3
    Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 08:49:46)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
  • Sublime 配置指定版本解释器,其实安装路径实在 /Library/Frameworks/Python.framework/Versions/3.5/bin

    1
    2
    3
    4
    5
    6
    {
    "path":"$PATH:/Library/Frameworks/Python.framework/Versions/3.5/bin",
    "cmd": ["python3.5", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
    }

安装matplotlib库

  • 直接输入命令会自动下载,注意使用的是 pip3

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    MacBook-Pro:~ wilker$ pip3 install matplotlib
    Collecting matplotlib
    Downloading matplotlib-2.0.2-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (12.8MB)
    100% |████████████████████████████████| 12.8MB 82kB/s
    Collecting numpy>=1.7.1 (from matplotlib)
    Downloading numpy-1.13.0-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.5MB)
    100% |████████████████████████████████| 4.5MB 163kB/s
    Collecting python-dateutil (from matplotlib)
    Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194kB)
    100% |████████████████████████████████| 194kB 240kB/s
    Collecting cycler>=0.10 (from matplotlib)
    Downloading cycler-0.10.0-py2.py3-none-any.whl
    Collecting six>=1.10 (from matplotlib)
    Downloading six-1.10.0-py2.py3-none-any.whl
    Collecting pytz (from matplotlib)
    Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
    100% |████████████████████████████████| 491kB 241kB/s
    Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=1.5.6 (from matplotlib)
    Downloading pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 253kB/s
    Installing collected packages: numpy, six, python-dateutil, cycler, pytz, pyparsing, matplotlib
    Successfully installed cycler-0.10.0 matplotlib-2.0.2 numpy-1.13.0 pyparsing-2.2.0 python-dateutil-2.6.0 pytz-2017.2 six-1.10.0

  • 安装其他可以也是一样,直接 pip3 install xxx 会自动下载

安装TensorFlow(cpu)

  • 官网文档:https://www.tensorflow.org/install/install_mac

  • 输入命令:$ pip3 install --upgrade tensorflow ,会自动下载关联的库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    MacBook-Pro:~ wilker$ pip3 install --upgrade tensorflow
    Collecting tensorflow
    Downloading tensorflow-1.1.0-cp35-cp35m-macosx_10_11_x86_64.whl (30.9MB)
    100% |████████████████████████████████| 30.9MB 44kB/s
    Collecting protobuf>=3.2.0 (from tensorflow)
    Downloading protobuf-3.3.0.tar.gz (271kB)
    100% |████████████████████████████████| 276kB 252kB/s
    Collecting wheel>=0.26 (from tensorflow)
    Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 254kB/s
    Requirement already up-to-date: six>=1.10.0 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from tensorflow)
    Requirement already up-to-date: numpy>=1.11.0 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from tensorflow)
    Collecting werkzeug>=0.11.10 (from tensorflow)
    Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
    100% |████████████████████████████████| 317kB 270kB/s
    Collecting setuptools (from protobuf>=3.2.0->tensorflow)
    Downloading setuptools-36.0.1-py2.py3-none-any.whl (476kB)
    100% |████████████████████████████████| 481kB 262kB/s
    Installing collected packages: setuptools, protobuf, wheel, werkzeug, tensorflow
    Found existing installation: setuptools 28.8.0
    Uninstalling setuptools-28.8.0:
    Successfully uninstalled setuptools-28.8.0
    Running setup.py install for protobuf ... done
    Successfully installed protobuf-3.3.0 setuptools-36.0.1 tensorflow-1.1.0 werkzeug-0.12.2 wheel-0.29.0
  • 测试一下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    def tf_test1():
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    result = sess.run(hello)
    print(result)
    # 输出结果:Hello, TensorFlow!

    a = tf.constant(10)
    b = tf.constant(32)
    result = sess.run(a+b)
    print(result)
    pass

    结果有一堆警告
    2017-06-14 17:53:46.808537: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
    2017-06-14 17:53:46.808552: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
    2017-06-14 17:53:46.808555: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
    2017-06-14 17:53:46.808579: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
    2017-06-14 17:53:46.808581: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
    b'Hello, TensorFlow!'
    42
    [Finished in 1.2s]