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:
If I put these two lists inside the outer loop:
averaged_precision = []
averaged_recall = []
I get this:
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?