As part of a larger function, I’m writing some code to generate a vector/matrix (depending on the input) containing the mean value of each column of the input vector/matrix ‘x’. These values are stored in a vector/matrix of the same shape as the input vector.
My preliminary solution for it to work on both a 1-D and matrix arrays is very(!) messy:
# 'x' is of type array and can be a vector or matrix.
import scipy as sp
shp = sp.shape(x)
x_mean = sp.array(sp.zeros(sp.shape(x)))
try: # if input is a matrix
shp_range = range(shp[1])
for d in shp_range:
x_mean[:,d] = sp.mean(x[:,d])*sp.ones(sp.shape(z))
except IndexError: # error occurs if the input is a vector
z = sp.zeros((shp[0],))
x_mean = sp.mean(x)*sp.ones(sp.shape(z))
Coming from a MATLAB background, this is what it would look like in MATLAB:
[R,C] = size(x);
for d = 1:C,
xmean(:,d) = zeros(R,1) + mean(x(:,d));
end
This works on both vectors as well as matrices without errors.
My question is, how can I make my python code work on input of both vector and matrix format without the (ugly) try/except block?
Thanks!
First A quick note about broadcasting in numpy. Broadcasting was kinda confusing to me when I switched over from matlab to python, but once I took the time to understand it I realized how useful it could be. To learn more about broadcasting take a look at http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html,
Because of broadcasting an (m,) array in numpy (what you’re calling a vector) is essentially equivelent to an (1, m) array or (1, 1, m) array and so on. It seems like you want to have an (m,) array behave like a (m, 1) array. I believe this happens sometimes, especially in the linalg module, but if you’re going to do it you should know that you’re breaking the numpy convention.
With that warning there’s the code:
and an example:
This is faster than using
tile, the timing is bellow: