1

With a self defined function:

    def myfunc(x1,x2, ... ,x10):                 # list
        ... 
        matrix operation on x1, x2 ..., x10      
        ...
        return X                                 # one value

What is the right way to use array operation to transfer A to B by calling myfunc() like this:

    A.myfunc() >> B

1 Answer 1

1
import copy
def myfunc(x1,x2, ... ,x10):                 # list
    ... 
    matrix operation on copy.deepcopy(x1), copy.deepcopy(x2) ..., copy.deepcopy(x10)      
    ...
    return X      

B = myfunc(a,b,c,d...)

I think ... is what you are looking for

arrays are mutable so in your matrix operation you are likely modifying the original arrays... it sounds like you just want to return a new dataset without modifying the existing x1..x10 arrays

of coarse there is probably a 75% chance I didnt understand what you were asking ...

Sign up to request clarification or add additional context in comments.

4 Comments

oops! I missed something important: A is a 4-dimensional array, B is a 3-dimensional array. I could use myfunc() with 3 loops to generate B from A. But I wonder whether there is a method to reduce the dimension of A from 4 to 3 by calling myfunc()
post A and B ... but small size example ... I dont understand what your input is nor your expected output
Thanks a lot for your quick response! Just make it simpler: A is 3 dimension (5*5*10), B is 2 dimension (5*5), myfunc() is a multiple linear regression function which returns a single value from 10 input values. So for each A[i,j,:] could generate a B[i,j] by calling myfunc() using iterative method: for i in range(5): / for j in range(5): / B[i,j] = myfunc(A[i,j,:]). But is it possible to call myfunc() in matrix without loops?
For example: A = np.random.randn(5,5,10), C = A.sum(axis = 2) would return a 5*5 array, how to do a similar way to let B = A.myfunc(axis=2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.