一、介绍

官方文档:https://www.numpy.org.cn/reference/
实例介绍 https://www.cnblogs.com/A-FM/p/6547137.html
可以实现
1. 任意齐次项的数组对象
2. 数组上的快速数学运算
3.线性代数,傅里叶变换,随机数生成
numpy模块定义两个新的对象类型:array和ufunc,以及一组操作该对象的函数,可以将这两个新的对象类型与其他Python类型进行转换。

二、下载及安装

https://sourceforge.net/projects/numpy/
image.png
下载完成,到下载目录下面,进入解压目录,进行安装
image.png
image.png

三、array对象

array对象是一个可为大数目的数字集合,集合内的数字必须是相同类型,eg:都是双精度浮点数
创建array对象的语法格式:
array(numbers [ , tpyecode = None] )
numbers是一个序列对象,eg:元组或列表
typecode是number是元素的类型

3.1 创建array对象

  1. ##创建一个一维向量数组
  2. from numpy import *
  3. x ,y,z =1,2,3
  4. a = array([x,y,z])
  5. print(a)
  6. [1 2 3]
  7. ##创建一个一维向量数组,并将向量值以浮点数表示
  8. from numpy import *
  9. x,y,z =1,2,3
  10. a = array([x,y,z],float)
  11. print(a)
  12. [1. 2. 3.]
  13. ##创建一个2行3列的矩阵
  14. from numpy import *
  15. ma = array([[1,2,3],[4,5,6]])
  16. print(ma)
  17. print(ma.shape) ##显示矩阵的行列数
  18. [[1 2 3]
  19. [4 5 6]]
  20. (2, 3)

