python - MNIST Python numpy 特征向量可视化错误

标签 python numpy machine-learning pca eigenvector

我正在尝试对 MNIST 执行 PCA数据集,作为过程的一部分,我需要生成特征向量并可视化最重要的特征。以下是我的算法:

  1. 加载图片
  2. 减去平均值
  3. 生成协方差矩阵
  4. 导出特征向量和特征值

这是一个运行起来相当简单的算法;我的第一个任务是将前 10 个特征向量可视化为图像。以下是我到目前为止的代码:

__author__      =   "Ajay Krishna Teja Kavuri"


import numpy as np 
import random
from mnist import MNIST
import matplotlib.pylab as plt

class PCAMNIST:


    #Initialization
    def __init__(self):
        #Load MNIST datset
        mnistData = MNIST('./mnistData')
        self.imgTrain,self.lblTrain=mnistData.load_training()
        self.imgTrainSmpl=self.imgTrain[:60000]
        np.seterr(all='warn')


    #1. Subtract the mean because the PCA will work better
    def subMean(self):
        try:
            self.sumImg = np.empty([784,])
            #calculate the sum
            for img in self.imgTrainSmpl:
                imgArr = np.asarray(img)
                self.sumImg = np.add(imgArr,self.sumImg)

            #Calculate the mean array
            self.meanImg = self.sumImg/(len(self.imgTrainSmpl))
            self.meanImg = np.nan_to_num(self.meanImg)

            #subtract it out
            index=0
            for img in self.imgTrainSmpl:
                imgArr = np.asarray(img)
                self.imgTrainSmpl[index] = np.subtract(imgArr,self.meanImg).tolist()
                index += 1

            #for img in self.imgTrainSmpl:
                #print img
        except:
            print Exception 


    #2. get the covaraince matrix for each digit
    def getCov(self):
        self.imgCov=[]
        dgtArr = np.asarray(self.imgTrainSmpl).T
        dgtCov = np.cov(dgtArr)
        self.imgCov.append(dgtCov)
        #for img in self.imgCov:
            #print img

    #3. get the eigen vectors from the covariance matrix
    def getEigen(self):
        self.eigVec=[]
        self.eigVal=[]
        dgtArr = np.asarray(self.imgCov)
        tmpEigVal,tmpEigVec=np.linalg.eig(dgtArr)
        self.eigVal.append(tmpEigVal.tolist())
        self.eigVec.append(tmpEigVec.tolist())

        #print "\nEigen values:\n"
        #for img in self.eigVal:
            #print img

        #print "\nEigen vectors:\n"
        #for img in self.eigVec:
            #print img


    def sortEV(self):
        self.eigValArr = np.asarray(self.eigVal[0][0])
        self.eigVecArr = np.asarray(self.eigVec[0][0])
        self.srtdInd = np.argsort(np.abs(self.eigValArr))
        self.srtdEigValArr = self.eigValArr[self.srtdInd]
        self.srtdEigVecArr = self.eigVecArr[self.srtdInd]
        self.srtdEigVec = self.srtdEigVecArr.real.tolist()
        #print self.srtdEigValArr[0]
        print len(self.srtdInd.tolist())
        #print self.eigVec[self.srtdInd[0]]
        #print np.asarray(self.srtdEigVec).shape
        #for img in self.srtdEigVecArr:
            #print img
        #self.drawEig()

    def plotVal(self):
        """
        plt.figure()
        plt.scatter(np.asarray(self.eigVal).real)
        plt.show()
        """

    def drawEig(self):
        for vec in self.srtdEigVec[:10]:
            self.drawEigV(vec)


    def drawEigV(self,digit):
        plt.figure()
        fig=plt.imshow(np.asarray(digit).reshape(28,28),origin='upper')
        fig.set_cmap('gray_r')
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.savefig(str(random.randint(0,10000))+".png")
        #plt.show()
        plt.close()



    def drawChar(self,digit):
        plt.figure()
        fig=plt.imshow(np.asarray(digit).reshape(28,28),clim=(-1,1.0),origin='upper')
        fig.set_cmap('gray_r')
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.show()
        plt.close()


    def drawSmpl(self):
        for img in self.imgTrainSmpl:
            self.drawChar(img) 


    def singleStep(self):
        self.val, self.vec = np.linalg.eig(np.cov(np.array(self.imgTrainSmpl).transpose()))
        self.srtd = np.argsort(self.val)[::-1]
        print self.val


#asnmnt4=PCAMNIST()
#asnmnt4.singleStep()
asnmnt4=PCAMNIST()
asnmnt4.subMean()
asnmnt4.getCov()
asnmnt4.getEigen()
asnmnt4.sortEV()
asnmnt4.drawEig()
#asnmnt4.plotVal()
"""
asnmnt4.getSorted()
asnmnt4.printTopEigenVal()
"""

尽管上面的代码运行完美并且所有数组大小都与给定的数据集匹配,但它会生成以下图像和特征向量: Eigen vectors as images

显然,特征向量没有意义,因为它们必须表示数据集的特征,在本例中应该是数字。任何帮助表示赞赏。如果您尝试运行此代码,您可能必须安装 MNIST 包并从链接下载数据。

最佳答案

正如一些用户所指出的,问题在于特征向量使其工作,而不是更改附加逻辑,只需将绘制函数修改为:

def drawEig(self):
        for vec in self.srtdEigVecArr.T[:10]:
            self.drawEigV(vec)

现在,特征向量有意义了: enter image description here

关于python - MNIST Python numpy 特征向量可视化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36758300/

相关文章:

python - 结合条形图和线图的问题(python)

python - 在Python中对加速度计数据随时间的变化进行分类

python - 使用 key 对身份验证的 Snowflake 的 SQLAlchemy engine.connect() 失败

python - 如何在 x 轴上设置日期刻度标签,仅适用于 matplotlib 上的给定点

python - 在 C 中嵌入 Python : Error when attempting to call Python code in a C callback called by Python code

python - 二维数组内的填充列表

python - Python 上的映射字段

python - TensorFlow:优化推断 Estimator 导出的 SavedModel

algorithm - minimax 静态树是如何构建的?

python - 我该如何处理最后的跳跃?