Numpy-ufunc函数

来源:互联网 发布:淘宝店铺0信誉出售 编辑:程序博客网 时间:2024/06/06 02:26

转载请注明出处http://blog.csdn.net/JavaMoo/article/details/78323084

ufunc函数是能对数组的每个元素进行运算的函数,python内置的许多ufunc函数使用C语言编写的,所以运行速度很快

2.2.0 ufunc与math方法性能对比

    print('sin计算后的值没有没有保存在x中,指定out来指定保存计算结果位置')    x = np.linspace(0, 2 * np.pi, 10)    y = np.sin(x, out=x)    print(y)    print(x)    # math.sin的单值运算速度比np.sin的运算速度快,但是运算数组时np.sin比math.sin快    # math.sin返回的是标准的float类型而np.sin返回的是float64类型    a = np.arange(6.0).reshape(2, 3)    # 利用a.item返回标准的python类型    print(type(a.item(1, 2)))

运行结果

sin计算后的值没有没有保存在x中,指定out来指定保存计算结果位置[  0.00000000e+00   6.42787610e-01   9.84807753e-01   8.66025404e-01   3.42020143e-01  -3.42020143e-01  -8.66025404e-01  -9.84807753e-01  -6.42787610e-01  -2.44929360e-16][  0.00000000e+00   6.42787610e-01   9.84807753e-01   8.66025404e-01   3.42020143e-01  -3.42020143e-01  -8.66025404e-01  -9.84807753e-01  -6.42787610e-01  -2.44929360e-16]<class 'float'>

2.2.1四则运算

常用ufunc四则运算

这里写图片描述

print('利用python计算两个数组之和')    a = np.arange(0, 4)    b = np.arange(1, 5)    c = np.add(a, b)    print(a)    print(b)    print(c)    print('计算两个数组相减')    np.subtract(a, b, c)    print(c)    # 数组运算使用不当可能会产生大量的中间结果导致程序运算速度过慢    # x=a*b+c会这样运算t=a*b x=t+c 会产生中间结果t 可以这样改进 x=a*b x+=c

运行结果

利用python计算两个数组之和[0 1 2 3][1 2 3 4][1 3 5 7]计算两个数组相减[-1 -1 -1 -1]

2.2.2 比较运算和布尔运算

a = np.arange(0, 4)    b = np.arange(1, 5)    c = (a > b)    print(c)    a = np.arange(5)    b = np.arange(4, -1, -1)    print(a == b)    print(a > b)    print('使用np.logical_or()运算的例子')    print(np.logical_or(a == b, a > b))    print('any:只要数组中有一个为True则为True,All:必须全为True才能为True')    print(np.any(a == b))    print(np.any(a == b) and np.any(a > b))    # bitwise开头的函数是位运算函数,包括bitwise_and,bitwise_not,bitwise_or和bitwise_xor等    # 也可以用& ~ | ……等操作符进行计算    # 位运算比> <运算符优先级高

运行结果

[False False False False][False False  True False False][False False False  True  True]使用np.logical_or()运算的例子[False False  True  True  True]any:只要数组中有一个为True则为True,All:必须全为True才能为TrueTrueTrue

2.2.3自定义ufunc

x = np.linspace(0, 2, 1000)    # 第自定义函数,自定义函数参数个数,自定义函数返回参数个数    triangle_ufunc1 = np.frompyfunc(triangle_wave, 4, 1)    y2 = triangle_ufunc1(x, 0.6, 0.4, 1.0)    # 返回的数组元素类型是object,使用astype方法转化为双精度    print(y2.astype(np.float).size)    # np.vectorize和np.fromfunc方法功能一样,只不过多了otypes返回参数类型列表    # otypes可以描述多个返回数组的元素类型    triangle_ufunc2 = np.vectorize(triangle_wave, otypes=[np.float])

2.2.4广播

    a = np.arange(0, 60, 10).reshape(-1, 1)    b = np.arange(0, 5)    c = a + b    print('多维数组')    print(c)    print('将a的第一轴长度扩展为5')    a = a.repeat(5, axis=1)    print(a)    print('创建广播运算用的数组')    x, y = np.ogrid[:5, :5]    print(x)    print(y)    print('用切片元组作为下标返回一组可以用来广播计算的数组')    # 带有j代表返回的数组的长度,不带j表示步长    x, y = np.ogrid[:1:4j, :1:3j]    print(x)    print(y)    print('创建广播之后的数组')    x, y = np.mgrid[:5, :5]    print(x)    print(y)

运算结果

多维数组[[ 0  1  2  3  4] [10 11 12 13 14] [20 21 22 23 24] [30 31 32 33 34] [40 41 42 43 44] [50 51 52 53 54]]将a的第一轴长度扩展为5[[ 0  0  0  0  0] [10 10 10 10 10] [20 20 20 20 20] [30 30 30 30 30] [40 40 40 40 40] [50 50 50 50 50]]创建广播运算用的数组[[0] [1] [2] [3] [4]][[0 1 2 3 4]]用切片元组作为下标返回一组可以用来广播计算的数组[[ 0.        ] [ 0.33333333] [ 0.66666667] [ 1.        ]][[ 0.   0.5  1. ]]创建广播之后的数组[[0 0 0 0 0] [1 1 1 1 1] [2 2 2 2 2] [3 3 3 3 3] [4 4 4 4 4]][[0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4]]

2.2.5 ufunc方法

print('ufunc函数对象本身还有一些方法函数,这些方法只对两个输入一个输出的ufunc函数有效')    r1 = np.add.reduce([1, 2, 3])    r2 = np.add.reduce([[1, 2, 3], [4, 5, 6]], axis=1)    print(r1)    print(r2)    print('accumulate方法和reduce类似,只是返回的数组和输入的数组的形状相同,保留中间计算结果')    r1 = np.add.accumulate([1, 2, 3])    r2 = np.add.accumulate([[1, 2, 3], [4, 5, 6]], axis=1)    print(r1)    print(r2)    a = np.array([1, 2, 3, 4])    print('reduceat()方法计算多组reduce()结果,通过indices参数指定一系列的起始和终止位置')    result = np.add.reduceat(a, indices=[0, 1, 0, 2, 0, 3, 0])    print(result)    print('outer方法')    c = np.multiply.outer([1, 2, 3, 4, 5], [2, 3, 4])    print(c)

运行结果

ufunc函数对象本身还有一些方法函数,这些方法只对两个输入一个输出的ufunc函数有效6[ 6 15]accumulate方法和reduce类似,只是返回的数组和输入的数组的形状相同,保留中间计算结果[1 3 6][[ 1  3  6] [ 4  9 15]]reduceat()方法计算多组reduce()结果,通过indices参数指定一系列的起始和终止位置[ 1  2  3  3  6  4 10]outer方法[[ 2  3  4] [ 4  6  8] [ 6  9 12] [ 8 12 16] [10 15 20]]