Reputation: 3
While working with a trainer module I used dictionary to store the parameter being observed and the corresponding accuracy of the data for the parameter value. The parameter being the key to the dictionary and the accuracy being the value.
I have used sklearn.metrics tool to calculate the accuracy here. But the result is showing the following behaviour.
def fit(self, X, Y):
acc={}
best_b = 0
for b in range(0, X.shape[1] + 1):
self.b=b
Y_Pred = self.predict(X)
acc[b]=accuracy_score(Y_Pred,Y)
print(type(acc))
best_b = max(acc, key=acc.get)
self.b = best_b
print("Best b : ", self.b, " with highest accuracy : ", acc[self.b])
Output:
<class 'dict'>
<ipython-input-184-8b55ae4bcee2> in fit(self, X, Y)
21 acc[b]=accuracy_score(Y_Pred,Y)
22 print(type(acc))
---> 23 best_b = max(acc, key=acc.get)
24 self.b = best_b
25 print("Best b : ", self.b, " with highest accuracy : ", acc[self.b])
TypeError: 'list' object is not callable
Why the dictionary is being treated as list object?
Upvotes: 0
Views: 56
Reputation: 231365
This use of max
on dictionary is valid:
In [1]: acc = {}
In [2]: acc = {'a':1, 'b':3, 'c':2}
In [3]: max(acc, key=acc.get)
Out[3]: 'b'
In [4]: type(max)
Out[4]: builtin_function_or_method
The error suggests that you have redefined max
. It is no longer the builtin function, but a list. That's why I keep asking type(max)
.
In [5]: max=[1,2,3]
In [6]: max(acc, key=acc.get)
Traceback (most recent call last):
File "<ipython-input-6-d5bdb2afa5ad>", line 1, in <module>
max(acc, key=acc.get)
TypeError: 'list' object is not callable
In [7]: type(max)
Out[7]: list
Upvotes: 1