python numpy:数组的数组

1 投票
2 回答
2065 浏览
提问于 2025-04-18 16:05

我正在尝试用下面的代码创建一个包含数组的数组的数组,也就是多维数组。

但是运行后我得到了一个

ValueError: setting an array element with a sequence.

我猜在numpy中,我需要一开始就把数组声明为多维的,但我不太确定……

我该如何修正下面的代码,以便能够创建数组的数组的数组呢?

from PIL import Image
import pickle
import os
import numpy

indir1 = 'PositiveResize'

trainimage = numpy.empty(2)
trainpixels = numpy.empty(80000)
trainlabels = numpy.empty(80000)
validimage = numpy.empty(2)
validpixels = numpy.empty(10000)
validlabels = numpy.empty(10000)
testimage = numpy.empty(2)
testpixels = numpy.empty(10408)
testlabels = numpy.empty(10408)

i=0
tr=0
va=0
te=0
for (root, dirs, filenames) in os.walk(indir1):
    print 'hello'
    for f in filenames:
            try:
                    im = Image.open(os.path.join(root,f))
                    Imv=im.load()
                    x,y=im.size
                    pixelv = numpy.empty(6400)
                    ind=0
                    for i in range(x):
                            for j in range(y):
                                    temp=float(Imv[j,i])
                                    temp=float(temp/255.0)
                                    pixelv[ind]=temp
                                    ind+=1
                    if i<40000:
                            trainpixels[tr]=pixelv
                            tr+=1
                    elif i<45000:
                            validpixels[va]=pixelv
                            va+=1
                    else:
                            testpixels[te]=pixelv
                            te+=1
                    print str(i)+'\t'+str(f)
                    i+=1
            except IOError:
                    continue

trainimage[0]=trainpixels
trainimage[1]=trainlabels
validimage[0]=validpixels
validimage[1]=validlabels
testimage[0]=testpixels
testimage[1]=testlabels

2 个回答

1

可以参考一下这个链接里的例子:numpy.empty

>>> np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])         #random

给你的图像设置一个有N个维度的形状:

testpixels = numpy.empty([96, 96])
1

不要试图把整个对象都塞进一个numpy数组里。如果你有不同的东西,最好为每个东西用一个numpy数组,然后用合适的数据结构把它们放在一起。

比如说,如果你想对多张图片进行计算,那么你可能只需要把像素和标签分别存储在不同的数组里。

trainpixels = np.empty([10000, 80, 80])
trainlabels = np.empty(10000)
for i in range(10000):
    trainpixels[i] = ...
    trainlabels[i] = ...

要访问某一张图片的数据:

imagepixels = trainpixels[253]
imagelabel = trainlabels[253]

你还可以很方便地对这些图片进行一些统计计算。

meanimage = np.mean(trainpixels, axis=0)
meanlabel = np.mean(trainlabels)

如果你真的想把所有数据放在同一个对象里,可能可以使用结构数组,正如Eelco Hoogendoorn所建议的那样。这里有一些示例用法:

# Construction and assignment
trainimages = np.empty(10000, dtype=[('label', np.int), ('pixel', np.int, (80,80))])
for i in range(10000):
    trainimages['label'][i] = ...
    trainimages['pixel'][i] = ...

# Summary statistics
meanimage = np.mean(trainimages['pixel'], axis=0)
meanlabel = np.mean(trainimages['label'])

# Accessing a single image
image = trainimages[253]
imagepixels, imagelabel = trainimages[['pixel', 'label']][253]

另外,如果你想单独处理每一张图片,可以把每张图片的数据存储在不同的数组里,然后把它们放在一个元组或字典中,最后把所有这些放在一个列表里。

trainimages = []
for i in range(10000):
    pixels = ...
    label = ...
    image = (pixels, label)
    trainimages.append(image)

现在要访问单张图片的数据:

imagepixels, imagelabel = trainimages[253]

这样访问单张图片的数据会更直观,但因为所有数据不在一个大numpy数组里,所以你就不能方便地使用那些可以跨图片操作的函数了。

撰写回答