Numpy
Numpy基础7:基本操作
索引
li = [[1,2,3,],[4,5,6]]
arr = np.array(li)
arr[1][0] # 与列表一样
arr[1,0] # tuple类型的索引
⇒ \Rightarrow ⇒ 4,4
aa[[1,0,1]] #使用索引生成新的array对象
⇒ \Rightarrow ⇒ array([[4, 5, 6],[1, 2, 3],[4, 5, 6]])
重设形状
reshape
#在形状改变中 -1 代表的是剩余的元素总和
ndarray.reshape(ndarray.shape[0],-1) # 留下一维,二维和三维融合
#将数组展开成一维
ndarray.reshape(-1)
#把数组转换成 多行一列
ndarray.reshape(-1,1).shape
数组展开
ravel
的目的是将任意形状的数组扁平化,变为 1 维数组。不管是几维的数组都会变成1维的数据
ndarray.ravel()
级联
concatenate
- 级联的参数是列表:一定要加中括号或小括号
- 维度必须相同,形状相符
- 级联的方向默认是
shape
这个tuple的第一个值所代表的维度方向
可通过axis
参数改变级联的方向,默认为0
, (0
表示列相连,行发生改变,表示的Y轴的事情,1
表示列相连,列发生改变,X轴的事情)
ndarray2 = ndarray[:,:,::-1]
np.concatenate([ndarray,ndarray2],axis=0)
vstack
水平级联hstack
垂直级联,填入的参数必须被小括号或中括号包裹vertical
垂直的horizontal
水平的stack
层积,这两个函数的值也是一个list或tuple
np.vstack([ndarray,ndarray2 ])
np.hstack((ndarray,ndarray2 ))
聚合函数
- 累加
sum
- ∑ = s i g m a \sum = sigma ∑=sigma
axis
不写则为所有的元素求和,为0表示行求和,1表示列求和
ndarray.sum(axis)
- 最大最小值
max
、min
ndarray.max()
ndarray.min()
- 平均值
mean
ndarray.mean()
Function Name | NaN-safe Version | Description |
---|---|---|
np.sum | np.nansum | Compute sum of elements |
np.prod | np.nanprod | Compute product of elements |
np.mean | np.nanmean | Compute mean of elements |
np.std | np.nanstd | Compute standard deviation |
np.var | np.nanvar | Compute variance |
np.min | np.nanmin | Find minimum value |
np.max | np.nanmax | Find maximum value |
np.argmin | np.nanargmin | Find index of minimum value 找到最小数的下标 |
np.argmax | np.nanargmax | Find index of maximum value 找到最大数的下标 |
np.median | np.nanmedian | Compute median of elements |
np.percentile | np.nanpercentile | Compute rank-based statistics of elements |
np.any | N/A | Evaluate whether any elements are true |
np.all | N/A | Evaluate whether all elements are true |
np.power square | ||
np.argwhere(nd1<0) | ||
np.bincount |
带有nan前缀的函数
- #
NaN
Javascript not a number 不是一个数字的数字类型 - 任何数值+
NaN
=NaN
NaN
== MySQL当中的Null
就是空数据类型None
np.nan
==np.NaN
⇒ \Rightarrow ⇒ falsenp.array([np.nan,1,2,3])
⇒ \Rightarrow ⇒ array([nan, 1., 2., 3.])- 带有
nan
前缀的函数都不会计算nan
带有arg的函数
- 返回相应的索引
where函数
- np.where(ndarray condition 条件)
- 返回一个tuple
- 索引为0 的位置是y轴坐标
- 索引为1的位置是x轴坐标
- np.concatenate([y.reshape(-1,1),x.reshape(-1,1)],axis=1)用于合并以上tuple
- 直接返回符合条件的坐标
如果要是使用数组进行条件查询,最好先把数组展开
cond = np.argwhere(ndarray.ravel() condition 条件)
cond是序列类型,是二维的,也需要展开cond.ravel() - index = ndarray.ravel() condition 条件
ndarray.ravel()[cond_index]
计数
- np.bincount([2,2,2,2,10,2]) : array([0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 1], dtype=int64)
- 解读方式是数索引,2出现过5次,10出现过1次,其它数字没出现过
轴移动
moveaxis
可以将数组的轴移动到新的位置。其方法如下:
np.moveaxis(a, source, destination)
a
:数组。source
:要移动的轴的原始位置。destination
:要移动的轴的目标位置。
A = np.random.randint(0,100,(6,5,4))
A.T.shape
np.moveaxis(np.moveaxis(A,0,-1),0,1).shape
⇒
\Rightarrow
⇒(4, 5, 6)
⇒
\Rightarrow
⇒(4, 5, 6)
轴交换
和 moveaxis
不同的是,swapaxes
可以用来交换数组的轴。其方法如下:
numpy.swapaxes(a, axis1, axis2)
a
:数组。axis1
:需要交换的轴 1 位置。axis2
:需要与轴 1 交换位置的轴 1 位置。
B = np.random.randint(0,100,(7,6,5,4))
B.T.shape
np.swapaxes(np.swapaxes(B,0,-1),1,2).shape
⇒
\Rightarrow
⇒(4, 5, 6, 7)
⇒
\Rightarrow
⇒(4, 5, 6, 7)
数组转置
transpose
类似于矩阵的转置,它可以将 2 维数组的水平轴和垂直交换。其方法如下:
numpy.transpose(a, axes=None)
a
:数组。axis
:该值默认为none
,表示转置。如果有值,那么则按照值替换轴。
C = np.random.randint(0,100,(7,6,5,4))
C.transpose().shape
C.T.shape
⇒
\Rightarrow
⇒(4, 5, 6, 7)
⇒
\Rightarrow
⇒(4, 5, 6, 7)
广播机制
- 数组乘以一个数等于数据中的每个元素都乘以一个数,在语言特性中,这就是广播机制
np.array([[1,2,3],[4,5,6,]])*3
⇒ \Rightarrow ⇒array([[ 3, 6, 9], [12, 15, 18]])
数组循环
tile
与 repeat
np.tile([[1,2,3],[4,5,6]],3)
⇒ \Rightarrow ⇒array([[1, 2, 3, 1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6, 4, 5, 6]])
np.repeat([[2,1,3],[4,5,6]],3,axis=0)
⇒ \Rightarrow ⇒array([[2, 1, 3], [2, 1, 3], [2, 1, 3], [4, 5, 6], [4, 5, 6], [4, 5, 6]])