标签:04 rowCount int 08 height width colCount 2021 array
利用Numpy分割矩阵
应用场景:滑动窗口计算
import numpy as np
import re
def window(array, width, height):
"""
将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([i for i in range(1, 82)]).reshape(9, 9) / 1.0, 3, 3)
运行结果
[[[[ 1. 2. 3.]
[10. 11. 12.]
[19. 20. 21.]]
[[ 4. 5. 6.]
[13. 14. 15.]
[22. 23. 24.]]
[[ 7. 8. 9.]
[16. 17. 18.]
[25. 26. 27.]]]
[[[28. 29. 30.]
[37. 38. 39.]
[46. 47. 48.]]
[[31. 32. 33.]
[40. 41. 42.]
[49. 50. 51.]]
[[34. 35. 36.]
[43. 44. 45.]
[52. 53. 54.]]]
[[[55. 56. 57.]
[64. 65. 66.]
[73. 74. 75.]]
[[58. 59. 60.]
[67. 68. 69.]
[76. 77. 78.]]
[[61. 62. 63.]
[70. 71. 72.]
[79. 80. 81.]]]]
标签:04,rowCount,int,08,height,width,colCount,2021,array 来源: https://blog.csdn.net/weixin_44092702/article/details/120750091
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。