Q1) Numpy functions can take arguments in different shapes. For instance, np.sum(V) can take either of two below and return outputs with different shapes.
x1= np.array( [1,3] ) #(1)
x2= np.array([[[1,2],[3,4]], [[5,6],[7,8]]]) #(2)
I am making my own function something like below, which adds two values in an 1D vector with the length of two and return the real number.
def foo(V):
return V[0]+V[1];
However, this foo function can only take one 1D vector and cannot take any other shapes. It can only take x1 above as an argument but not x2. If I want to make my function work with either of two variables above(x1 and x2), or with any other shapes that has arrays with the length of 2 in their last dimension, how should I revise my foo function?
---------------------------update------------------------------
My original function was a hardcoded negative gaussian pdf function.
def nGauss(X, mu, cov):
# multivariate negative gaussian.
# mu is a vector and cov is a covariance matrix.
k = X.shape[0];
dev = X-mu
p1 = np.power( np.power(np.pi * 2, k) , -0.5);
p2 = np.power( np.linalg.det(cov) , -0.5)
p3 = np.exp( -0.5 * np.dot( np.dot(dev.transpose(), np.linalg.inv(cov)), dev));
return -1.0 * p1 * p2 * p3;
Now his function can return only one pdf value. For example, it can only take arguments like np.array([1,2]), but cannot take arguments X like np.array([[[1,2], [5,6]], [[7,8],[9,0]]]). Here my question was how to make my gaussian function takes arguments of arbitrary shapes and return the pdf value of each point maintaining the same structure except the last dimension, such as
nGauss(np.array( [1,2] ), mu, cov)
returns [ 0.000023 ], and
nGauss(np.array([[[1,2], [5,6]], [[7,8],[9,0]]]), mu, cov)
returns [[ 0.000023, 0000014], [0.000012, 0.000042]].
I notice that scipy function 'multivariate_normal.pdf' can do this.
Q2) I am also having a difficulty in understanding np's basic array.
t1=np.array([[1,2,3], [4,5,6]])
t2=np.array([1,2,3])
t3=np.array([[1,2,3], [4,5,6],5])
The shape of t1 is (2,3), and it seems legitimate in terms of matrix perspective; 2 rows and 3 columns. However, the shape of t2 is (3,), which I think has to be (3). What's the meaning of the empty space after "3,"? Also, the shape of t3 is (3,). In this case, is the meaning of the empty space that dimensions vary?
In advance, thanks for your help.