Extracting specific columns in numpy array

前端 未结 9 1294
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:27

This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. He

9条回答
  •  悲&欢浪女
    2020-11-28 02:38

    Just:

    >>> m = np.matrix(np.random.random((5, 5)))
    >>> m
    matrix([[0.91074101, 0.65999332, 0.69774588, 0.007355  , 0.33025395],
            [0.11078742, 0.67463754, 0.43158254, 0.95367876, 0.85926405],
            [0.98665185, 0.86431513, 0.12153138, 0.73006437, 0.13404811],
            [0.24602225, 0.66139215, 0.08400288, 0.56769924, 0.47974697],
            [0.25345299, 0.76385882, 0.11002419, 0.2509888 , 0.06312359]])
    >>> m[:,[1, 2]]
    matrix([[0.65999332, 0.69774588],
            [0.67463754, 0.43158254],
            [0.86431513, 0.12153138],
            [0.66139215, 0.08400288],
            [0.76385882, 0.11002419]])
    

    The columns need not to be in order:

    >>> m[:,[2, 1, 3]]
    matrix([[0.69774588, 0.65999332, 0.007355  ],
            [0.43158254, 0.67463754, 0.95367876],
            [0.12153138, 0.86431513, 0.73006437],
            [0.08400288, 0.66139215, 0.56769924],
            [0.11002419, 0.76385882, 0.2509888 ]])
    

提交回复
热议问题