在 Numpy/Scipy 中切片数组 [英] Slicing arrays in Numpy / Scipy

查看:24
本文介绍了在 Numpy/Scipy 中切片数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组:

a = array([[1,2,3],[3,4,5],[4,5,6]])

从中切出只有前两列a"的 1x2 数组的最有效方法是什么?

array([[2,3],[4,5],[5,6]]) 在这种情况下.

解决方案

二维 numpy 数组使用 a[i,j](不是 a[i][j]),但是您可以对 numpy 数组和矩阵使用与 Python 中的普通矩阵相同的切片符号(只需将它们放在一个 [] 中):

<前>>>> 从 numpy 导入数组>>> a = 数组([[1,2,3],[3,4,5],[4,5,6]])>>> a[:,1:]数组([[2, 3],[4, 5],[5, 6]])

I have an array like:

a = array([[1,2,3],[3,4,5],[4,5,6]])

What's the most efficient way to slice out a 1x2 array out of this that has only the first two columns of "a"?

i.e.

array([[2,3],[4,5],[5,6]]) in this case.

解决方案

Two dimensional numpy arrays are indexed using a[i,j] (not a[i][j]), but you can use the same slicing notation with numpy arrays and matrices as you can with ordinary matrices in python (just put them in a single []):

>>> from numpy import array
>>> a = array([[1,2,3],[3,4,5],[4,5,6]])
>>> a[:,1:]
array([[2, 3],
       [4, 5],
       [5, 6]])

这篇关于在 Numpy/Scipy 中切片数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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