利用Numpy分割矩阵
应用场景:滑动窗口计算
import numpy as np
import re
def window(array, width, height):
"""
将array分割成若干小窗口
"""
print(array)
# 判断是否符合条件
_rowCount, _colCount = array.shape
if _rowCount % height or _colCount % width:
raise ValueError('please input suitable width/height value')
# 计算最终生成shape
rowCount, colCount = int(_rowCount / height), int(_colCount / width)
# 字节数
bytesN = int(int(re.match('[a-zA-Z]+(\d+)', str(array.dtype)).group(1)) / 8)
# 生成目标矩阵
target = np.lib.stride_tricks.as_strided(array, shape=(rowCount, colCount, height, width), strides=(height * _colCount * bytesN, width * bytesN, *array.strides))
print(target)
return target
window(np.array