python setting an array_Python-NumPy-元组作为数组的元素

I'm a CS major in university working on a programming project for my Calc III course involving singular-value decomposition. The idea is basically to convert an image of m x n dimensions into an m x n matrix wherein each element is a tuple representing the color channels (r, g, b) of the pixel at point (m, n). I'm using Python because it's the only language I've really been (well-)taught so far.

From what I can tell, Python generally doesn't like tuples as elements of an array. I did a little research of my own and found a workaround, namely, pre-allocating the array as follows:

def image_to_array(): #converts an image to an array

aPic = loadPicture("zorak_color.gif")

ph = getHeight(aPic)

pw = getWidth(aPic)

anArray = zeros((ph,pw), dtype='O')

for h in range(ph):

for w in range(pw):

p = getPixel(aPic, w, h)

anArray[h][w] = (getRGB(p))

return anArray

This worked correctly for the first part of the assignment, which was simply to convert an image to a matrix (no linear algebra involved).

The part with SVD, though, is where it gets trickier. When I call the built-in numPy svd function, using the array I built from my image (where each element is a tuple), I get the following error:

Traceback (most recent call last):

File "", line 1, in -toplevel-

svd(x)

File "C:\Python24\Lib\site-packages\numpy\linalg\linalg.py", line 724, in svd

a = _fastCopyAndTranspose(t, a)

File "C:\Python24\Lib\site-packages\numpy\linalg\linalg.py", line 107, in _fastCopyAndTranspose

cast_arrays = cast_arrays + (_fastCT(a.astype(type)),)

ValueError: setting an array element with a sequence.

This is the same error I was getting initially, before I did some research and found that I could pre-allocate my arrays to allow tuples as elements.

The issue now is that I am only in my first semester of (college-level) programming, and these numPy functions written by and for professional programmers are a little too black-box for me (though I'm sure they're much clearer to those with experience). So editing these functions to allow for tuples is a bit more complicated than when I did it on my own function. Where do I need to go from here? I assume I should copy the relevant numPy functions into my own program, and modify them accordingly?

Thanks in advance.

解决方案

Instead of setting the array element type to 'O' (object) you should set it to a tuple. See the SciPy manual for some examples.

In your case, easiest is to use something like

a = zeros((ph,pw), dtype=(float,3))

Assuming your RGB values are tuples of 3 floating point numbers.

This is similar to creating a 3d array (as Steve suggested) and, in fact, the tuple elements are accessed as a[n,m][k] or z[n,m,k] where k is the element in the tuple.

Of course, the SVD is defined for 2d matrices and not 3d arrays so you cannot use linalg.svd(a). You have to decide SVD of what matrix (of the three possible ones: R G and B) you need.

If, for example, you want the SVD of the "R" matrix (assuming that is the first element of the tuple) use something like:

linalg.svd(a[:,:,1])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是因为你试图将一个序列(例如列表或元组)赋值给NumPy数组的单个元素。这是不允许的,因为NumPy数组的每个元素必须具有相同的数据类型和大小。 解决这个问题的方法是确保你将要分配给NumPy数组的值具有与该数组相同的数据类型和大小。 以下是一个例子: ``` python import numpy as np # 创建一个包含 5 个元素的空数组 a = np.zeros((5,)) # 尝试将一个序列(列表)赋值给数组中的一个元素 a[0] = [1, 2, 3] # 抛出 "ValueError: setting an array element with a sequence." 错误 ``` 在这个例子中,我们尝试将一个包含三个元素的列表赋值给NumPy数组的第一个元素。这导致了一个“ValueError:setting an array element with a sequence.”的错误。 要解决这个问题,我们可以将要分配给NumPy数组的值转换为相同的数据类型和大小。例如,我们可以将列表转换为包含三个元素NumPy数组,然后将其分配给NumPy数组的第一个元素。这是修改后的代码: ``` python import numpy as np # 创建一个包含 5 个元素的空数组 a = np.zeros((5,)) # 将列表转换为 NumPy 数组 b = np.array([1, 2, 3]) # 将 NumPy 数组分配给数组的第一个元素 a[0] = b # 没有错误,分配成功 ``` 在这个例子中,我们将要分配给NumPy数组的值从列表转换为NumPy数组。然后,我们将这个NumPy数组分配给NumPy数组的第一个元素,这没有抛出错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值