numpy.ndarray

>>> import numpy as np
>>> a = np.random.rand(5)
>>> a
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> dir(a)
['T', '__abs__', '__add__', '__and__', '__array__', '__array_finalize__', '__array_interface__', '__array_prepare__', '__array_priority__', '__array_struct__', '__array_ufunc__', '__array_wrap__', '__bool__', '__class__', '__complex__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__iand__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__imul__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__lshift__', '__lt__', '__matmul__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmatmul__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__xor__', 'all', 'any', 'argmax', 'argmin', 'argpartition', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 'diagonal', 'dot', 'dtype', 'dump', 'dumps', 'fill', 'flags', 'flat', 'flatten', 'getfield', 'imag', 'item', 'itemset', 'itemsize', 'max', 'mean', 'min', 'nbytes', 'ndim', 'newbyteorder', 'nonzero', 'partition', 'prod', 'ptp', 'put', 'ravel', 'real', 'repeat', 'reshape', 'resize', 'round', 'searchsorted', 'setfield', 'setflags', 'shape', 'size', 'sort', 'squeeze', 'std', 'strides', 'sum', 'swapaxes', 'take', 'tobytes', 'tofile', 'tolist', 'tostring', 'trace', 'transpose', 'var', 'view']

any() all()

numpy 可以直接使用==进行比较(返回一个array),any()和all()是对比较结果进行操作

>>> b = a.copy()
>>> b == a
array([ True,  True,  True,  True,  True])
>>> (b==a).all()
True
>>> b[0]=0
>>> b==a
array([False,  True,  True,  True,  True])
>>> (b==a).all()
False
>>> (b==a).any()
True

argmax() argmin()

argmax()返回最大数的索引

argmin()返回最小数的索引

>>> a
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> a.argmax()
3
>>> a.argmin()
0
>>> 

argpartition()

argpartition(index)接受一个参数,argpartition返回一个索引数组,argpartition把比index小的索引放前面,大的放后面,不会进行排序

>>> a
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> a.argpartition(2)
array([0, 4, 1, 3, 2], dtype=int64)
>>> for i in a.argpartition(2):
	print(a[i])

	
0.26456759
0.3020745
0.65047261
0.89258495
0.76661875
>>> a[2]
0.76661875

argsort()

返回一个排序后的索引数组

>>> a
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> a.argsort()
array([0, 4, 1, 2, 3], dtype=int64)
>>> for i in a.argsort():
	print(a[i])

	
0.26456759
0.3020745
0.65047261
0.76661875
0.89258495

astype()

类型转换

>>> a.dtype
dtype('float64')
>>> a
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> a.dtype
dtype('float64')
>>> a.astype('f4')
array([0.26456758, 0.6504726 , 0.7666187 , 0.892585  , 0.3020745 ],
      dtype=float32)

base

基对象。用来判断对象是不是其他对象操作得来的,不是时为None

>>> b = a[0:1]
>>> b.base
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> a
array([0.26456759, 0.65047261, 0.76661875, 0.89258495, 0.3020745 ])
>>> b = np.random.random(5)
>>> print(b.base)
None

byteswap()

字节交换。大端和小端之间切换。

>>> A = np.array([1, 256, 8755], dtype=np.int16)
>>> map(hex, A)  //map()在3.6中无法使用
['0x1', '0x100', '0x2233']
>>> A.byteswap(inplace=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值