Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取numpy矩阵的前n列_Python_Numpy - Fatal编程技术网

Python 提取numpy矩阵的前n列

Python 提取numpy矩阵的前n列,python,numpy,Python,Numpy,我有这样一个数组: array([[-0.57098887, -0.4274751 , -0.38459931, -0.58593526], [-0.22279713, -0.51723555, 0.82462029, 0.05319973], [ 0.67492385, -0.69294472, -0.2531966 , 0.01403201], [ 0.41086611, 0.26374238, 0.32859738, -0

我有这样一个数组:

  array([[-0.57098887, -0.4274751 , -0.38459931, -0.58593526],
         [-0.22279713, -0.51723555,  0.82462029,  0.05319973],
         [ 0.67492385, -0.69294472, -0.2531966 ,  0.01403201],
         [ 0.41086611,  0.26374238,  0.32859738, -0.80848795]])
现在我想摘录以下内容:

   [-0.57098887, -0.4274751]
   [-0.22279713, -0.51723555]
   [ 0.67492385, -0.69294472]
   [ 0.41086611,  0.26374238]

所以基本上只有前两列。

如果
a
是您的数组:

In [11]: a[:,:2]
Out[11]: 
array([[-0.57098887, -0.4274751 ],
       [-0.22279713, -0.51723555],
       [ 0.67492385, -0.69294472],
       [ 0.41086611,  0.26374238]])

我知道这是一个很老的问题-

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
比方说,您希望提取前2行和前3列

A_NEW = A[0:2, 0:3]
A_NEW = [[1, 2, 3],
         [4, 5, 6]]
理解语法

A_NEW = A[start_index_row : stop_index_row, 
          start_index_column : stop_index_column)]
如果需要第2行、第2列和第3列

A_NEW = A[1:2, 1:3]

请参考numpy索引和切片文章-

numpy文档:。总是先检查文档。@JoelCornett:谢谢。。因此,切片是一个术语。。如果你知道这个概念,但不知道这个术语,那就很难了……)非常感谢:)