我正在为一个大的单列数据的数据操作而挣扎,正如这里所看到的testo.dat(googledrive认为这个文件是一个视频,但是有1.6Mb的数据文件-不用担心)。到目前为止,我所做的代码部分地起作用,您可以在下面查看。在
我正在尝试使用我的输入进行以下操作:
所以我的问题是:矩阵填充的部分(点3)是否有意义,因为它停留在一维数组的第一行?。我在其他post方法中看到了,比如“slice”和“split”—这些方法在这里适用吗?在
非常感谢任何帮助
import numpy as np
import matplotlib.pyplot as plt
# 1. Load a file with a single column and store into an array
data = np.loadtxt('testo.dat',skiprows=3)
bz_array = data[:]
# 2. Prepare the matrix by setting up rows and cols which matchs the length of data
rows = 20
cols = 3600
n=len(data)
if (n == (rows*cols)):
print "rows*cols == length of data"
# 3. Create the matrix
shape = (rows,cols) # 72000 lines/20 = 3600 columns
bz_matrix = np.arange(n).reshape(shape)
if np.shape(bz_matrix) == shape:
print "shape is ok"
# 4. How to fill this matrix with minimum of memory and cpu ?
line=0
while line < n:
for i in range(cols):
for j in range(rows):
bz_matrix=bz_array[line]
print bz_matrix
line+=line
# 5. Transpose the matrix
bz_matrix_transposed = bz_matrix.transpose()
if (np.shape(bz_matrix_transposed) == (cols,rows)):
print "transposed shape is ok"
# 6. I would like to have also a printed file of the transposed matrix
f = open('out.dat', 'w')
np.savetxt(f, bz_matrix_transposed, fmt='%10.5f')
# 7. plot a 2D graph of the transposed matrix,
plt.imshow(bz_matrix_transposed((cols,rows)));
plt.colorbar()
plt.show()
我想更新上面的代码。多亏了乔治(2018年1月19日),我才知道真正的问题在哪里。在
相关问题 更多 >
编程相关推荐