python numpy--矩阵的通用函数

一、概念

通用函数(ufunc)是一种对ndarray中的数据执行元素级运算的函数。你可以将其看作简单函数(接受一个或多个标量值,并产生一个或多个标量值)的矢量化包装器通用函数的输入是一组标量,输出也是一组标量,它们通常可以对应于基本数学运算,如加、减、乘、除等。

二、分类

1.一元函数

它们接受一个数组。返回一个结果数组,当然也能返回两个数组(modf函数),但是这种的不是很常见;
在这里插入图片描述

(1)abs fabs

import numpy as np #导入模块
a = np.mat(np.arange(-4,3)) #创建一个矩阵
np.abs(a) # 对矩阵a取绝对值
np.fabs(a) # 对矩阵a取浮点类的绝对值

(2) sqrt () 平方根 square() 平方

b = np.mat(range(1,6)) #创建一个矩阵
np.sqrt(b) #b的平方根
np.square(b) #b的平方 

(3)log log10 log2 log1p

c = np.mat([1,2,np.e,np.e+1,4,10,100]) #创建一个矩阵
np.log(c) #以e为底
np.log10(c)# log以10为底
np.log2(c)#log2以2为底
np.log1p(c) #在c的基础上每一个值加上一个1,再以e为底的对数 log1p(x)==log(1+x)
np.log1p(np.e-1)     

(4)sign ceil floor rint

d = np.mat([
    [2.3,4.6],
    [1.2,1.8]
]) #创建一个矩阵
np.sign(d)  #符号位 +1:正数 -1:负数 0:0
np.ceil(d) #向上取整 右
np.floor(d)#向下取整 左
np.rint(d) #四舍五入
e = np.mat([
    [1,4,8],
    [2,3,7]
])
# e*0.1 #快速变成浮点数
np.rint(e)#四舍五入的方法也可以

(5)modf 分别返回小数部分和整数部分

这就是输入一个数组,返回两个数组的函数

arr1,arr2=np.modf(d)
#arr1 返回的是小数部分,arr2返回的是整数部分

(6)isnan() 判断不是数字

nan: not a number

f=np.array([1,2,np.nan,np.nan,3]) #创建一个矩阵 不是数字的就转换为np.nan  np.inf 是无穷大,是个数字类型
np.isnan(f)

(7)cos sin tan

g=np.mat([0,np.pi/4,np.pi/2,np.pi*3/4])  #创建一个矩阵,里面表示的是角度
g*2 #所有的角度都放大2倍
np.cos(g) # 求角度的cos值
np.set_printoptions(precision=3)#科学计数法设置显示3位小数,作为了解吧!
np.tan(g) #求角度的tan值

(8)logical_not

import numpy as np
a = np.mat(np.arange(-4,3))
print(a)
b = np.logical_not(a)
print(b)

只有0为真,其他都为假
在这里插入图片描述

2.二元函数

它们接受两个数组,并返回一个结果数组。
在这里插入图片描述

#准备三个矩阵
a = np.mat([1,2,3,4])
b = np.mat([5,6,7,8])
c = np.mat([9,10,11,12])

(1)power() 求幂

np.power(b,a) #矩阵本身是二维的,有人问为什么返回的结果是两个中括号
np.power(b,2)

(2)maximum、minimum 元素级运算

如果两个矩阵的元素不一样多的话则会报错

#准备两个矩阵
arr1 = np.mat([1,8,2,9])
arr2 = np.mat([6,3,5,4])
np.maximum(arr1,arr2) 
matrix([[6, 8, 5, 9]])

返回的是两个数组中对应位大的数值。

np.minimum(arr1,arr2)
matrix([[1, 3, 2, 4]])

返回的是两个数组中对应位小的数值

(3)greater 大于 ,greater_equal 大于等于

得到的是布尔矩阵或则数组

np.greater(arr1,arr2)
matrix([[False,  True, False,  True]])

(4)逻辑"与":logical_and ,“或”:logical_or,“非”:logical_xor

在python中非0为真

#准备一个矩阵
d = np.mat('2 0;1 0')
e = np.mat('0 2;1 0')
#与
np.logical_and(d,e)  #对应位都为真,结果为真,否则为假
matrix([[False, False],
        [ True, False]])
#或
np.logical_or(d,e) #对应位其中一位为真则为真,都为假则为假
matrix([[ True,  True],
        [ True, False]])
#非
np.logical_xor(d,e)  #相同为false ,不同是true
matrix([[ True,  True],
        [False, False]])

三、自定义通用函数

步骤:
step1:定义并设置函数内容
step2:使用np.frompyfunc(函数名,输入参数个数 Int ,输出值的个数 int)创建通用函数

