I have a function which has to replace every element x of numpy array X with f(x).
def modify_inplace(X):
X = 2. / (8. + numpy.exp(-X))
But this doesn't work:
>>> X = numpy.random.random( size=(2,3) )
>>> X
array([[ 0.97476386, 0.76411101, 0.37690288],
[ 0.05462798, 0.44722799, 0.23570353]])
>>>> modify_inplace(X)
>>> X
array([[ 0.97476386, 0.76411101, 0.37690288],
[ 0.05462798, 0.44722799, 0.23570353]])
I know, I can simply return new array, but I wonder if it's possible to modify numpy arrays inplace so that no additional memory will be allocated?
Ashwini Chaudhary has provided a solution below, but this is not exactly what I am looking for, because I need to modify array inplace without any additional malloc.