将lil_matrix格式的稀疏矩阵转换为图像

该博客展示了如何读取并操作稀疏矩阵,通过numpy和scipy库进行转换,然后利用matplotlib进行可视化。作者首先加载了一个名为'plants_amat.npz'的数据文件,将数据转换为numpy数组,并创建了一个400x400的零矩阵。接着,遍历稀疏矩阵的每一行,填充零矩阵,并保存到'matrix.txt'文件中。最后,用matplotlib显示了矩阵的图像。
摘要由CSDN通过智能技术生成
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')




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值