Python中的 numpy.invert()
numpy.invert()函数用于计算数组元素的按位反转。它计算输入数组中整数的底层二进制表示的按位 NOT。
对于有符号整数输入,返回二进制补码。在二进制补码系统中,负数由绝对值的二进制补码表示。
Syntax : numpy.invert(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘invert’)
Parameters :
x : [array_like] Input array.
out : [ndarray, optional] A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
**kwargs : Allows you to pass keyword variable length of argument to a function. It is used when we want to handle named argument in a function.
where : [array_like, optional] True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.
Return : [ndarray or scalar] Result. This is a scalar if x is scalar.
代码#1:工作
# Python program explaining
# invert() function
import numpy as geek
in_num = 10
print ("Input number : ", in_num)
out_num = geek.invert(in_num)
print ("inversion of 10 : ", out_num)
输出 :
Input number : 10
inversion of 10 : -11
代码#2:
# Python program explaining
# invert() function
import numpy as geek
in_arr = [2, 0, 25]
print ("Input array : ", in_arr)
out_arr = geek.invert(in_arr)
print ("Output array after inversion: ", out_arr)
输出 :
Input array : [2, 0, 25]
Output array after inversion: [ -3 -1 -26]
代码#3:
# Python program explaining
# invert() function
import numpy as geek
in_arr = [True, False]
print("Input array : ", in_arr)
out_arr = geek.invert(in_arr)
print ("Output array after inversion: ", out_arr)
输出 :
Input array : [True, False]
Output array after inversion: [False True]