The NumPy module of Python provides a method for converting the positive elements of an array to negative elements. The method is called numpy.negative(). Let’s explore the numpy.negative method in further detail.
What is the Numpy.negative() method?
The Numpy.negative() method is a function that takes an array of integers and returns an element-wise negative value of an array.
We can use this method to calculate the negative of an array element.
Numpy is a powerful module for performing mathematical and scientific operations in Python. To use it, first ensure it is installed by typing ‘
pip3 install numpy‘ into your command prompt shell.
Once installed, you can use the ‘numpy.negative()’ function to display respective numerical negatives.
Syntax of numpy.negative()
numpy.negative(a, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'negative'>
The numpy.negative() methods takes these arguments:
- a – This is an input array or an element.
- out – Represents the location where the output of the method is stored.
If location is not provided ‘None’ is used which will return a newly-allocated array.
- where – True value indicates that a universal function should be calculated at the respective position.
- casting – Controls the type of datacasting. casting= ‘same_kind’ indicates casting within the same kind should take place.
- order – Controls the memory layout order of the output function. order =’K’ means reading the elements in the order they occur in memory.
- dtype – Denotes the data type of the array.
- subok – Decides if subclasses should be made or not. If True, subclasses will be passed through.
- ufunc – Stands for a universal function. <ufunc ‘negative’> functions element-by-element.
- Return value – returns the negative value of the input, i.e. y = -x.
The return type is an array depending on the input type. The
negative() method is a universal function.
Examples of Numpy.negative()
The following examples show the different ways numpy.negative() is used in Python.
Example 1: Using Numpy.negative( ) on single variables.
Let’s start with the most basic example. Here we’ve declared two variables ‘x’ and ‘y’ respectively. This code uses NumPy.negative(variable_name) method to create a negative element and prints the corresponding output.
import numpy as np x = 12 y = 99 print(" Original value of x:",x,"\n Negative value of x:",np.negative(x)) print(" Original value of y:",y,"\n Negative value of y:",np.negative(y))
Example 2: Passing Arrays as Input.
This code takes an array of numbers (array1=[10, 20, 30]) and (array2=[9, 8, 7]) then it uses the Numpy.negative() method to create a new array. Observe how negative() prints all the negative values separately as seen in the output window, which reveals that it works element by element.
import numpy as np array1 = np.array([10, 20, 30]) array2 = np.array([9, 8, 7]) print("Original value of array1:",array1,"\nNegative value of array1:",np.negative(array1)) print("Original value of array2:",array2,"\nNegative value of array2:",np.negative(array2))
Example 3: Passing Negative values as Input
Using Numpy.negative(), we check if the array is working on negative numbers. To verify if it works on negative elements, the code creates an array “arr1” of negative elements and one positive element ([-2, -4, -6, -8, 10]. It also creates another variable ‘x’ = -983.
Numpy.negative() returns – Negative input = Positive output irrespective of the input as observed in the output.
import numpy as np arr1 = np.array([-2,-4,-6,-8,10]) x = -983 print("Original value of array:",arr1, "\n Negative value of array:",np.negative(arr1)) print("Original value of x:",x, "\n Negative value of x:",np.negative(x))
Example 4: Checking whether the Input array/elements get updated in the memory.
This code creates an array of numbers (array1=[10,20,30) and variable (x = 12) then for checking it uses NumPy.negative( ) and prints the original inputs. Here, to check whether the inputs are updated in memory or not, we used the id() function, which returns an object’s memory address. Both outputs have different outputs, so we can see that the output of the negative() function is stored in a new array, but the original input is not affected
import numpy as np x = 12 print("Original value of x:",x, "\nNegative value of x:",np.negative(x)) print("Value of x:",x) print("Memory Location of x:",id(x)) print("Memory Location of -x:",id(np.negative(x))) array1 = np.array([10,20,30]) print("Original value of array1:",array1, "\nNegative value of array1:",np.negative(array1)) print("Elements of array1:",array1) print("Memory Location of array1:",id(array1)) print("Memory Location of -array1:",id(np.negative(array1)))
The Numpy.negative() function is used when we want to calculate the negative of array elements. It returns an elementwise negative value of an array. Because it returns a reassigned array of negative elements, the original arrays/inputs are not updated. This method is useful when you want to calculate the negative elements of an array. All parameters out, where, casting, order, dtype, subok, ufunc are already defined in the function, they can be changed according to your needs, but usually they are not used much for negative elements calculation.