如何过滤ndarray中的特定列?

时间:2019-05-28 10:48:44

标签: multidimensional-array rust

如果要在NumPy中使用特定的列,可以执行data[:, columnslist]。例如,如果您想要第1列和第9列

data[:, [1, 9]]

如何使用Rust的ndarray做到这一点?我经历过ndarray for NumPy users,但他们没有任何类似的例子。下面是我尝试过的。

let a = arr2(&[[1., 2., 3.], [4., 5., 6.]]);
let b = stack(Axis(0), &[a.column(1).view(), a.column(2).view()]).unwrap();
let b = Array::from_iter(b.iter());
let b = b.reshape((2, 2));

出现错误:

error[E0277]: the trait bound `ndarray::OwnedRepr<&{float}>: ndarray::data_traits::DataShared` is not satisfied
   --> src/main.rs:143:15
    |
143 |     let b = b.reshape((2,2));
    |               ^^^^^^^ the trait `ndarray::data_traits::DataShared` is not implemented for `ndarray::OwnedRepr<&{float}>`

1 个答案:

答案 0 :(得分:2)

根据the ndarray docsreshape仅可用于ArcArray。对于任何其他数组,请使用into_shape

let b = b.into_shape((2, 2));
相关问题