1、自定义函数1,简单定义写个代码

# 写一个通用函数 返回与参数结构相同的zero矩阵
#step1:
def copyshape(a):
    return np.zeros_like(a)
#step2:
ucopyshape = np.frompyfunc(copyshape,1,1)
#step3:使用函数
f  = np.mat('1,2;3,4') #创建一个2*2的矩阵
ucopyshape(f)   #返回的是与f矩阵相同结构2*2的值为0 的矩阵
matrix([[0, 0],
        [0, 0]], dtype=object)

2、自定义函数2,返回所有元素的平方,传入一个参数,输出一个参数

# step1:
def square(a):   # 定义函数名和参数
    return a**2 # 返回参数的平方
#step2
usquare = np.frompyfunc(square,1,1)  #使用该函数创建通用函数,传入一个参数,输出一个参数
#step3:使用这个通用函数
usquare(np.mat('1 3 5 7'))
matrix([[1, 9, 25, 49]], dtype=object)

3、自定义函数3,返回两个矩阵对应位的平方和,传入2个,输出1个

 # step1
def square_add(a,b):
    return a**2 + b**2
#step2
usquare_add = np.frompyfunc(square_add,2,1)
#step3:使用参数
g1 = np.mat('1 2 3 4')
g2 = np.mat('6 5 4 3')
usquare_add(g1,g2)

4、自定义函数4,返回一个矩阵的平方 第二个矩阵的立方 传入两个参数,输出2个参数

# step1
def square_cubic(a,b):
    return a**2,b**3
#step2
usquare_cubic = np.frompyfunc(square_cubic,2,2)
#step3:使用函数
a,b = usquare_cubic(np.mat('1 2 3'),np.mat('4 5 6')) #因为输出的是2个,所以放2个变量来进行存储

四、numpy中已有的通用函数

有四种:
在这里插入图片描述

1…add.accumulate()

递归作用于输入的数组,将运算的中间结果返回
axis决定方向

a = np.arange(9) #准备一个数组
np.add.accumulate(a)
array([ 0,  1,  3,  6, 10, 15, 21, 28, 36], dtype=int32)

以下加入axis:
axis=0表述列
axis=1表述行

a.reshape(3,3) #把数组a变成3*3的数组
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
np.add.accumulate(a.reshape(3,3),axis=0) #竖着加
array([[ 0,  1,  2],
       [ 3,  5,  7],
       [ 9, 12, 15]], dtype=int32)
np.add.accumulate(a.reshape(3,3),axis=1) #横着加
array([[ 0,  1,  3],
       [ 3,  7, 12],
       [ 6, 13, 21]], dtype=int32)

2.add.reduce() 求和

axis决定方向

a数组是:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])

np.add.reduce(a) #参数要是一个数组,矩阵就不适用了
36

以下加入axis:

b=np.arange(12).reshape(3,4) #准备一个3行4列的数组

array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

np.add.reduce(b,axis=0) #竖着加
array([12, 15, 18, 21])
np.add.reduce(b,axis=1) #横着加
array([ 6, 22, 38])

3.add.reduceat()

需要输入数组以及索引值列表作为参数 按照区间计算方式进行求和
a数组是:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])

#相当于sum(a[0:5]) sum(s[5]) sum(a[2:7]) sum(a[7:])
np.add.reduceat(a,[0,5,2,7])
array([10,  5, 20, 15], dtype=int32)

4.add.outer(a,b)

a的每一个值加上b的所有值,作用于第一个参数的元素以及第二个参数的整体

a数组是:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])

c = np.array([1,3,5,7])
np.add.outer(c,a)  # c的每一个值加上a的所有值
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 3,  4,  5,  6,  7,  8,  9, 10, 11],
       [ 5,  6,  7,  8,  9, 10, 11, 12, 13],
       [ 7,  8,  9, 10, 11, 12, 13, 14, 15]])
np.add.outer(a,c) #a的每一个值加上c的所有值
array([[ 1,  3,  5,  7],
       [ 2,  4,  6,  8],
       [ 3,  5,  7,  9],
       [ 4,  6,  8, 10],
       [ 5,  7,  9, 11],
       [ 6,  8, 10, 12],
       [ 7,  9, 11, 13],
       [ 8, 10, 12, 14],
       [ 9, 11, 13, 15]])
np.add.outer(np.mat('1,2;3,4'),np.mat('4 5;6 7'))  #返回的是一个数组
array([[[[ 5,  6],
         [ 7,  8]],

        [[ 6,  7],
         [ 8,  9]]],


       [[[ 7,  8],
         [ 9, 10]],

        [[ 8,  9],
         [10, 11]]]])

猜你喜欢

转载自blog.csdn.net/qq_43287650/article/details/82861197