02Sklearn库的安装

Sklearn库的安装

sklearn是scikit-learn的简称,是一个基于Python的第三方模块。sklearn库集成了一些常用的机器学习方法,在进行机器学习任务时,并不需要实现算法,只需要简单的调用sklearn库中提供的模块就能完成大多数的机器学习任务。

sklearn库是在Numpy、Scipy和matplotlib的基础上开发而成的,因此在介绍sklearn的安装前,需要先安装这些依赖库。

Numpy库

Numpy ( Numerical Python的缩写)是一个开源的Python科学计算库。
Scipy库是sklearn库的基础,它是基于Numpy的一个集成了多种数学算法和函数的Python模块。
matplotlib是基于Numpy的一套Python工具包,它提供了大量的数据绘图工具。

安装顺序

  • Numpy库
  • Scipy库
  • matplotlib库
  • sklearn库

02Sklearn库的安装

依赖库之Numpy的安装

访问Numpy的相关下载链接依据Python的具体版本下载对应的文件。例如:使用的是Python3.5的64位版,责下载win_amd64.whl文件。

1、首先下载Numpy函数库,下载链接https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 。

注意选择与自己Python版本匹配的numpy,我下载的是numpy-1.21.2+mkl-cp39-cp39-win_amd64.whl,放在python中的Scripts文件夹里

2、安装Numpy库

  • cmd命令打开Windows终端,切换到Scripts目录(忘记截图了)

  • 首先升级pip,在python的Scripts目录下执行python -m pip install --upgrade pip

  • 输入命令安装Numpy函数库,格式为:pip install numpy名称。

    我的是pip install numpy numpy-1.21.2+mkl-cp39-cp39-win_amd64.whl

3、numpy函数库安装成功后,可以查看

  • 切换到Python所在目录,执行python.exe ,进入python
  • 最后,输入from numpy import *,可用 random.rand(4,4)检测Numpy是否安装成功,若运行结果如下图所示,则安装成功。
D:\Biye\python>python.exe
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> random.rand(4,4)
array([[0.39905189, 0.73615976, 0.38211157, 0.03597167],
       [0.44192813, 0.16232804, 0.48702556, 0.10889808],
       [0.54138559, 0.06563385, 0.9558441 , 0.44737099],
       [0.20978171, 0.38831494, 0.86710668, 0.46733382]])
>>> exit()

依赖库之Scipy库的安装

1、在网站上找到对应的scipy文件,我下载的是scipy-1.7.1-cp39-cp39-win_amd64.whl,我放在python中的Scripts文件夹里

2、运行pip install scipy-1.7.1-cp39-cp39-win_amd64.whl ,进行安装

  ```b
  D:\Biye\python\Scripts>pip install scipy-1.7.1-cp39-cp39-win_amd64.whl
  Processing d:\biye\python\scripts\scipy-1.7.1-cp39-cp39-win_amd64.whl
  Requirement already satisfied: numpy<1.23.0,>=1.16.5 in d:\biye\python\lib\site-packages (from scipy==1.7.1) (1.21.2+mkl)
  Installing collected packages: scipy
  Successfully installed scipy-1.7.1
  ```

3、Scipy函数库安装成功后,可以查看

  • 与Numpy一样,在python中查看

  • import scipy

  • exit()退出后,可以用pip list查看当前的版本

    D:\Biye\python>python
    Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import scipy
    >>> exit()
    
    D:\Biye\python>pip list
    Package    Version
    ---------- ----------
    numpy      1.21.2+mkl
    pip        21.2.4
    scipy      1.7.1
    setuptools 57.4.0
    

依赖库之matplotlib库的安装

1、在网站上找到对应的matplotlib文件,我下载的是matplotlib-3.4.3-cp39-cp39-win_amd64.whl,我放在python中的Scripts文件夹里

2、运行pip install matplotlib-3.4.3-cp39-cp39-win_amd64.whl ,进行安装

D:\Biye\python\Scripts>pip install matplotlib-3.4.3-cp39-cp39-win_amd64.whl
Processing d:\biye\python\scripts\matplotlib-3.4.3-cp39-cp39-win_amd64.whl
Collecting pyparsing>=2.2.1
  Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 410 kB/s
Requirement already satisfied: numpy>=1.16 in d:\biye\python\lib\site-packages (from matplotlib==3.4.3) (1.21.2+mkl)
Collecting pillow>=6.2.0
  Downloading Pillow-8.3.2-cp39-cp39-win_amd64.whl (3.2 MB)
     |████████████████████████████████| 3.2 MB 297 kB/s
Collecting python-dateutil>=2.7
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     |████████████████████████████████| 247 kB 107 kB/s
Collecting kiwisolver>=1.0.1
  Downloading kiwisolver-1.3.2-cp39-cp39-win_amd64.whl (52 kB)
     |████████████████████████████████| 52 kB 157 kB/s
Collecting cycler>=0.10
  Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Collecting six
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, python-dateutil, pyparsing, pillow, kiwisolver, cycler, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.3.2 matplotlib-3.4.3 pillow-8.3.2 pyparsing-2.4.7 python-dateutil-2.8.2 six-1.16.0

3、matplotlib函数库安装成功后,可以查看

  • 与numpy一样,在python中查看

  • import matplotlib

  • exit()退出后,可以用pip list查看当前的版本

    D:\Biye\python>python
    Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import matplotlib
    >>> exit()
    
    D:\Biye\python>pip list
    Package         Version
    --------------- ----------
    cycler          0.10.0
    kiwisolver      1.3.2
    matplotlib      3.4.3
    numpy           1.21.2+mkl
    Pillow          8.3.2
    pip             21.2.4
    pyparsing       2.4.7
    python-dateutil 2.8.2
    scipy           1.7.1
    setuptools      57.4.0
    six             1.16.0
    

sklearn库的安装

在以上三个库都安装完之后,可以安装sklearn库

1、下载sklearn文件,下载地址:https://pypi.python.org/pypi/scikit-learn/0.18.1 ,我是在https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 下安装的,找到的相关文件scikit_learn-1.0-cp39-cp39-win_amd64.whl,我放在python中的Scripts文件夹里

2、运行pip install scikit_learn-1.0-cp39-cp39-win_amd64.whl ,进行安装

D:\Biye\python\Scripts>pip install scikit_learn-1.0-cp39-cp39-win_amd64.whl
Processing d:\biye\python\scripts\scikit_learn-1.0-cp39-cp39-win_amd64.whl
Requirement already satisfied: scipy>=1.1.0 in d:\biye\python\lib\site-packages (from scikit-learn==1.0) (1.7.1)
Requirement already satisfied: numpy>=1.14.6 in d:\biye\python\lib\site-packages (from scikit-learn==1.0) (1.21.2+mkl)
Collecting threadpoolctl>=2.0.0
  Downloading threadpoolctl-2.2.0-py3-none-any.whl (12 kB)
Collecting joblib>=0.11
  Using cached joblib-1.0.1-py3-none-any.whl (303 kB)
Installing collected packages: threadpoolctl, joblib, scikit-learn
Successfully installed joblib-1.0.1 scikit-learn-1.0 threadpoolctl-2.2.0

3、与之前一样的操作,来测试,太繁琐我就不继续写了,同样的操作执行4次,熟练的让人心疼

最后,用pip list查看以下已经安装的库

D:\Biye\python>pip list
Package         Version
--------------- ----------
cycler          0.10.0
joblib          1.0.1
kiwisolver      1.3.2
matplotlib      3.4.3
numpy           1.21.2+mkl
Pillow          8.3.2
pip             21.2.4
pyparsing       2.4.7
python-dateutil 2.8.2
scikit-learn    1.0
scipy           1.7.1
setuptools      57.4.0
six             1.16.0
threadpoolctl   2.2.0
上一篇:mac系统matplotlib中文乱码(本人新手小白一个,没有干货就是解决问题的小白文)


下一篇:matplotlib 在画图前应该提前设置好的参数