0

I am trying to write an equation for a nested for loop. i am getting an error and am trying to understand how to write it?

ysub =  GPA
0   0.4509999999999996
1   -0.04900000000000038
2   -0.5490000000000004
3   0.20099999999999962
4   -0.4490000000000003
5   0.19099999999999984
6   0.4509999999999996
7   -0.5490000000000004
8   0.25099999999999945
9   0.05099999999999971

Code:

i = range (1,10,1)
m = range (1,10,1)
 RMSEtest = ((1/m)*(ysub[i]^2))

we have to calculate RMSEtest (sum of values from m = 1 to 10) for each of the ysub values.

4
  • For example, in 1/m you're trying to divide the integer 1 by the range object range (1,10,1), which is not possible.
    – ForceBru
    Dec 7, 2020 at 19:08
  • okay...thq.so how do we write it? any ideas?
    – satya
    Dec 7, 2020 at 19:10
  • @satya, what's RMSEtest? If, as your question says, you want to sum all values of ysub from m = 1 to 10, then you could do sum(ysub[k] for k in m)
    – ForceBru
    Dec 7, 2020 at 19:14
  • trying to solve an equation (google RMSD). basically i am trying to solve that equation for all the values of ysub
    – satya
    Dec 7, 2020 at 19:17

2 Answers 2

0

There were a lot of mistakes

  1. you cannot divide integer with a range object as told by @ForceBru.

  2. You don't need 2 loops for your work

  3. To raise the power of an integer or a float, '**' is used instead of '^'

     ysub = [0.4509999999999996, -0.04900000000000038,-0.5490000000000004,0.20099999999999962,-0.4490000000000003, 0.19099999999999984, 0.4509999999999996, -0.549000000000000, 0.25099999999999945, 0.05099999999999971]
    

Code:

for m in range (1,10):
    RMSEtest = ((1/m)*(ysub[m]**2))

From your code, I think this is what you wanted.

0

first of all that is not a for-loop. You really don't need to add a third parameter to the range because the default is 1. Your question is unclear about what you need the for-loop to do and the code doesn't help.
For nested for-loops, you want something like:

for x in I:
    for y in m:
        do_something(x, y)
1
  • i am trying to solve RMSE (google for equation) for i = 1 to 10 for all the 10 values in ysub.
    – satya
    Dec 7, 2020 at 19:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.