def center_matrix(M, dim=0): """ Return the matrix M with each row having zero mean and unit std if dim=1, center columns rather than rows """ # todo: implement this w/o loop. Allow optional arg to specify # dimension to remove the mean from if dim==1: M = transpose(M) M = array(M, Float) if len(M.shape)==1 or M.shape[0]==1 or M.shape[1]==1: M = M-mean(M) sigma = std(M) if sigma>0: M = divide(M, sigma) if dim==1: M=transpose(M) return M for i in range(M.shape[0]): M[i] -= mean(M[i]) sigma = std(M[i]) if sigma>0: M[i] = divide(M[i], sigma) if dim==1: M=transpose(M) return M
def center_matrix(M, dim=0): """ Return the matrix M with each row having zero mean and unit std if dim=1, center columns rather than rows """ # todo: implement this w/o loop. Allow optional arg to specify # dimension to remove the mean from if dim == 1: M = transpose(M) M = array(M, Float) if len(M.shape) == 1 or M.shape[0] == 1 or M.shape[1] == 1: M = M - mean(M) sigma = std(M) if sigma > 0: M = divide(M, sigma) if dim == 1: M = transpose(M) return M for i in range(M.shape[0]): M[i] -= mean(M[i]) sigma = std(M[i]) if sigma > 0: M[i] = divide(M[i], sigma) if dim == 1: M = transpose(M) return M