Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

3.1 NumPy介绍

numpy-logo

NumPy是一个开源的Python库,几乎在科学和工程的每个领域都有应用场景。它给如何处理数值型数据提供了一种通用标准。

NumPy的用户包括从初级程序员、分析师到从事最先进的科学和工业研究的研究人员。NumPy也被广泛应用其他Python工具中,包括Pandas、SciPy、Matplotlib、scikit-learn等。

NumPy主要提供了:

  • 创建一维到多维数组(矩阵)的方法

  • 快速操作数组的方法,包括数学、逻辑、形状操作、排序、选择、还有一些基本的统计方法。

如何加载Numpy呢?

Anaconda的python环境预装了NumPy库,我们使用以下方式来加载NumPy到当前运行环境下:

import numpy as np

使用as np是为了增加之后代码的可读性和简易性。这种方式被广泛地采用,你应该会慢慢习惯。

如果import过程中出现失败,显示未找到numpy,可能没有安装。请在jupyter-lab新建一个Terminal(终端)。

在终端下,输入命令

pip install numpy --user

如果网速比较慢,可以打断以上命令,使用ctrl+c。然后指定pypi镜像地址:https://mirrors.aliyun.com/pypi/simple,输入命令如下:

pip install numpy --user -i https://mirrors.aliyun.com/pypi/simple

或者使用conda来安装也可以,使用conda来安装,输入以下命令:

conda install numpy

可以把某个源设为默认,例如把清华源设为默认:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

练习:安装NumPy