Function application over numpy's matrix row/column

后端 未结 4 1285
夕颜
夕颜 2020-12-02 13:36

I am using Numpy to store data into matrices. Coming from R background, there has been an extremely simple way to apply a function over row/columns or both of a matrix.

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 13:38

    I also come from a more R background, and bumped into the lack of a more versatile apply which could take short customized functions. I've seen the forums suggesting using basic numpy functions because many of them handle arrays. However, I've been getting confused over the way "native" numpy functions handle array (sometimes 0 is row-wise and 1 column-wise, sometimes the opposite).

    My personal solution to more flexible functions with apply_along_axis was to combine them with the implicit lambda functions available in python. Lambda functions should very easy to understand for the R minded who uses a more functional programming style, like in R functions apply, sapply, lapply, etc.

    So for example I wanted to apply standardisation of variables in a matrix. Tipically in R there's a function for this (scale) but you can also build it easily with apply:

    (R code)

    apply(Mat,2,function(x) (x-mean(x))/sd(x) ) 
    

    You see how the body of the function inside apply (x-mean(x))/sd(x) is the bit we can't type directly for the python apply_along_axis. With lambda this is easy to implement FOR ONE SET OF VALUES, so:

    (Python)

    import numpy as np
    vec=np.random.randint(1,10,10)  # some random data vector of integers
    
    (lambda x: (x-np.mean(x))/np.std(x)  )(vec)
    

    Then, all we need is to plug this inside the python apply and pass the array of interest through apply_along_axis

    Mat=np.random.randint(1,10,3*4).reshape((3,4))  # some random data vector
    np.apply_along_axis(lambda x: (x-np.mean(x))/np.std(x),0,Mat )
    

    Obviously, the lambda function could be implemented as a separate function, but I guess the whole point is to use rather small functions contained within the line where apply originated.

    I hope you find it useful !

提交回复
热议问题