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])
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 (ie 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]
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).
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.