0

I want to iterate through a numpy array and perform division, multiplication, and addition. I keep coming up with several errors. The latest is

IndexError: invalid index to scalar variable.

import numpy as np
rays = np.array([[7.651e-03, 7.284e-03, 5.134e-03, 7.442e-03, 3.035e-03)],[2.373e-03, 6.877e-03, 4.809e-03, 2.870e-04, 3.175e-04, 1.791e-03]])

for i in range(rays):
    for w in range(i):
        estimate = rays[0][i]/(rays[0][i]+rays[1][i])
6
  • 1
    Your rays definition looks syntactically incorrect -- it's got a mismatched ) in the first sublist. This also isn't a numpy array, it's a list of lists. Commented Oct 8, 2021 at 18:38
  • what is 'ray' in your code not 'rays'? Commented Oct 8, 2021 at 18:39
  • Thats my bad, I should have specified numpy array. Going to try and change the title and rays type. Commented Oct 8, 2021 at 18:45
  • What do you want your output to be ? Your code is going to repeatedly replace the value of estimate resulting in the ratio of the last value. You might as well have written estimate = rays[0][-1]/(rays[0][-1]+rays[1][-1]) without any for loop. Commented Oct 8, 2021 at 19:34
  • @AlainT. it will be appended to a list and converted to an array of (1000, ) Commented Oct 8, 2021 at 19:54

3 Answers 3

1

There are several issues with your example (some of which may be actual problems, others just typos or over simplifiactions):

import numpy as np     # if you want to use for-loops don't use numpy
rays = np.array(...    # closing parentheses instead of brackets
                       # unequal dimensions row of 5 and row of 6
                            
for i in range(rays):  # rays is not a number, did you mean len(rays[0])?
    for w in range(i): # w is not used anywhere
        estimate = rays[0][i]/(rays[0][i]+rays[1][i]) 
                       # estimate is overwritten at each iteration

The whole point of using numpy is to avoid "manually" iterating through array elements using for-loops. You should think of your result as an operation between matrices (or vectors):

For example (without for-loops):

import numpy as np
rays = np.array([[7.651e-03, 7.284e-03, 5.134e-03, 7.442e-03, 3.035e-03],
                 [2.373e-03, 6.877e-03, 4.809e-03, 2.870e-04, 3.175e-04]])

estimates = rays[0]/(rays[0]+rays[1])
print(estimates)
[0.76326816 0.51437045 0.51634316 0.96286712 0.90529456]

Note that I removed the last value from the second row because numpy requires fixed dimensions (i.e. it cannot have one row with 5 elements and another with 6)

Your nested loop for w in range(i), though you're not doing anything with w, suggests that you may be looking for the ratio between cumulative sums. If that is the case, use the cumsum function from numpy:

estimates = np.cumsum(rays[0])/np.cumsum(rays[0]+rays[1])
print(estimates)
[0.76326816 0.61753153 0.58805087 0.65726163 0.67565445]
Sign up to request clarification or add additional context in comments.

Comments

0
rays = [
[7.651e-03, 7.284e-03, 5.134e-03, 7.442e-03, 3.035e-03],
[2.373e-03, 6.877e-03, 4.809e-03, 2.870e-04, 3.175e-04],
]

for i in range(min(len(rays[0]), len(rays[1]))):
    estimate = rays[0][i] / (rays[0][i] + rays[1][i])
    print(estimate)

1 Comment

Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts.
0

Fixing up your rays definition:

rays = [
    [7.651e-03, 7.284e-03, 5.134e-03, 7.442e-03, 3.035e-03],
    [2.373e-03, 6.877e-03, 4.809e-03, 2.870e-04, 3.175e-04, 1.791e-03]
]

we can iteratively compute your estimates like this:

for r0, r1 in zip(*rays):
    estimate = r0 / (r0 + r1)
    print(estimate)

If you're not familiar with zip (note that zip(*rays) is the same as zip(rays[0], rays[1])), the above is basically equivalent to:

for i in range(len(rays[0])):  # assuming all rays have same length!
    r0, r1 = rays[0][i], rays[1][i]
    estimate = r0 / (r0 + r1)
    print(estimate)

The zip version is considered more "pythonic" (and is obviously much more concise).

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.