A query in Exercise M1.02

Whenever I am using

test=pd.read_csv("../datasets/adult-census-numeric-test.csv")
target_test=test["class"]
target_data=test.drop(columns="class")
neigh.score(target_data , target_test ) ```
 
I am getting a TypeError: 'tuple' not callable

Whereas when I am using


test=pd.read_csv("../datasets/adult-census-numeric-test.csv")
target_test=test["class"]
target_data=test.drop(columns="class")
(neigh.predict(target_data)==target_test).mean()```

I am getting a output 0.8182004299314157

What is the error in First code?

Your first snippet is working fine when I created neigh as a KNeighborsClassifier with 50 neighbors and fitting it on data and target loaded before and it works.

I assume that something is wrong with some of your previous code that is not shown in this snippet.
If you provide it, I could help.

Thank you for your reply.
Yesterday when I ran the full code it did show me ‘tuple’ error as I have said in my post.
Today when I re-ran the same code from start to end and it did not show me the error , I have attached my yesterday’s code (second one) with this thread (if possible to find the error)
(the code I ran was same with the first one except I have changed the last lines)


Also, I just want to know when do we encounter ‘tuple’ not callable error , so that in future when I get the same error I can debug it .
Thanking you
Maaz Karim

Your code is good. An issue with notebooks is that you can execute cells in a non-linear order (not from top to bottom). I assume that a variable did not have the expected value and something went sideways.

The error that you got will be raised with such pattern:

In [1]: mytuple = (1, 2)

In [2]: mytuple(3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-99b974382bd8> in <module>
----> 1 mytuple(3)

TypeError: 'tuple' object is not callable

My guess could be that in your code at some point, two values were packed together in a tuple (be aware that tuple are returned from Python functions when there are multiple values returned).

Thanks for the reply, it was helpful. :grinning_face_with_smiling_eyes: :smiley:

1 Like