0

I want to get a loop, so instead of writing me code such:

tfidf = vectorizer.fit_transform([ data[0]['body'] , data[1]['body'] , data[2]['body'] , data[3]['body'] ....  ])

I get it in a loop. So far I have tried:

for i in range(len(data) - 1):
    tfidf = vectorizer.fit_transform([ append(data[i]['body']) , data[i+1]['body']) ])

Any idea on how to make it work? I get the following error:

name 'append' is not defined

1
  • 1
    append() must be called on a list, it doesn't exist as a global scope function. Commented Apr 27, 2016 at 19:19

2 Answers 2

1

I guess you want to extract the 'body' content from the json data and have a list of text elements to then pass it to the feature extractor (make sure you have preprocessed the text before).

Try this:

    tfidf = vectorizer.fit_transform([d['body'] for d in data])

Or, if it is more clear to you, you can create the list first and then pass it to the function:

bodies = [d['body'] for d in data]
tfidf = vectorizer.fit_transform(bodies)

Hope it helps :)

Happy coding !!!

P.D: I haven't tested the code, but I think the idea is clear.

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

2 Comments

great answer @Salias ! I looks really good, wha tod you mean by processes the text before?
If you want to extract tf-idf matrix of your collection, you should have been removed stopwords, punctuation, etc.... before. Otherwise, you will have a very noisy matrix with meaningless words like "to" or "and" as the most repeated, and thus, the most important. The pre-processing filters depend on the task you want to accomplish. Have a look at researchgate.net/publication/…
1

You need to call append() on a list. eg MyList.append(element)

1 Comment

true, I tried to define a list before but the vectorizer.fit_transform does not allow it.... any other alternatives?

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.