W4 Exercise 11: approximate_knn error

I solve the code but when I run it give me this error
ValueError Traceback (most recent call last)
in
4 # Sample
5 nearest_neighbor_ids = approximate_knn(
----> 6 doc_id, vec_to_search, planes_l, hash_tables, id_tables, k=3, num_universes_to_use=5)

in approximate_knn(doc_id, v, planes_l, hash_tables, id_tables, k, num_universes_to_use, hash_value_of_vector)
40 for i, new_id in enumerate(new_ids_to_consider):
41
—> 42 if doc_id == new_id:
43 continue
44

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hi @Amar_Ismail

This error typically occurs when you’re trying to use a NumPy array in a context where a single Boolean value is expected, but the array contains more than one element, and it’s not clear how to interpret the truth value of the entire array.

In other words, new_id is probably an array. So first check that (for example, by printing this variable). And if it is, the most probable place for this error should be in previous exercises (for example, make_hash_table function or other. Have you passed all previous tests?)

yes I passed all previous tests

Either your doc_id or new_id are incorrect - likely one of them is a vector. I suspect it’s doc_id.

If you passed all the tests, then make sure that you do not use global variables (for example, you should use vecs variable in UNQ_C19 (which is in local scope for this function) and not document_vecs (which is declared outside this function)).

The doc_id should not be a problem, since you should never change it and is provided for you.

The problem should lie in the new_id, because looking at the Assignment (the code that is provided for you) you can see:

...
        # get the id_table for this particular universe_id
        id_table = id_tables[universe_id]

        # get the subset of documents to consider as nearest neighbors from this id_table dictionary
        new_ids_to_consider = id_table[hash_value]

        ### START CODE HERE (REPLACE INSTANCES OF 'None' with your code) ###

        # loop through the subset of document vectors to consider
        for i, new_id in enumerate(new_ids_to_consider):
            
            if doc_id == new_id:
                continue
...

that the value for new_id depends on id_tables which is passed for this function and is calculated (in a previous cell) by:

hash_tables, id_tables = create_hash_id_tables(N_UNIVERSES)

which depends on your implementation of make_hash_table() function in UNQ_C19.


In other words, you should check if and why the`new_id` is a NumPy array and trace back where in your code that happens.

Cheers