I am trying to create a function which counts the length of elements in the list and then run an if / elif loop on them:
k_nearest_samples_class = training_data[sorted_indices[:k]][:, -1] # print(k_nearest_samples_class) # counting number of occurrences of either 0's or 1's with below 2 lines class_0_count = len(k_nearest_samples_class[k_nearest_samples_class == 0]) class_1_count = len(k_nearest_samples_class[k_nearest_samples_class == 1]) class_2_count = len(k_nearest_samples_class[k_nearest_samples_class == 2]) # Combining > & = sign so even in tie-up cases the instance will be classified to malignant - assumed it # would be okay in order to reduce false positives if class_0_count >= class_1_count and class_2_count: print("0", class_0_count) return 0 elif class_1_count >= class_0_count and class_2_count: print("1", class_1_count) return 1 else: print("2", class_2_count) return 2 Giving input one by one like:
[0.0] [1.0] [2.0] currently, my if loop is working illogically.
if class_0_count >= class_1_count and class_0_count >= class_2_count. The>=is not being applied to bothclass_1andclass_2like you may thinkif ... class_2_countis only testing thatclass_2_countis > 0. Is this your design? If not, this might be part of the issue. (?)