0

I am trying to add new columns using the average of old columns, but I don't know how to loop through the columns from the weights array and append them to the class2 array.

class1 = weights[:,sort[popsize-1]]

for x in range(1,avgmating+1):
  new = (class1 + np.array(weights[:,sort[popsize - 1 -x]]))/2
  class2 = np.hstack((class2,new))

NameError: name 'class2' is not defined

How can I define Class2 and then add the "new" array to it in each iteration?

2
  • You can define class2 before the loop with any of the array creation routines. Commented Jun 22, 2020 at 22:17
  • what are your variables in code? could you give us explanation or sample input/output on what you are trying to achieve? Commented Jun 22, 2020 at 22:45

1 Answer 1

1
for x in range(1,avgmating+1):
  new = (class1 + np.array(weights[:,sort[popsize - 1 -x]]))/2
  if x == 1:
    class2 = new
  else:
    class2 = np.hstack((class2,new))

That works but I'm sure there is a faster way...

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.