我正在尝试对包含图像的数据集进行主成分分析,但每当我想从sklearn.decomposition模块应用pca.transform时,我都会收到此错误: * AttributeError:'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:\Users\private\Desktop\K FOLDER\private\train"
image_dir2 = "C:\Users\private\Desktop\K FOLDER\private\test1"
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:\Users\Private\workspace\final_submission\src\d.py", line 54, in <module>
train_set = pca.transform(train_set)
File "C:\Python27\lib\site-packages\sklearn\decomposition\pca.py", line 298, in transform
if self.mean_ is not None:
AttributeError: 'PCA' object has no attribute 'mean_'
EDIT1: 所以我试着在改造它之前拟合模型,现在我得到了一个更奇怪的错误。我查了一下,它涉及f2py,一个将Fortran移植到Python的模块,它是Numpy Library的一部分。
File "C:\Users\Private\workspace\final_submission\src\d.py", line 54, in <module>
pca.fit(train_set)
File "C:\Python27\lib\site-packages\sklearn\decomposition\pca.py", line 200, in fit
self._fit(X)
File "C:\Python27\lib\site-packages\sklearn\decomposition\pca.py", line 249, in _fit
U, S, V = linalg.svd(X, full_matrices=False)
File "C:\Python27\lib\site-packages\scipy\linalg\decomp_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,它们包含正确的位置(为了清楚起见,我通过转到实际文件,查看一个图像的属性并复制位置来获取它们)。错误应该在其他地方。
答案 0 :(得分:6)
你应该在变换之前拟合模型:
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:\Users\private\Desktop\K FOLDER\private\train\C:\Users\private\Desktop\K FOLDER\private\train\foo.jpg"
,你得到的是空列表而不是文件名。
答案 1 :(得分:3)
我认为您要应用fit_transform
而不是transform
。您需要使用fit
或fit_transform
生成模型。
这是关于每种方法的文档说明:
适合(X,y =无) 使用X适合模型。
fit_transform(X,y =无) 使用X拟合模型并在X上应用降维。
您正在直接应用transform
,因此尚未生成任何模型。