numpy-ufunc函数

import numpy as np

x = np.linspace(0, 2 * np.pi, 10)
t = np.sin(x, out=x)
print('x;  ', x)  # 值存到了x里面
"""
运 算 符  对应的ufunc 函数
y = x1 + x2  add(x1, x2 [, y])
y = x1 - x2  subtract(x1, x2 [, y])
y = x1 * x2  multiply (x1, x2 [, y])
y = x1 / x2  divide (x1, x2 [, y]), 如果两个数组的元素为整数,那么用整数除法
y = x1 / x2  true_divide (x1, x2 [, y]), 总是返回精确的商
y = x1 // x2  floor_divide (x1, x2 [, y]), 总是对返回值取整
y = -x  negative(x [,y])
y = x1**x2  power(x1, x2 [, y])
y = x1 % x2  remainder(x1, x2 [, y]),或 mod(x1, x2, [, y])
"""
a = np.arange(0, 4)
b = np.arange(1, 5)
print("np.add(a,b,a):   \n", np.add(a, b, a))
print("a:  \n", a)
"""
np.add(a,b,a):   
 [1 3 5 7]
a:  
 [1 3 5 7]
"""

"""
比较运算符  ufunc 函数
y = x1 == x2  equal(x1, x2 [, y])
y = x1 != x2  not_equal(x1, x2 [, y])
y = x1 < x2  less(x1, x2, [, y])
y = x1 <= x2  less_equal(x1, x2, [, y])
y = x1 > x2  greater(x1, x2, [, y])
y = x1 >= x2  greater_equal(x1, x2, [, y])
"""
a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 1, 2, 4, 5])
print("a>b:   \n", a > b)
print("np.greater(a,b,a):  \n", np.greater(a, b, a))
print("a:  \n", a)
"""
a>b:   
 [False  True  True False False]
np.greater(a,b,a):  
 [0 1 1 0 0]
a:  
 [0 1 1 0 0]
"""
# np.logical_and np.logical_not np.logical_or np.logical_xor
a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 1, 2, 4, 5])
print("np.logical_or(a==b, a>b):  \n", np.logical_or(a < b, a > b))  # a!=b
"""
np.logical_or(a==b, a>b):  
 [False  True  True False False]
"""

可以使用数组的 any()或 all()方法。只要数组中有一个值为True,any()就返回True;而只有当数组的全部元素都为True 时,all()才返回True。

猜你喜欢

转载自blog.csdn.net/u014575047/article/details/81182799