主成分分析不起作用



>我正在尝试对包含图像的数据集进行主成分分析,但是每当我想从sklearn.decomposition模块应用pca.transform时,我总是收到此错误:*属性错误:"PCA"对象没有属性"mean_"*。我知道这个错误意味着什么,但我不知道如何解决它。我想你们中的一些人知道如何解决这个问题。

谢谢你的帮助

我的代码:

from sklearn import svm
import numpy as np
import glob
import os
from PIL import Image
from sklearn.decomposition import PCA
image_dir1 = "C:UsersprivateDesktopK FOLDERprivatetrain"
image_dir2 = "C:UsersprivateDesktopK FOLDERprivatetest1"
Standard_size = (300,200)
pca = PCA(n_components = 10)
file_open = lambda x,y: glob.glob(os.path.join(x,y))

def matrix_image(image_path):
    "opens image and converts it to a m*n matrix" 
    image = Image.open(image_path)
    print("changing size from %s to %s" % (str(image.size), str(Standard_size)))
    image = image.resize(Standard_size)
    image = list(image.getdata())
    image = map(list,image)
    image = np.array(image)
    return image
def flatten_image(image):  
    """
    takes in a n*m numpy array and flattens it to 
    an array of the size (1,m*n)
    """
    s = image.shape[0] * image.shape[1]
    image_wide = image.reshape(1,s)
    return image_wide[0]
if __name__ == "__main__":
    train_images = file_open(image_dir1,"*.jpg")
    test_images = file_open(image_dir2,"*.jpg")
    train_set = []
    test_set = []
    "Loop over all images in files and modify them"
    train_set = [flatten_image(matrix_image(image)) for image in train_images]
    test_set = [flatten_image(matrix_image(image)) for image in test_images]
    train_set = np.array(train_set)
    test_set = np.array(test_set)
    train_set = pca.fit_transform(train_set) "line where error occurs"
    test_set = pca.fit_transform(test_set)

完整回溯:

Traceback (most recent call last):
  File "C:UsersPrivateworkspacefinal_submissionsrcd.py", line 54, in <module>
    train_set = pca.transform(train_set)
  File "C:Python27libsite-packagessklearndecompositionpca.py", line 298, in transform
    if self.mean_ is not None:
AttributeError: 'PCA' object has no attribute 'mean_'

编辑1:所以我试图在转换模型之前拟合它,现在我遇到了一个更奇怪的错误。我查了一下,它涉及f2py,一个将Fortran移植到Python的模块,它是Numpy库的一部分。

File "C:UsersPrivateworkspacefinal_submissionsrcd.py", line 54, in <module>
    pca.fit(train_set)
  File "C:Python27libsite-packagessklearndecompositionpca.py", line 200, in fit
    self._fit(X)
  File "C:Python27libsite-packagessklearndecompositionpca.py", line 249, in _fit
    U, S, V = linalg.svd(X, full_matrices=False)
  File "C:Python27libsite-packagesscipylinalgdecomp_svd.py", line 100, in svd
    full_matrices=full_matrices, overwrite_a = overwrite_a)
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

编辑2:

所以我检查了我的train_set和data_set是否包含任何数据,而它们没有。我已经检查了我的image_dirs,它们包含正确的位置(为了清楚起见,我通过转到实际文件,查看其中一个图像的属性并复制位置来获取它们)。错误应该在其他地方。

您应该在转换之前拟合模型:

train_set = np.array(train_set)
test_set = np.array(test_set)
pca.fit(train_set)
pca.fit(test_set)
train_set = pca.transform(train_set) "line where error occurs"
test_set = pca.transform(test_set)

编辑

第二个错误表示您的train_set为空。可以使用以下代码轻松复制它:

train_set = np.array([[]])
pca.fit(train_set)

我认为一个问题在于flatten_image功能。我可能是错的,但这行会引发AttributeError

image.wide = image.reshape(1,s)

它可以替换为:

image_wide = image.reshape(1,s)
return image_wide[0]

这一行也有问题:

print("changing size from %s to %s" % str(image.size), str(Standard_size))

有关更多详细信息,请阅读 http://docs.python.org/2/library/stdtypes.html#string-formatting-operations,但values must be a tuple 。所以你想要这个:

print("changing size from %s to %s" % (str(image.size), str(Standard_size)))

另一个编辑

最后,您将"Loop over all images in files and modify them"后的循环替换为:

train_set = [flatten_image(matrix_image(image)) for image in train_images]
test_set = [flatten_image(matrix_image(image)) for image in test_images]

现在你调用file_open所以它会在路径中查找文件,如下所示:"C:UsersprivateDesktopK FOLDERprivatetrainC:UsersprivateDesktopK FOLDERprivatetrainfoo.jpg",你会得到空列表而不是文件名。

我认为您想应用fit_transform而不是transform.您需要使用 fitfit_transform 生成模型。

以下是文档对每种方法的说明:

拟合(X, y=无) 用 X 拟合模型。

fit_transform(X, y=无) 用 X 拟合模型,并在 X 上应用降维。

您直接应用transform,因此尚未生成模型。

相关内容

  • 没有找到相关文章

最新更新