当前位置: 首页 > 工具软件 > lil-brother > 使用案例 >

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

饶铭
2023-12-01
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')
 类似资料: