简化行和列提取,numpy [英] Simplfy row AND column extraction, numpy

查看:73
本文介绍了简化行和列提取,numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用单个奇特"切片从矩阵中提取行和列,这可能吗?

I wish to extract rows and columns from a matrix using a single "fancy" slice, is this possible?

m = matrix([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

我的目标是

matrix([[1, 3],
        [7, 9]])

哪里有我想要的物品清单

Where I have a list of the items I want

d = [0,2]

我可以通过以下方式实现功能

I can achieve the functionality by

m[d][:,d]

但是有没有更简单的表达方式?

But is there a simpler expression?

推荐答案

您可以使用

You can do this using numpy.ix_:

m = matrix([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

d = [0,2]
print m[ix_(d,d)]

它将发出:

[[1 3]
 [7 9]]

这篇关于简化行和列提取,numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