What is numpy.negative() in Python?

Python’s numpy.negative() method computes the negative of a number or array element-wise.

Syntax

numpy.negative() is declared as shown below:

numpy.negative(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'negative'>

A universal function (ufunc) is a function that operates on ndarrays in an element-by-element fashion. The negative() method is a universal function.

Parameters

The numpy.negative() method takes the following compulsory parameters:

  • x [array_like or scalar] - this is the input array.

The numpy.negative() method takes the following optional parameters:

Parameter

Description

out

Represents the location into which the output of the method is stored. If not provided or None, a freshly-allocated array is returned.

where

True value indicates that a universal function should be calculated at this position.

casting

Controls the type of datacasting that should occur. The same_kind option indicates that safe casting or casting within the same kind should take place. 

order

Controls the memory layout order of the output function. The option K means reading the elements in the order they occur in memory.

dtype

Represents the desired data type of the array.

subok

Decides if subclasses should be made or not. If True, subclasses will be passed through. 

Return value

numpy.negative() returns the negative of the input, i.e. y = -x. The return type is an array or scalar depending on the input type.

Examples

The examples below show the different ways numpy.negative() is used in Python.

Negative of numbers

The code below outputs the negative value of the numbers 17.5 and 12. The result is shown below:

import numpy as np
a = 17.5
b = 20
print (np.negative(a))
print (np.negative(b))

Negative of arrays

The example below outputs the negative of arrays arr1 and arr2:

import numpy as np
arr1 = np.array([20,-30,40])
arr2 = np.array([2,-3,4])
print(np.negative(arr1))
print(np.negative(arr2))

The example below outputs the negative of arrays arr3 and arr4:

import numpy as np
arr3 = np.array([[2.5,100,-10], [-2.9,90,89]])
arr4 = np.array([[-2,-3,-4], [30,40,50]])
print(np.negative(arr3))
print(np.negative(arr4))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved