带numpy的Python代码SVD

2024-04-23 06:40:16 发布

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

我想得到一些关于Python的代码的帮助。我是Python的新手。在

在高级-我从命令行读取一个(.png)文件,放入原始数组,计算svd,根据命令行找到svd的高秩,与原始数组相乘,最后将文件和数组输出。在

我的问题:生成的文件被扭曲,看起来不像我想要生成的真实图片。在

我的问题是:我已经把我正在使用的代码片段放在这里了,你能指出我做错事的地方吗?在

import sys
import os
import numpy
import numpy.linalg
import scipy.misc

def getOutputPngName(path, rank):
    filename, ext = os.path.splitext(path)
    return filename + '.' + str(rank) + '.png'

def getOutputNpyName(path, rank):
    filename, ext = os.path.splitext(path)
    return filename + '.' + str(rank) + '.npy'

if len(sys.argv) < 3:
    sys.exit('usage: task1.py <PNG inputFile> <rank>')

    inputfile = sys.argv[1]
    rank = int(sys.argv[2])
    outputpng = getOutputPngName(inputfile, rank)
    outputnpy = getOutputNpyName(inputfile, rank)

# Import pic.png into array im as command parameter
img = scipy.misc.imread(inputfile)

# Perform SVD on im and obtain individual matrices
P, D, Q = numpy.linalg.svd(img, full_matrices=False)

# Compute overall SVD matrix based on individual matrices
svd_decomp = numpy.dot(numpy.dot(P, numpy.diag(D)), Q)

# Keep Top entries in svd_decomp
initial = svd_decomp.argsort()
temp = numpy.array(initial)
svd_final = numpy.argpartition(temp,-rank)[-rank:]

# Multiply to obtain the best rank-k approximation of the original array 
img = numpy.transpose(img)
final = (numpy.dot(svd_final,img))

#Saving the approximated array as a binary array file(1) and as a PNG file(2)
numpy.save(outputnpy, final)
scipy.misc.imsave(outputpng, final)

Tags: 文件pathimportnumpyimgpngossys
2条回答

最大的问题是svd_分解argsort(). 不带任何参数的argsort()会将整个矩阵展平并进行排序,这不是您想要的。在

实际上,您不需要进行任何排序,因为linalg的svd()函数可以为您完成排序。参见documentation。在

The singular values for every matrix, sorted in descending order.

所以你只需要做下面的事情

import sys
import os
import numpy
import numpy.linalg
import scipy.misc

def getOutputPngName(path, rank):
    filename, ext = os.path.splitext(path)
    return filename + '.' + str(rank) + '.png'

def getOutputNpyName(path, rank):
    filename, ext = os.path.splitext(path)
    return filename + '.' + str(rank) + '.npy'

if len(sys.argv) < 3:
    sys.exit('usage: task1.py <PNG inputFile> <rank>')

inputfile = sys.argv[1]
rank = int(sys.argv[2])
outputpng = getOutputPngName(inputfile, rank)
outputnpy = getOutputNpyName(inputfile, rank)

# Import pic.png into array im as command parameter
img = scipy.misc.imread(inputfile)

# Perform SVD on im and obtain individual matrices
P, D, Q = numpy.linalg.svd(img, full_matrices=True)

# Select top "rank" singular values
svd_decomp = numpy.matrix(P[:, :rank]) * numpy.diag(D[:rank]) * numpy.matrix(Q[:rank, :])

# Save the output
numpy.save(outputnpy, svd_decomp)
scipy.misc.imsave(outputpng, svd_decomp)

注意,我们所做的只是选择“rank”单数值,不需要排序。在

示例输出:

基本图像:

enter image description here

等级=1

enter image description here

等级=10

enter image description here

不需要分类。只需计算矩阵的秩

svd_decomp = np.zeros((len(P), len(Q)))
for i in range(rank):
    svd_decomp += D[i] * np.outer(P.T[i], Q[i])

相关问题 更多 >