Numpy基础7:基本操作

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)
  • 最大最小值maxmin
ndarray.max()
ndarray.min()
  • 平均值mean
ndarray.mean()
Function NameNaN-safe VersionDescription
np.sumnp.nansumCompute sum of elements
np.prodnp.nanprodCompute product of elements
np.meannp.nanmeanCompute mean of elements
np.stdnp.nanstdCompute standard deviation
np.varnp.nanvarCompute variance
np.minnp.nanminFind minimum value
np.maxnp.nanmaxFind maximum value
np.argminnp.nanargminFind index of minimum value 找到最小数的下标
np.argmaxnp.nanargmaxFind index of maximum value 找到最大数的下标
np.mediannp.nanmedianCompute median of elements
np.percentilenp.nanpercentileCompute rank-based statistics of elements
np.anyN/AEvaluate whether any elements are true
np.allN/AEvaluate 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 false
  • np.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]])


数组循环

tilerepeat

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]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值