如何创建与其他数组相同列数但具有零行的新ndarray

时间:2019-09-03 09:02:44

标签: python numpy numpy-ndarray

假设我有一个形状如下(50、784)的数组

 @using (Html.BeginForm("InsertOrder", "Add", FormMethod.Post))

我想要另一个形状数组(0,784),将用于收集一些匹配某些复杂条件的x_train行。

就像下面的sudo代码所示

x_train = np.ones((50, 784), np.int16)
x_train.shape

我尝试了以下给出形状数组(1,784)的代码

## sudo code
new_x_train = <initialize with array of shape (0,784)> 
for condition in conditions
    new_x_train = new_x_train.r_[new_x_train, elements_matching(condition, x_train)]

然后我删除了第一行,该行给出了我想要的形状数组(0,784)

np.empty_like(x_train[[0]]) # shape (1,784)

还有另一种干净的方法吗?

我知道我可以按照下面的方法做,但是想要多个这样的数组,这会导致很多我想避免的if-else块

np.delete(np.empty_like(x_train[[0]]), [0], axis=0) ## shape (0,784)

1 个答案:

答案 0 :(得分:0)

我只会做类似的事情:

new_x_train = np.r_[[elements_matching(condition, x_train) for condition in conditions]]

即只需列出一个数组列表并在最后放置它们即可。