0

I am working on the following Python codes.

I am hoping to accomplish the following:

  1. Create a total_fold_array which will hold 5 items (folds)
  2. For each fold, create an array of data from a larger dataset based off of the logic (which I know is correct) inside of my for...zip loop

To help you understand: The classses and class_weights returns: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] and [0.14285714 0.14285714 0.14285714 0.14285714 0.14285714 0.14285714 0.14285714]

The while count !=6 is not working properly. In short, what I am trying to accomplish is to populate the total_fold_array with 5 individual folds, which each contain a number of rows from a dataset.

An example of the current_fold_array might be [A,B,C,D], so then ultimately, I have a total_fold_array which has 5 of those individual folds, which would look like [[A,B,C,D,],[A,B,B,C],[A,A,A,A],[B,C,D,D],[B,B,B,C]]

However, this loop does not do that. Instead, it creates total_fold_array with the length of whatever the length of classes is (in this case 7), instead of having 5 folds within.

My code is below:

I am currently getting a total_fold_array containing 7 items, when instead, it should contain 5. Each item within can have as many items as needed, but the total_fold_array should be 5 items long. I believe there is a logic bug in my code, and I am looking for some help. If I were to use a dataset with 5 classes, this works appropriately.

Please let me know if I need to make this clearer.

2
  • Is count = count + 1 indented properly? The indentation for the last two lines look suspicious also. Commented Mar 10, 2019 at 22:30
  • Not sure @wwii. I tried moving them up 1 layer and instead get a total_fold_array size of 1, so I don't know if that helps. Commented Mar 10, 2019 at 22:34

1 Answer 1

1

Just before for a_class,a_class_weight in zip(classes, class_weights):, you're initializing total_fold_array to [].

That loop executes for exactly as many times as there are elements in classes.

Each iteration of that loop appends a curr_fold_array to total_fold_array.

That is why, at the end of that loop, you have as many elements in total_fold_array, as there are in classes.

You've enclosed all of this in while count != 6:. That seems totally unnecessary -- I think that while loop will execute exactly once. You are returning from that function before the second iteration of that while loop can happen. My guess is that you introduced that while loop hoping that it would somehow limit the number of elements in total_fold_array to 5. But that's not going to happen, because, inside that while loop, the for loop grows total_fold_array to have 7 elements, and this happens in the very first iteration of the while loop.

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

10 Comments

thank you, this was extremely helpful. Your intuition regarding the while loop is correct. If I understand your answer appropriately, I need to move the instantiation of the total array to outside the while loop which ensures I properly create 5 folds, right?
That really depends upon some details of what you're intending to do, which I don't have. If I were to make a wild guess, try reducing the indent of the last four lines by one indentation level (that would bring the return statement out of the while loop, and the append statement out of the for loop. Retain the rest of the code as it currently stands, including the while loop. Perhaps that would give you the result you are looking for - but I'm only second-guessing your program's objectives.
How can I improve my question to better explain my intentions? In short, I just need to create an array with 5 elements, where each element itself is an array that is a result of the logic in the class loop.
The suggestion I've mentioned above will definitely ensure that total_fold_array has 5 elements. But what I'm not sure about is whether the content within will be correct or not (consistent with your expectations of what that content should be). So far, you've only stated that total_field_array should have 5 elemements, but you've not mentioned any details of what the content of those 5 elements should be.
The classes and class weights that are described in the post vary in size, but essentially, they are uses to loop through a large dataset and append a certain number of elements to a "fold", or one of those 5 elements.
|

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.