Quickstart
外形操纵
改变阵列形状
import numpy as np
rg = np.random.default_rng(1)
# 准备一个数组
a = np.floor(10*rg.random((3,4)))
#array([[3., 7., 3., 4.],
# [1., 4., 2., 2.],
# [7., 2., 4., 9.]])
print(a.shape) # (3, 4)
# 以下三个命令均返回修改后的数组,但不更改原始数组
print(a.ravel()) # 平铺:array([3., 7., 3., 4., 1., 4., 2., 2., 7., 2., 4., 9.])
print(a.reshape(6,2)) # 重塑格式,不改变原始数组
print(a.reshape(3,-1)) # 参数为-1则自动适配长度
#array([[3., 7.],
# [3., 4.],
# [1., 4.],
# [2., 2.],
# [7., 2.],
# [4., 9.]])
print(a.T) # 转置,不改变原始数组
#array([[3., 1., 7.],
# [7., 4., 2.],
# [3., 2., 4.],
# [4., 2., 9.]])
print(a.T.shape) # (4, 3)
print(a.shape) # (3, 4)
# 以下命令改变原始数组
a.resize((2,6)) # 重塑格式,改变原始数组
堆叠在一起的不同阵列
a = np.floor(10*rg.random((2,2)))
# array([[9., 7.],
# [5., 2.]])
b = np.floor(10*rg.random((2,2)))
#array([[1., 9.],
# [5., 1.]])
np.vstack((a,b)) # vstake:按第一维度堆叠
#array([[9., 7.],
# [5., 2.],
# [1., 9.],
# [5., 1.]])
np.hstack((a,b)) # hstack:按第二维度堆叠
#array([[9., 7., 1., 9.],
# [5., 2., 5., 1.]])
column_stake:该函数将一维数组当作列拼接到二维数组后,在二维情况下相当于hstake
import numpy as np
from numpy import newaxis
a = np.array([4.,2.])
b = np.array([3.,8.])
print(np.column_stack((a,b))) # column_stack返回二维数组
# [[4. 3.]
# [2. 8.]]
print(np.hstack((a,b))) # hstack与column_stack返回结果不同
# [4. 2. 3. 8.]
print(a[:,newaxis]) # 把a看作二维数组
# [[4.]
# [2.]]
print(np.column_stack((a[:,newaxis],b[:,newaxis]))) # column_stack返回二维数组
# [[4. 3.]
# [2. 8.]]
print(np.hstack((a[:,newaxis],b[:,newaxis]))) # hstack与column_stack返回结果相同
# [[4. 3.]
# [2. 8.]]
row_stack:相当于 vstack
print(np.column_stack is np.hstack) # False
print(np.row_stack is np.vstack) # True
concatenate:可选择按第几轴方向堆叠
numpy.concatenate((a1, a2, ...), axis=0, out=None)
# In general, for arrays with more than two dimensions,
#column_stack:第一轴方向堆叠,返回二维数组
#hstack:按第二轴方向堆叠
#vstack:按第一轴方向堆叠
#concatenate:可选轴
#总结:hstack, vstack, column_stack, concatenate, c_, r_
hsplit:切分
import numpy as np
rg = np.random.default_rng(1)
a = np.floor(10*rg.random((2,12)))
# [[5. 9. 1. 9. 3. 4. 8. 4. 5. 0. 7. 5.]
# [3. 7. 3. 4. 1. 4. 2. 2. 7. 2. 4. 9.]]
print(np.hsplit(a,4)) # 指定均分为4组
# [array([[5., 9., 1.],
# [3., 7., 3.]]), array([[9., 3., 4.],
# [4., 1., 4.]]), array([[8., 4., 5.],
# [2., 2., 7.]]), array([[0., 7., 5.],
# [2., 4., 9.]])]
print(np.hsplit(a,(3,6))) # 指定切分的位置
# [array([[5., 9., 1.],
# [3., 7., 3.]]),
# array([[9., 3., 4.],
# [4., 1., 4.]]),
# array([[8., 4., 5., 0., 7., 5.],
# [2., 2., 7., 2., 4., 9.]])]
复制和视图
简单的引用不会复制生成新对象
import numpy as np
a = np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
b = a # a和b是同一对象的两个名称
print(b is a) # True
def f(x):return id(x) # Python将可变对象作为引用传递,因此函数调用不会创建新对象
print(id(a)) # 1707217192960
print(f(a)) # 1707217192960
视图和浅复制
import numpy as np
rg = np.random.default_rng(1)
a = np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
c = a.view() #视图与引用对象共享数据,但改变视图格式不改变引用对象格式
print(c is a) # False
print(c.base is a) # True
print(c.flags.owndata) # False
c = c.reshape((2, 6)) # a的形状不改变
print(c)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]]
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
c[0, 4] = 1234 # a的数据被改变
print(c)
# [[ 0 1 2 3 1234 5]
# [ 6 7 8 9 10 11]]
print(a)
# [[ 0 1 2 3]
# [1234 5 6 7]
# [ 8 9 10 11]]
深度复制
d = a.copy() # 创建新对象
print(d is a) # False
print(d.base is a) # False
函数和方法概述
这是一些有用的NumPy函数和方法名称的列表,按类别排序。
有关完整列表,请参见https://numpy.org/doc/stable/reference/routines.html#routines。
~
数组创建
arange, array, copy, empty, empty_like, eye, fromfile,
fromfunction, identity, linspace, logspace, mgrid, ogrid, ones,
ones_like, r_, zeros, zeros_like
~
转换
ndarray.astype, atleast_1d, atleast_2d, atleast_3d, mat
~
操纵
array_split, column_stack, concatenate, diagonal,
dsplit, dstack, hsplit, hstack, ndarray.item, newaxis, ravel, repeat,
reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack
~
判断
all, any, nonzero, where
~
序列
argmax, argmin, argsort, max, min, ptp, searchsorted, sort
~
操作
choose, compress, cumprod, cumsum, inner, ndarray.fill,
imag, prod, put, putmask, real, sum
~
基本统计函数
cov, mean, std, var
~
基本线性贷数
cross, dot, outer, linalg.svd, vdot