• 大小: 5.88MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-02
  • 语言: 其他
  • 标签: pca  yale  machine  lear  

资源简介

一次课程实验作业,用人脸数据集进行降维处理显示降维处理后的图像

资源截图

代码片段和文件信息

import operator
import numpy as np
import os
from PIL import Image


class PCAtest():
    def __init__(self d):#d表示所降维度
        self.d = d

    def train(self X):
        X = np.array(X dtype=“float64“)
        X = self.normalize(X)
        cov_matrix_t = X.T.dot(X)
        eigen_values eigen_vectors = np.linalg.eig(cov_matrix_t)
        eigen_values = eigen_values.tolist()
        eigen_vectors = X.dot(eigen_vectors)
        eigen_vectors = eigen_vectors.T.tolist()
        eigen_zip = [(value.real vector) for value vector in zip(eigen_values eigen_vectors)]
        print(“len of real eigen values“ len(eigen_zip))
        orderd_eigens = sorted(eigen_zip key=operator.itemgetter(0) reverse=True)
        self.max_eigen_vectors = [orderd_eigens[i][1] for i in range(self.d)]
        self.W = np.real(np.array(self.max_eigen_vectors).T)
        self.W /= np.sqrt(np.sum(self.W ** 2 axis=0 keepdims=True))

    def normalize(self X):#标准化
        self.mean_vector = 1.0 / X.shape[1] * np.sum(X axis=1 keepdims=True)
        self.std = np.std(X axis=1 keepdims=True) + 10e-8
        X = (X - self.mean_vector)
        return X

    def dim_reduction(self x):#降维处理
        return self.W.T.dot(x)

    def construct(self c):#数据构造
        new = self.W.dot(c)
        return (new)


def main():
    imgName = [‘subject03.wink‘ ‘subject08.happy‘ ‘subject14.surprised‘]#所选图片
    K = [20 100]#所降维数
    for i in K:
        for j in imgName:
            d = i
            path = “yalefaces“
            X = []
            n = 1
            for file in os.listdir(path):
                if not file.endswith(“.txt“):
                    img = Image.open(os.path.join(path file))
                    img = np.array(img).reshape(img.width * img.height)
                    X.append(img)
                    n += 1
            X = np.array(X)
            X = X.T
            pca = PCAtest(d)
            pca.train(X)
            img = Image.open(os.path.join(path j))
            w h = img.width img.height
            img = np.array(img).reshape(w * h 1)
            img = img - pca.mean_vector
            features = pca.dim_reduction(img)
            x_hat = pca.construct(features)
            x_hat += pca.mean_vector
            x_hat = x_hat.reshape(h w)
            x_hat = np.array(x_hat dtype=“int32“)
            new_img = Image.fromarray(x_hat).convert(‘L‘)
            new_file = j + str(i) + ‘.png‘
            new_img.save(os.path.join(‘yalefaces‘ new_file))

if __name__ == “__main__“:
    main()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2656  2018-12-16 15:21  PCA_yale人脸数据集\test.py

     文件       1172  2018-12-16 14:14  PCA_yale人脸数据集\yalefaces\Readme.txt

     文件      35413  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.gif

     文件      36294  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.glasses

     文件      36294  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.glasses.gif

     文件      36692  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.happy

     文件      42921  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.leftlight

     文件      34100  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.noglasses

     文件      36129  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.normal

     文件      71627  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.rightlight

     文件      36370  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.sad

     文件      36307  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.sleepy

     文件      36647  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.surprised

     文件      36381  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject01.wink

     文件      31137  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.centerlight

     文件      45239  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.glasses

     文件      43765  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.happy

     文件      38159  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.leftlight

     文件      43681  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.noglasses

     文件      43681  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.normal

     文件      62924  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.rightlight

     文件      43439  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.sad

     文件      43518  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.sleepy

     文件      45447  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.surprised

     文件      43080  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject02.wink

     文件      26833  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject03.centerlight

     文件      27292  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject03.glasses

     文件      28032  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject03.happy

     文件      34932  1997-04-06 16:06  PCA_yale人脸数据集\yalefaces\subject03.leftlight

     文件      27895  1997-04-06 16:07  PCA_yale人脸数据集\yalefaces\subject03.noglasses

............此处省略143个文件信息

评论

共有 条评论