numpy常用函数
max
max(a, axis=None, out=None, keepdims=np._NoValue)
Return the maximum of an array or maximum along an axis.
返回array的最大值或者沿axis轴的最大值
axis参数详解
np.max(a,axis)表示在axis轴上取得的最值
axis的取值范围为[0,len(a.shape))
axis也可以是一个元组axis=(0,1)表示在第一个轴和第二个轴相交的空间上取得的最值
1 | >>> a=np.array( |
where
where(condition, [x, y])
Return elements, either from x
or y
, depending on condition
.If only condition
is given, return condition.nonzero()
.
按照给定condition
返回元素x
或y
.如果只给定condition
,则返回condition.nonzero()
.
1 | >>> a=np.arange(9).reshape(3,3) |
cumsum
cumsum(a, axis=None, dtype=None, out=None)
Return the cumulative sum of the elements along a given axis.
返回给定轴上元素累计和.也可以称作累加函数
1 | >>> a=np.arange(6).reshape(2,3) |
cumprod
cumprod(a, axis=None, dtype=None, out=None):
Return the cumulative product of elements along a given axis.
返回给定轴上元素累计积.也可以称作累积函数
1 | >>> a=np.arange(1,7).reshape(2,3) |
unique
asarray
asarray(a, dtype=None, order=None)
Convert the input to an array.
将输入转化为array
1 | >>> a=[1,2,3,4] |
errstate
np.errstate(**kwargs)
kwargs:{divide,over,under,invalid}
关键字参数。有效的关键字是可能的浮点异常。每个关键字都应该有一个字符串值来定义特定错误的处理。可能的值为{‘ignore’,’warn’,’raise’,’call’,’print’,’log’}。
transpose
transpose(a, axes=None)
Permute the dimensions of an array.
将矩阵a进行转置, 其中当a为一维时不起作用, 转置物理意义为切换坐标轴, 将x轴换为y轴, y轴换为x轴.
aa=np.arange(4).reshape(2,2)
aa
array([[0, 1],
[2, 3]])
bb =aa.transpose()
array([[0, 2],
[1, 3]])
#aa.transpose() <===> aa.transpose(1,0), 将0轴(数据下标)换为1轴, 将1轴换为0轴. 如a[0][1] 变换为 bb[1][0]
aa=np.arange(27).reshape(-1,3,3)
aa
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
bb=aa.transpose()
array([[[ 0, 9, 18],
[ 3, 12, 21],
[ 6, 15, 24]],
[[ 1, 10, 19],
[ 4, 13, 22],
[ 7, 16, 25]],
[[ 2, 11, 20],
[ 5, 14, 23],
[ 8, 17, 26]]])
#此时 aa.transpose() <===> aa.transpose(2,1,0), 将0轴换为2轴, 1轴不变, 2轴换为0轴. 如 aa[2][1][0] 变换为 bb[0][1][2]
bb= aa.transpose(2,0,1)
array([[[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]],
[[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]],
[[ 2, 5, 8],
[11, 14, 17],
[20, 23, 26]]])
#同理, aa[2][0][1] == bb[1][2][0]
clip
clip(a, a_min, a_max, out=None)
Clip (limit) the values in an array.
将a限制在a_min, a_max内,
a = np.arange(10)
np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
np.clip(a, 1, 8) <===> b=np.where(a<1, 1, a) 和 b =np.where(b>8,8,b)
设置精度
np.set_printoptions(precision=2)
np.squeeze
empty
随机初始化数组
genfromtxt
1 | def genfromtxt(fname, dtype=float, comments='#', delimiter=None, |
Load data from a text file, with missing values handled as specified.
Each line past the first skip_header
lines is split at the delimiter
character, and characters following the comments
character are discarded.
identity
np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])