3

I am writing a program that will append a list with a single element pulled from a 2 dimensional numpy array. So far, I have:

# For loop to get correlation data of selected (x,y) pixel for all bands
zdata = []
for n in d.bands:
    cor_xy = np.array(d.bands[n])
    zdata.append(cor_xy[y,x])

Every time I run my program, I get the following error:

Traceback (most recent call last):
    File "/home/sdelgadi/scr/plot_pixel_data.py", line 36, in <module>
        cor_xy = np.array(d.bands[n])
TypeError: only integer arrays with one element can be converted to an index

My method works when I try it from the python interpreter without using a loop, i.e.

>>> zdata = []
>>> a = np.array(d.bands[0])     
>>> zdata.append(a[y,x])
>>> a = np.array(d.bands[1])
>>> zdata.append(a[y,x])
>>> print(zdata)
[0.59056658, 0.58640128]

What is different about creating a for loop and doing this manually, and how can I get my loop to stop causing errors?

3
  • 1
    What does d.bands look like? Commented Jul 16, 2015 at 2:05
  • You're treating n as if it's an index into d.bands when it's an element of d.bands. Commented Jul 16, 2015 at 2:08
  • @AnandSKumar 'd.bands' is a list of 50x50 2 dimensional arrays. Commented Jul 16, 2015 at 18:48

1 Answer 1

4

You're treating n as if it's an index into d.bands when it's an element of d.bands

zdata = []
for n in d.bands:
    cor_xy = np.array(n)
    zdata.append(cor_xy[y,x])

You say a = np.array(d.bands[0]) works. The first n should be exactly the same thing as d.bands[0]. If so then np.array(n) is all you need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.