应该是不正确的输出 np.reshape 函数
supposedly incorrect output np.reshape function
我有一个名为
“foto_dct”,形状为 (16,16,8,8),表示 8x8 的 16x16 矩阵。
当我打印 foto_dct[0,15] 时,作为第一行的最后一个矩阵,我得到:
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
当我做 foto_dct_big = np.reshape(foto_dct,(128,128))
并打印 foto_dct_big 我明白了:
[[ 0 0 0 ... 49 148 245]
[ 0 16 0 ... 10 0 3]
[ 1 4 3 ... 148 137 128]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
如您所见,右上角(应该是上面所有为零的矩阵)被替换为不同的值。
这只是为了证明我确实得到了不同的值,矩阵的其他部分也是错误的。
为什么会发生这种情况,我该如何解决?
亲切的问候。
使用重塑时,维度的顺序很重要,因为它决定了矩阵元素的读取方式。如 documentation, by default the last dimension is the one the fastest then follows the second to last and so on. So in your case when you do the reshape two 8x8 will be read first and reshaped into a row of your 128x128 matrix. For the read to be made in the correct order you first have to swap the dimensions to have the dimensions relating to the rows together (that is the rows of the 8x8 matrix and rows of the 16x16 matrix), same with the columns. You can do this with np.transpose.
中指定
我还没有测试过,但我相信这应该有效
a = np.transpose(a, (0, 2, 1, 3)) # The new shape is (16, 8, 16, 8)
a = np.reshape(a, (128, 128))
我有一个名为 “foto_dct”,形状为 (16,16,8,8),表示 8x8 的 16x16 矩阵。
当我打印 foto_dct[0,15] 时,作为第一行的最后一个矩阵,我得到:
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
当我做 foto_dct_big = np.reshape(foto_dct,(128,128)) 并打印 foto_dct_big 我明白了:
[[ 0 0 0 ... 49 148 245]
[ 0 16 0 ... 10 0 3]
[ 1 4 3 ... 148 137 128]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
如您所见,右上角(应该是上面所有为零的矩阵)被替换为不同的值。 这只是为了证明我确实得到了不同的值,矩阵的其他部分也是错误的。
为什么会发生这种情况,我该如何解决?
亲切的问候。
使用重塑时,维度的顺序很重要,因为它决定了矩阵元素的读取方式。如 documentation, by default the last dimension is the one the fastest then follows the second to last and so on. So in your case when you do the reshape two 8x8 will be read first and reshaped into a row of your 128x128 matrix. For the read to be made in the correct order you first have to swap the dimensions to have the dimensions relating to the rows together (that is the rows of the 8x8 matrix and rows of the 16x16 matrix), same with the columns. You can do this with np.transpose.
中指定我还没有测试过,但我相信这应该有效
a = np.transpose(a, (0, 2, 1, 3)) # The new shape is (16, 8, 16, 8)
a = np.reshape(a, (128, 128))