Take the 2-minute tour ×

I am trying to do some text analysis with scikit-learn. However when I try to call CountVectorizer an error is raised. The example code and the raised error are below:

    >>> from sklearn.feature_extraction.text import CountVectorizer
    >>> corpus = [  'This is the first document.', 'This is the second second document.',  'And    the third one.',  'Is this the first document?', ]
    >>> vectorizer = CountVectorizer(min_df=1)
    >>> X = vectorizer.fit_transform(corpus)
    Traceback (most recent call last):
    File "/Library/Python/2.6/site-packages/sklearn/feature_extraction/text.py", line 789, in fit_transform
    vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
    File "/Library/Python/2.6/site-packages/sklearn/feature_extraction/text.py", line 716, in _count_vocab
    vocabulary = defaultdict(None)
    TypeError: first argument must be callable

Is this a bug or something with my installation? Other examples are working fine.

share|improve this question
I can't reproduce this with the latest scikit-learn on Python 2.7, and my Python 2.6.6 doesn't have a problem with defaultdict(None) either. What is your exact Python version? –  larsmans Sep 25 '13 at 14:26
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin –  Castilla Sep 25 '13 at 14:27
Does python -c 'from collections import defaultdict as d; d(None) give an error message? –  larsmans Sep 25 '13 at 14:30
Yes, same error! –  Castilla Sep 25 '13 at 14:34
Just compiled Python 2.7.5 and it does run successfully defaultdict . CountVectorizer now should work. Thanks –  Castilla Sep 25 '13 at 18:20
show 4 more comments

1 Answer

up vote 1 down vote accepted

To summarize the discussion in the comments: this is a bug in Python 2.6.1 that has been fixed more recent versions of Python 2.6 (and later as 2.7+, 3.2+...).

share|improve this answer
add comment

Your Answer