3.2 操作array对象

  1. from numpy import *
  2. ma = array([[1,2,3],[4,5,6]])
  3. ##将矩阵改成一维矩阵
  4. ma2 = reshape(ma,(6,))
  5. print("一维矩阵:","\n" , ma2)
  6. ##将矩阵改成9行9列的矩阵
  7. big = resize(ma,(9,9))
  8. print("多维矩阵:" ,"\n",big)
  9. 一维矩阵:
  10. [1 2 3 4 5 6]
  11. 多维矩阵:
  12. [[1 2 3 4 5 6 1 2 3]
  13. [4 5 6 1 2 3 4 5 6]
  14. [1 2 3 4 5 6 1 2 3]
  15. [4 5 6 1 2 3 4 5 6]
  16. [1 2 3 4 5 6 1 2 3]
  17. [4 5 6 1 2 3 4 5 6]
  18. [1 2 3 4 5 6 1 2 3]
  19. [4 5 6 1 2 3 4 5 6]
  20. from numpy import *
  21. a =array([[1,2,3],[4,5,6]])
  22. b =array([[7,8,9],[10,11,12]])
  23. print("两个矩阵相加的结果","\n",a+b)
  24. print("两个矩阵相乘的结果","\n",a*b)
  25. 两个矩阵相加的结果
  26. [[ 8 10 12]
  27. [14 16 18]]
  28. 两个矩阵相乘的结果
  29. [[ 7 16 27]
  30. [40 55 72]]

3.3 数组属性

3.3.3.1 内存布局

方法 描述
ndarray.flags 有关数组内存布局的信息。
ndarray.shape 数组维度的元组。
ndarray.strides 遍历数组时每个维度中的字节元组。
ndarray.ndim 数组维数。
ndarray.data Python缓冲区对象指向数组的数据的开头。
ndarray.size 数组中的元素数。
ndarray.itemsize 一个数组元素的长度,以字节为单位
ndarray.nbytes 数组元素消耗的总字节数。
ndarray.base 如果内存来自其他对象,则为基础对象。

3.3.3.2 数据类型

方法 描述
ndarray.dtype 数组元素的数据类型。

3.3.3.3 其他属性

方法 描述
ndarray.T 转置数组。
ndarray.real 数组的真实部分。
ndarray.imag 数组的虚部。
ndarray.flat 数组上的一维迭代器。
ndarray.ctypes 一个简化数组与ctypes模块交互的对象

四、ufunc对象

ufunc对象是一个用于操作array对象的函数集合。是一种ndarrays以逐元素方式操作的函数,支持数组广播,类型转换和其他一些标准功能。也就是说,ufunc是一个函数的 “ 矢量化 ” 包装器,它接受固定数量的特定输入并产生固定数量的特定输出。

4.1 数学运算

方法 描述
add(x1, x2, /[, out, where, cast, order, …]) 按元素添加参数。
subtract(x1, x2, /[, out, where, cast, …]) 从元素方面减去参数。
multiply(x1, x2, /[, out, where, cast, …]) 在元素方面乘以论证。
divide(x1, x2, /[, out, where, cast, …]) 以元素方式返回输入的真正除法。
logaddexp(x1, x2, /[, out, where, cast, …]) 输入的取幂之和的对数。
logaddexp2(x1, x2, /[, out, where, cast, …]) base-2中输入的取幂之和的对数。
true_divide(x1, x2, /[, out, where, …]) 以元素方式返回输入的真正除法。
floor_divide(x1, x2, /[, out, where, …]) 返回小于或等于输入除法的最大整数。
negative(x, /[, out, where, cast, order, …]) 数字否定, 元素方面。
positive(x, /[, out, where, cast, order, …]) 数字正面, 元素方面。
power(x1, x2, /[, out, where, cast, …]) 第一个数组元素从第二个数组提升到幂, 逐个元素。
remainder(x1, x2, /[, out, where, cast, …]) 返回除法元素的余数。
mod(x1, x2, /[, out, where, cast, order, …]) 返回除法元素的余数。
fmod(x1, x2, /[, out, where, cast, …]) 返回除法的元素余数。
divmod(x1, x2 [, out1, out2], /[[, out, …]) 同时返回逐元素的商和余数。
absolute(x, /[, out, where, cast, order, …]) 逐个元素地计算绝对值。
fabs(x, /[, out, where, cast, order, …]) 以元素方式计算绝对值。
rint(x, /[, out, where, cast, order, …]) 将数组的元素舍入为最接近的整数。
sign(x, /[, out, where, cast, order, …]) 返回数字符号的元素指示。
heaviside(x1, x2, /[, out, where, cast, …]) 计算Heaviside阶跃函数。
conj(x, /[, out, where, cast, order, …]) 以元素方式返回复共轭。
conjugate(x, /[, out, where, cast, …]) 以元素方式返回复共轭。
exp(x, /[, out, where, cast, order, …]) 计算输入数组中所有元素的指数。
exp2(x, /[, out, where, cast, order, …]) 计算输入数组中所有 p 的 2**p。
log(x, /[, out, where, cast, order, …]) 自然对数, 元素方面。
log2(x, /[, out, where, cast, order, …]) x的基数为2的对数。
log10(x, /[, out, where, cast, order, …]) 以元素方式返回输入数组的基数10对数。
expm1(x, /[, out, where, cast, order, …]) 计算数组中的所有元素。exp(x) - 1
log1p(x, /[, out, where, cast, order, …]) 返回一个加上输入数组的自然对数, 逐个元素。
sqrt(x, /[, out, where, cast, order, …]) 以元素方式返回数组的非负平方根。
square(x, /[, out, where, cast, order, …]) 返回输入的元素方块。
cbrt(x, /[, out, where, cast, order, …]) 以元素方式返回数组的立方根。
reciprocal(x, /[, out, where, cast, …]) 以元素方式返回参数的倒数。
gcd(x1, x2, /[, out, where, cast, order, …]) 返回 | x1 | 和的最大公约数 | x2 | 。
lcm(x1, x2, /[, out, where, cast, order, …]) 返回 | x1 | 和的最小公倍数 | x2 | 。

4.2 三角函数

方法 描述
sin(x, /[, out, where, cast, order, …]) 三角正弦, 元素方式。
cos(x, /[, out, where, cast, order, …]) 余弦元素。
tan(x, /[, out, where, cast, order, …]) 计算切线元素。
arcsin(x, /[, out, where, cast, order, …]) 反向正弦, 元素方式。
arccos(x, /[, out, where, cast, order, …]) 三角反余弦, 元素方式。
arctan(x, /[, out, where, cast, order, …]) 三角反正切, 逐元素。
arctan2(x1, x2, /[, out, where, cast, …]) x1/x2正确选择象限的逐元素反正切。
hypot(x1, x2, /[, out, where, cast, …]) 给定直角三角形的“腿”, 返回其斜边。
sinh(x, /[, out, where, cast, order, …]) 双曲正弦, 元素。
cosh(x, /[, out, where, cast, order, …]) 双曲余弦, 元素。
tanh(x, /[, out, where, cast, order, …]) 计算双曲正切元素。
arcsinh(x, /[, out, where, cast, order, …]) 逆双曲正弦元素。
arccosh(x, /[, out, where, cast, order, …]) 反双曲余弦, 元素。
arctanh(x, /[, out, where, cast, order, …]) 逆双曲正切元素。
deg2rad(x, /[, out, where, cast, order, …]) 将角度从度数转换为弧度。
rad2deg(x, /[, out, where, cast, order, …]) 将角度从弧度转换为度数。

4.3 位运算函数

方法 描述
bitwise_and(x1, x2, /[, out, where, …]) 逐个元素地计算两个数组的逐位AND。
bitwise_or(x1, x2, /[, out, where, cast, …]) 逐个元素地计算两个数组的逐位OR。
bitwise_xor(x1, x2, /[, out, where, …]) 逐个元素地计算两个数组的逐位XOR。
invert(x, /[, out, where, cast, order, …]) 计算逐位反转, 或逐位NOT, 逐元素计算。
left_shift(x1, x2, /[, out, where, cast, …]) 将整数位移到左侧。
right_shift(x1, x2, /[, out, where, …]) 将整数位移到右侧。

4.4 比较函数

方法 描述
greater(x1, x2, /[, out, where, cast, …]) 以元素方式返回(x1 > x2)的真值。
greater_equal(x1, x2, /[, out, where, …]) 以元素方式返回(x1 >= x2)的真值。
less(x1, x2, /[, out, where, cast, …]) 返回(x1 < x2)元素的真值。
less_equal(x1, x2, /[, out, where, cast, …]) 以元素方式返回(x1 =< x2)的真值。
not_equal(x1, x2, /[, out, where, cast, …]) 以元素方式返回(x1 != x2)。
equal(x1, x2, /[, out, where, cast, …]) 以元素方式返回(x1 == x2)。
fmax(x1, x2, /[, out, where, cast, …]) 元素最大的数组元素。
fmin(x1, x2, /[, out, where, cast, …]) 元素最小的数组元素。
minimum(x1, x2, /[, out, where, cast, …]) 元素最小的数组元素
maximum(x1, x2, /[, out, where, cast, …]) 元素最大的数组元素

4.5 浮动函数

方法 描述
isfinite(x, /[, out, where, cast, order, …]) 测试元素的有限性(不是无穷大或不是数字)。
isinf(x, /[, out, where, cast, order, …]) 正面或负面无穷大的元素测试。
isnan(x, /[, out, where, cast, order, …]) 测试NaN的元素, 并将结果作为布尔数组返回。
isnat(x, /[, out, where, cast, order, …]) 为NaT(不是时间)测试元素, 并将结果作为布尔数组返回。
fabs(x, /[, out, where, cast, order, …]) 以元素方式计算绝对值。
signbit(x, /[, out, where, cast, order, …]) 返回元素为True设置signbit(小于零)。
copysign(x1, x2, /[, out, where, cast, …]) 将元素x1的符号更改为x2的符号。
nextafter(x1, x2, /[, out, where, cast, …]) 将x1之后的下一个浮点值返回x2(元素方向)。
spacing(x, /[, out, where, cast, order, …]) 返回x与最近的相邻数字之间的距离。
modf(x [, out1, out2], /[[, out, where, …]) 以元素方式返回数组的小数和整数部分。
ldexp(x1, x2, /[, out, where, cast, …]) 以元素方式返回x1 2 * x2。
frexp(x [, out1, out2], /[[, out, where, …]) 将x的元素分解为尾数和二进制指数。
fmod(x1, x2, /[, out, where, cast, …]) 返回除法的元素余数。
floor(x, /[, out, where, cast, order, …]) 以元素方式返回输入的底限。
ceil(x, /[, out, where, cast, order, …]) 以元素方式返回输入的上限。
trunc(x, /[, out, where, cast, order, …]) 以元素方式返回输入的截断值。

4.6 产生随机数

linspace() 方法可以产生指定数目和范围的随机数

  1. import numpy as nps
  2. print(nps.linspace(1,3,9))
  3. ##1-3之间的9个随机数
  4. ##结果
  5. [1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. ]
  1. from numpy import *
  2. a =array([[1,2,3],[4,5,6]])
  3. b =array([[7,8,9],[10,11,12]])
  4. print("两个矩阵之和:","\n",add(a,b))
  5. a = arange(10)
  6. print("正弦值:","\n",sin(a))
  7. 两个矩阵之和:
  8. [[ 8 10 12]
  9. [14 16 18]]
  10. 正弦值:
  11. [ 0. 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427
  12. -0.2794155 0.6569866 0.98935825 0.41211849]