2

I'm new to Python. I have the following code snippet:

kf = KFold(n_splits=5)

averaged_precision_for_t_knn = []
averaged_recall_for_t_knn = []
for t in range(1,6,1):
    print(t)
    averaged_precision = []
    averaged_recall = []
    for trainset, testset in kf.split(data):
        algo.fit(trainset)
        predictions = algo.test(testset)
        precision, recall = compute_precision_recall(predictions, t, 4)

        # average over all users

        averaged_precision.append(sum(precision for precision in precision.values()) / len(precision))
        averaged_recall.append(sum(recall for recall in recall.values()) / len(recall))

        print(averaged_precision)
        print(averaged_recall)
    averaged_precision_for_t_knn.append(np.mean(averaged_precision))
    averaged_recall_for_t_knn.append(np.mean(averaged_recall))

If I put these two lists inside the outer loop:

averaged_precision = []
averaged_recall = [] 

I get this:

enter image description here

If I put these two lists inside the outer loop:

averaged_precision = []
averaged_recall = [] 

I get this:

enter image description here

When I put the two lists inside the outer loop, they both empty themselves after each iteration. While If I put them outside the outer loop they keep adding the values. Why is that?

Right leg
  • 16,080
  • 7
  • 48
  • 81
Stephen
  • 153
  • 6

2 Answers2

2

When the list assignments are outside (before) the outer loop, they are set to empty only at the beginning of the loop's execution. Because of this, each value added either of the lists stays there through all the iterations.

When the list assignments are inside the outer loop, they are set to empty every time the outer loop iterates. Because of this, each value added to one of the lists stays only until the next iteration clears the list.

Ben
  • 46
  • 2
  • Thank you Ben! Could you suggest me a website to learn about this particular issue? – Stephen Aug 31 '19 at 22:42
  • I don't have a specific site in mind, but if you search for "nested loops tutorial Python", you'll probably turn up some good resources. This isn't a Python-specific issue, though; you'll encounter this in pretty much any programming language. – Ben Aug 31 '19 at 22:58
0

1) if you put the lists outside the outer loop they are not reinitialized/emptied over your outer loop iteration, so they become bigger and bigger cumulating all the appended values

2) if you put the lists inside your outer loop they are reinitialized/emptied over each iteration, so they accumulate appended data only during one iteration time

kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Thank you rusu_ro1! Could you suggest me a website to learn about this particular issue? – Stephen Aug 31 '19 at 22:42
  • 1
    have a look over namespace and scope in python https://www.geeksforgeeks.org/namespaces-and-scope-in-python/ – kederrac Aug 31 '19 at 22:45
  • 1
    Incorrect. OP's issue is not due to scope, and your description of scope is also wrong. Python has no block scope, i.e., loops do not have a scope. See https://stackoverflow.com/questions/6167923/block-scope-in-python and https://stackoverflow.com/questions/3611760/scoping-in-python-for-loops. – Roope Aug 31 '19 at 22:52