4

I am trying to find the optimal smoothing parameter for additive smoothing with 10-fold cross validation. I wrote the following code:

alphas = list(np.arange(0.0001, 1.5000, 0.0001))

#empty list that stores cv scores
cv_scores = []

#perform k fold cross validation
for alpha in alphas:
    naive_bayes = MultinomialNB(alpha=alpha)
    scores = cross_val_score(naive_bayes, x_train_counts, y_train, cv=10, scoring='accuracy')
    cv_scores.append(scores.mean())

#changing to misclassification error
MSE = [1 - x for x in cv_scores]  

#determining best alpha
optimal_alpha = alphas[MSE.index(min(MSE))]

I am getting the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-9d171ddceb31> in <module>()
     18 
     19 #determining best alpha
---> 20 optimal_alpha = alphas[MSE.index(min(MSE))]
     21 print('\nThe optimal value of alpha is %f' % optimal_alpha)
     22 

TypeError: 'int' object is not callable

I have run the same code for different values of parameters of arange() and K (cross validation). This is the first time I encountered this error. Why?

1
  • 1
    I don't see why you'd run into this - it appears min is a reference to an integer. Did you set min equal to something elsewhere in your ipython workflow? Commented Oct 8, 2018 at 18:33

1 Answer 1

6

Somewhere else in your code you have something that looks like this:

 min = 10

Then you write this:

optimal_alpha = alphas[MSE.index(min(MSE))]

So, min() is interpreted as a function call.

Sign up to request clarification or add additional context in comments.

2 Comments

Completely correct, except index is function of the list, so you don't have a namespace collision. It would have to be min.
You guys are absolutely right. I recently added a new piece of code in my notebook in which I added a line 'min=999'. Thanks to both of you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.