将 2D 矩阵改造成 3D 矩阵? (x, y) -> (x/72,72,y)
Rehaspe a 2D matrix into a 3D ? (x, y) -> (x/72,72,y)
我有一个 text file 从中加载原始矩阵。
文本文件有#注释,基本都是77*44的多个矩阵
我想读取这个文件并存储这个完整数量的垫子中的每个矩阵。
import os
import sys
import numpy as np
from numpy import zeros, newaxis
import io
#read the txt file and store all vaules into a np.array
f = open('/path/to/akiyo_cif.txt','r')
x = np.loadtxt(f,dtype=np.uint8,comments='#',delimiter='\t')
nof = x.shape[0]/72
print ("shape after reading the file is "+ str(x.shape) )
#example program that works
newmat =np.zeros((nof+1,72,44))
for i in range(0,nof+1):
newmat[i,:,:]= x[i*72 : (i*72)+72 , :]
print ("Shape after resizing the file is "+ str(newmat.shape) )
输出:-Shape after reading the file is (21240, 44)
Shape after resizing the file is (274, 72, 44)
如果我运行这个
newmat=x.reshape((nof,72,44))
newmat = x.reshape((nof,72,44))
ValueError: total size of new array must be unchanged
我想将此矩阵的大小调整为 (21240/72,72,44)
。
其中前 77 行对应于 newmat[0,:,:]
,接下来的 77 行对应于 newmat[1,:,:]
.
使用x.reshape(-1, 72, 44)
:
In [146]: x = np.loadtxt('data' ,dtype=np.uint8, comments='#', delimiter='\t')
In [147]: x = x.reshape(-1, 72, 44)
In [148]: x.shape
Out[148]: (34, 72, 44)
当您将其中一个维度指定为 -1 时,np.reshape
会将 -1 替换为从数组长度和其余维度推断出的值。
我有一个 text file 从中加载原始矩阵。
文本文件有#注释,基本都是77*44的多个矩阵
我想读取这个文件并存储这个完整数量的垫子中的每个矩阵。
import os
import sys
import numpy as np
from numpy import zeros, newaxis
import io
#read the txt file and store all vaules into a np.array
f = open('/path/to/akiyo_cif.txt','r')
x = np.loadtxt(f,dtype=np.uint8,comments='#',delimiter='\t')
nof = x.shape[0]/72
print ("shape after reading the file is "+ str(x.shape) )
#example program that works
newmat =np.zeros((nof+1,72,44))
for i in range(0,nof+1):
newmat[i,:,:]= x[i*72 : (i*72)+72 , :]
print ("Shape after resizing the file is "+ str(newmat.shape) )
输出:-Shape after reading the file is (21240, 44)
Shape after resizing the file is (274, 72, 44)
如果我运行这个
newmat=x.reshape((nof,72,44))
newmat = x.reshape((nof,72,44))
ValueError: total size of new array must be unchanged
我想将此矩阵的大小调整为 (21240/72,72,44)
。
其中前 77 行对应于 newmat[0,:,:]
,接下来的 77 行对应于 newmat[1,:,:]
.
使用x.reshape(-1, 72, 44)
:
In [146]: x = np.loadtxt('data' ,dtype=np.uint8, comments='#', delimiter='\t')
In [147]: x = x.reshape(-1, 72, 44)
In [148]: x.shape
Out[148]: (34, 72, 44)
当您将其中一个维度指定为 -1 时,np.reshape
会将 -1 替换为从数组长度和其余维度推断出的值。