【问题标题】:Numpy - extract array2d columns based on arrayNumpy - 根据数组提取 array2d 列
【发布时间】:2020-02-29 18:24:42
【问题描述】:

我有一个形状为 (40, 926) 的多维 numpy 数组。 我还有一个 numpy 数组形状 (126),其中包含表示有效列的随机索引。

现在,我想做的是根据这个索引掩码过滤我的多维数组:必须删除与这个索引不同的所有列,结果形状 (40,126)

所以,类似:

data = np.array([[]])
sel = np.asarray(idx.split(',')) 
print sel.shape #(126) array 404,410,500...1300
print X.shape #(40,926) 
for x in wave: #(926) array 400,401,402...1325
    for y in sel:
        if float(x) == float(y):
            index = np.where(wave == x)[0][0]
            arr =  X[:,index]
            data = np.append(data, arr, axis=1)
print data.shape #must be (40,126)
return data

发生错误:返回连接((arr,值),轴=轴) ValueError: 所有输入数组的维数必须相同

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    如果 wave 和 sel 数组已经排序,你可以得到 wave where 的索引 wave == sel 并使用它们从矩阵中选择行

    idx = wave.searchsorted(sel)
    data = X[:, idx]
    

    【讨论】:

    • 我要选择不删除
    • 这样更容易
    • 太棒了...但我需要像上面的例子一样与wave相关的sel索引
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 2022-12-10
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多