import numpy as np import pickle from PIL import Image from scipy import misc import pylab import matplotlib.pyplot as plt from scipy import sparse with open('plants_amat.npz','rb') as handle: my_array=pickle.load(handle,encoding='latin1') data1=np.array(my_array) print(my_array.shape) print((my_array[0].data[0])) print(len(my_array[0].rows[0])) matrix=np.zeros((400,400)) print(matrix.shape) for i in range(0,400):#稀疏矩阵的行 zhi=my_array[i].data[0] geshu=len(zhi) col=my_array[i].rows[0] for l in zhi: for c in col: c=int(c) matrix[i][c]=l np.savetxt("matrix.txt",matrix) matrix=np.mat(matrix) plt.imshow(matrix) pylab.show() print('end')
将lil_matrix格式的稀疏矩阵转换为图像
最新推荐文章于 2023-07-22 15:53:12 发布
该博客展示了如何读取并操作稀疏矩阵,通过numpy和scipy库进行转换,然后利用matplotlib进行可视化。作者首先加载了一个名为'plants_amat.npz'的数据文件,将数据转换为numpy数组,并创建了一个400x400的零矩阵。接着,遍历稀疏矩阵的每一行,填充零矩阵,并保存到'matrix.txt'文件中。最后,用matplotlib显示了矩阵的图像。
摘要由CSDN通过智能技术生成