用Python切片矩阵

时间:2015-06-22 12:28:05

标签: python numpy matrix

我需要将12 * 12矩阵切成24 2 * 3个。输入矩阵是:

arr = [
[1,0,1,1,1,0,1,1,1,0,1,0],
[0,0,0,1,0,1,1,1,1,0,1,0],
[0,1,1,0,0,0,1,0,1,0,0,0],
[1,0,0,1,0,0,1,1,1,0,1,1],
[0,0,0,1,0,0,0,1,1,1,1,0],
[0,0,0,0,0,1,1,0,0,0,0,1],
[1,0,1,0,0,0,1,1,0,0,1,1],
[0,0,1,1,0,1,0,1,1,0,1,0],
[0,1,0,0,0,0,1,0,1,0,0,1],
[1,1,0,1,0,1,0,1,0,1,0,0],
[0,0,1,1,1,1,0,1,0,1,1,1],
[0,0,0,0,1,0,0,0,1,1,0,0]]

我尝试用numpy Matrix实现任务:

from sympy import Matrix
Matrix(arr)[:3,:2]

但它只会从原始矩阵中提供一个切片。

Matrix([
[1, 0],
[0, 0],
[0, 1]])

将12 * 12矩阵切成2 * 3块的便捷方法是什么?我还需要尺寸为原件尺寸的3 * 2,但是假设第一个尺寸准备就绪后很容易。

4 个答案:

答案 0 :(得分:3)

您可以使用numpy.reshape()函数或直接将numpy矩阵的形状从(12,12)更改为(24,3,2),这样可以为您提供所需的结果。

示例 -

In [25]: arr
Out[25]: 
[[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0],
 [0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0],
 [0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0],
 [1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1],
 [0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0],
 [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1],
 [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1],
 [0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
 [0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1],
 [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0],
 [0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1],
 [0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0]]

In [26]: n = np.array(arr)

In [28]: n.shape
Out[28]: (12, 12)

In [29]: n.shape = (24,3,2)

In [30]: n
Out[30]: 
array([[[1, 0],
        [1, 1],
        [1, 0]],

       [[1, 1],
        [1, 0],
        [1, 0]],

       [[0, 0],
        [0, 1],
        [0, 1]],

       [[1, 1],
        [1, 0],
        [1, 0]],
   .
   .
   .

答案 1 :(得分:1)

arr = [
[1,0,1,1,1,0,1,1,1,0,1,0],
[0,0,0,1,0,1,1,1,1,0,1,0],
[0,1,1,0,0,0,1,0,1,0,0,0],
[1,0,0,1,0,0,1,1,1,0,1,1],
[0,0,0,1,0,0,0,1,1,1,1,0],
[0,0,0,0,0,1,1,0,0,0,0,1],
[1,0,1,0,0,0,1,1,0,0,1,1],
[0,0,1,1,0,1,0,1,1,0,1,0],
[0,1,0,0,0,0,1,0,1,0,0,1],
[1,1,0,1,0,1,0,1,0,1,0,0],
[0,0,1,1,1,1,0,1,0,1,1,1],
[0,0,0,0,1,0,0,0,1,1,0,0]]

from sympy import Matrix
row_skip = 3
column_skip = 2

for i in xrange(0, len(arr), row_skip):
    for j in xrange(0, len(arr[0]), column_skip):
        print Matrix(arr)[i:i+row_skip, j:j+column_skip]

输出:

Matrix([[1, 0], [0, 0], [0, 1]])
Matrix([[1, 1], [0, 1], [1, 0]])
Matrix([[1, 0], [0, 1], [0, 0]])
Matrix([[1, 1], [1, 1], [1, 0]])
Matrix([[1, 0], [1, 0], [1, 0]])
Matrix([[1, 0], [1, 0], [0, 0]])
Matrix([[1, 0], [0, 0], [0, 0]])
Matrix([[0, 1], [0, 1], [0, 0]])
Matrix([[0, 0], [0, 0], [0, 1]])
Matrix([[1, 1], [0, 1], [1, 0]])
Matrix([[1, 0], [1, 1], [0, 0]])
Matrix([[1, 1], [1, 0], [0, 1]])
Matrix([[1, 0], [0, 0], [0, 1]])
Matrix([[1, 0], [1, 1], [0, 0]])
Matrix([[0, 0], [0, 1], [0, 0]])
Matrix([[1, 1], [0, 1], [1, 0]])
Matrix([[0, 0], [1, 0], [1, 0]])
Matrix([[1, 1], [1, 0], [0, 1]])
Matrix([[1, 1], [0, 0], [0, 0]])
Matrix([[0, 1], [1, 1], [0, 0]])
Matrix([[0, 1], [1, 1], [1, 0]])
Matrix([[0, 1], [0, 1], [0, 0]])
Matrix([[0, 1], [0, 1], [1, 1]])
Matrix([[0, 0], [1, 1], [0, 0]])

您可以根据需要更改行跳过和列跳过

答案 2 :(得分:0)

这样做。我觉得有一个更优雅的解决方案:

import numpy as np
a=np.array(arr) # also works with a=np.matrix(arr)
[np.split(x,4) for x in np.split(a,6,axis=1)]

使用重塑更好,因为@Anand的答案。

答案 3 :(得分:0)

在raw python中你可以使用列表表达式。

你会读一本书(从右到左,从上到下):

width = 2
height = 3
[[arr[h+i][w:w+width] for i in range(height)] for h in range(0, len(arr), height) for w in range(0, len(arr), width)]

首先从上到下从右到左:

[[arr[h+i][w:w+width] for i in range(height)] for w in range(0, len(arr), width) for h in range(0, len(arr), height)]