jack west
jack west

Reputation: 641

Python list and sklearn HashingVectorizer

I have a a nested python list in the following format

npes = [['a','b'], ['d','e'],['f','g']]

I need to vectorizer this list using HashingVectorizer() however I cant understand how I should process this list using vectorizer.fit_transform

vectorizer = HashingVectorizer()
Xc = vectorizer.fit_transform(npes)

The above gives the following error

  AttributeError: 'list' object has no attribute 'lower'

Would anyone know how to do this?

Upvotes: 0

Views: 172

Answers (1)

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

npes is a list of lists and the vectorizer doesn't take list of lists as parameter. So flatten the list and send it as a parameter i.e

npes = [['a','b'], ['d','e'],['f','g']]
vectorizer = HashingVectorizer()
Xc = vectorizer.fit_transform(sum(npes, []))

If you are using numpy then npes = np.ravel(npes)

Upvotes: 2

Related Questions