python - 在 Numpy/Scipy 中切片数组

标签 python numpy scipy

我有一个像这样的数组:

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

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

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

最佳答案

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

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

关于python - 在 Numpy/Scipy 中切片数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2725750/

相关文章:

python - __init__() 得到了一个意外的关键字参数 'instance',带有 inlineformset_factory 的自定义表单集

Python - 使用 scipy 查找函数的零

python - 绘图时的舍入误差

python - python计算两个风向的差异

python - 使用python在sqlite3中存储numpy数组时遇到问题

Python使用pandas将numpy数组插入sqlite3数据库

pandas - hstack csr 矩阵与 pandas 数组

python - 在 Pandas 中按周分组

python - PyCharm 和 Python 日志记录的正确工作流程

python - 如何计算多类图像分割的多类骰子系数?