numpy的函数对象ufunc详解

一、什么是ufunc对象?

ufunc对象是numpy的两大基本对象之一,另一个是array。ufunc是universal function object的缩写。在Python里面,一切皆为对象,包括我们的函数,而numpy里面的函数是ufunc对象的实例,如下所示:

type(np.add)#返回的是   <class 'numpy.ufunc'>  即ufunc对象

既然是add,本身也是一个ufunc类的对象,那我自然也可以通过 “  对象.方法或属性  ”去加以访问了。那到底ufunc类中定义了哪些方法或者是的属性呢,可以通过下面的方式加以查看;

dir(np.ufunc)

返回的列表为:

['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'accumulate', 'at', 'identity', 'nargs', 'nin', 'nout', 'ntypes', 'outer', 'reduce', 'reduceat', 'signature', 'types']

特别的,对于二元操作符所对应的ufunc对象,如上面的add,可以使用下面的一些方法(注意:虽然numpy函数是ufunc的对象,但不是每一个函数都是,也不是每一个函数都可以使用上面的ufunc对象罗列出来的所有的属性和方法)

二、ufunc方法的一些简单应用

1、reduce方法——np.add.reduce()

     reduce(),沿着指定轴对数组进行操作,相当于将相应的操作放到该轴元素之间。其实就是求某一个维度上的和

a1=np.add.reduce([1,2,3])                  #1+2+3=6
a2=np.add.reduce([[1,2,3],[4,5,6]])        #[1+4,2+5,3+6]=[5,7,9]
a3=np.add.reduce([[1,2,3],[4,5,6]],axis=1) #[1+2+3,4+5+6]=[6,15]
a4=np.add.reduce([[1,2,3],[4,5,6]],axis=0) #[14,2+5,3+6]=[5,7,9]

默认为axis=0

2、accumulate()和reduce()类似,区别时是前者会保留中间结果:

b1=np.add.accumulate([1,2,3])                          #[1,1+2,1+2+3]=[1,3,6]
print(b1)
b2=np.add.accumulate([[1,2,3],[4,5,6],[7,8,9]],axis=0) #
print(b2)
b3=np.add.accumulate([[1,2,3],[4,5,6],[7,8,9]],axis=1) 
print(b3)

b2的结果为 

[[1,2,3],

 [1+4,2+5,3+6],

  [1+4+7,2+5+8,3+6+9]]

所以最终的结果为:

[[ 1 2 3]

[ 5 7 9]

[12 15 18]]

b3的结果为:

[[ 1 ,1+2,1+2+3]

[ 4 ,4+5,4+5+6]

[7,7+8,7+8+9]]

所以最终的结果为:

[[ 1 3 6]

[ 4 9 15]

[ 7 15 24]]

3、reduceat()方法计算多纽reduce()的结果,通 过 indices参数指定一系列的起始和终止位置。它的计算有些特别,,计算的方法如下:

 

三、常见的numpy函数

1、三角函数

sin(x)
cos(x)
tan(x)
sinh(x)
conh(x)
tanh(x)
arccos(x)
arctan(x)
arcsin(x)
arccosh(x)
arctanh(x)
arcsinh(x)
arctan2(x,y)

arctan2(x,y) 返回 arctan(x/y)

2、向量操作

dot(x,y)
inner(x,y)
cross(x,y)
vdot(x,y)
outer(x,y)
kron(x,y)
tensordot(x,y[,axis])

3、其他常见初等函数

exp(x)
log(x)
log10(x)
sqrt(x)
absolute(x)
conjugate(x)
negative(x)
ceil(x)
floor(x)
fabs(x)
hypot(x)
fmod(x)
maximum(x,y)
minimum(x,y)

hypot 返回对应点 (x,y) 到原点的距离。

4、类型处理及判断

iscomplexobj
iscomplex
isrealobj
isreal
imag
real
real_if_close
isscalar
isneginf
isposinf
isinf
isfinite
isnan
nan_to_num
common_type
typename

5、数组修改形状及变更

atleast_1d
atleast_2d
atleast_3d
expand_dims
apply_over_axes
apply_along_axis
hstack
vstack
dstack
column_stack
hsplit
vsplit
dsplit
split
squeeze

6、其他有用函数

fix
mod
amax
amin
ptp
sum
cumsum
prod
cumprod
diff
angle

unwrap
sort_complex
trim_zeros
fliplr
flipud
rot90
diag
eye
select
extract
insert

roots
poly
any
all
disp
unique
nansum
nanmax
nanargmax
nanargmin
nanmin

nan 开头的函数会进行相应的操作,但是忽略 nan 值。

7、常见的加减乘除四则运算

运算函数
a + badd(a,b)
a - bsubtract(a,b)
a * bmultiply(a,b)
a / bdivide(a,b)
a ** bpower(a,b)
a % bremainder(a,b)

8、比较和逻辑运算

运算函数<
==equal
!=not_equal
>greater
>=greater_equal
<less
<=less_equal
 logical_and
 logical_or
 logical_xor
 logical_not
&bitwise_and
 bitwise_or
^bitwise_xor
~invert
>>right_shift
<<left_shift

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值