如何重塑,填充和转置矩阵正确和记忆友好

2024-04-25 22:45:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个大的单列数据的数据操作而挣扎,正如这里所看到的testo.dat(googledrive认为这个文件是一个视频,但是有1.6Mb的数据文件-不用担心)。到目前为止,我所做的代码部分地起作用,您可以在下面查看。在

我正在尝试使用我的输入进行以下操作:

  1. 加载文本并存储到一个1d数组中-这很好
  2. 根据一维数组的长度来重塑矩阵-这也很好
  3. 将单个数组中的值填充到新创建的矩阵中,这样数据就可以用所需的行数和列数排列在网格/矩阵上——这是我的实际问题!
  4. 最后转置矩阵-如果我有一个填充矩阵,这很好

所以我的问题是:矩阵填充的部分(点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()

Tags: ofthedatanplineplt矩阵数组
1条回答
网友
1楼 · 发布于 2024-04-25 22:45:55

我想更新上面的代码。多亏了乔治(2018年1月19日),我才知道真正的问题在哪里。在

# 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_array.shape=(shape)


# 5. Transpose the matrix 
bz_matrix_transposed = bz_array.transpose()
if (np.shape(bz_matrix_transposed) == (cols,rows)):
    print "transposed shape is ok" 


# 6. I would like to have the finnished data as a seperate file 
bz_finnished = np.asarray(bz_matrix_transposed).ravel()
f = open('out_finnished', 'w') 
np.savetxt(f, bz_finnished, fmt='%10.5f')     

相关问题 更多 >