Numpy modify array in place?

前端 未结 4 2103
南方客
南方客 2020-11-27 19:09

I have the following code which is attempting to normalize the values of an m x n array (It will be used as input to a neural network, where m is t

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 19:41

    This is a trick that it is slightly more general than the other useful answers here:

    def normalize(array, imin = -1, imax = 1):
        """I = Imin + (Imax-Imin)*(D-Dmin)/(Dmax-Dmin)"""
    
        dmin = array.min()
        dmax = array.max()
    
        array[...] = imin + (imax - imin)*(array - dmin)/(dmax - dmin)
    

    Here we are assigning values to the view array[...] rather than assigning these values to some new local variable within the scope of the function.

    x = np.arange(5, dtype='float')
    print x
    normalize(x)
    print x
    
    >>> [0. 1. 2. 3. 4.]
    >>> [-1.  -0.5  0.   0.5  1. ]
    

    EDIT:

    It's slower; it allocates a new array. But it may be valuable if you are doing something more complicated where builtin in-place operations are cumbersome or don't suffice.

    def normalize2(array, imin=-1, imax=1):
        dmin = array.min()
        dmax = array.max()
    
        array -= dmin;
        array *= (imax - imin)
        array /= (dmax-dmin)
        array += imin
    
    A = np.random.randn(200**3).reshape([200] * 3)
    %timeit -n5 -r5 normalize(A)
    %timeit -n5 -r5 normalize2(A)
    
    >> 47.6 ms ± 678 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)
    >> 26.1 ms ± 866 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)
    

提交回复
热议问题