Execute numpy exp function in place

As in the title, I need to execute numpy.expon a very large ndarray, say arand save the result in ar. Can this operation be performed on site?

+4
source share
2 answers

You can use an optional argument out exp:

a = np.array([3.4, 5])
res = np.exp(a, a)
print(res is a)
print(a)

Conclusion:

True
[  29.96410005  148.4131591 ]

exp (x [, out])

Calculate the exponent of all elements of the input array.

Returns

out: ndarray An output array indicative of the element x.

Here, all elements awill be replaced by the result exp. The return value is the ressame as a. New array not created

+7

, , int32, int, int64 .., TypeError. , - float64 float32 .., exp ,

In [12]: b
Out[12]: array([1, 2, 3, 4, 5], dtype=int32)

In [13]: np.exp(b, b)
--------------------------------------------------------------------------
TypeError: ufunc 'exp' output (typecode 'd') could not be coerced to provided 
output parameter (typecode 'i') according to the casting rule ''same_kind''

Casting exp:

# in-place typecasting
In [14]: b = b.astype(np.float64, copy=False)
In [15]: b
Out[15]: array([ 1.,  2.,  3.,  4.,  5.], dtype=float64)

# modifies b in-place
In [16]: np.exp(b, b)
Out[16]: array([   2.718,    7.389,   20.086,   54.598,  148.413], dtype=float64)
+3

Source: https://habr.com/ru/post/1664320/


All Articles