When using TfidfVectorizer how can I vectorize one sample data?

Viewed 52

I will try to be clear as possible in my question:

I trained a logistic regression model with a collected data of trusted tweets and also fake tweets related to covid-19.

Now I want to be able to predict data individually. I go and select the sample data and I want to get the result for that specific sample.

input_ = df_2['Tweets'][3]
input_

When running that I get:

'Warmer Weather May Slow, but Not Halt, Coronavirus'

Trying to tranform that sentence:

input_transformed = vectorization.fit_transform(input_)

ValueError: Iterable over raw text documents expected, string object received.

I does not accept strings, is there a way of vectorising strings only? or maybe there is another way when it comes to 1 string sample?

Repo: https://github.com/jorgesisco/Fake-News-Detection-Machine-Learning-Pipeline/blob/main/Notebooks/Classification-Model-for-Fake-News-Detection.ipynb

At the end of the notebook you can see what I am trying to do.

1 Answers

Maybe there is, but note that you could also do:

from sklearn.feature_extraction.text import TfidfVectorizer

df = pd.DataFrame({'Tweet': ['Hello world', 'Good world']})

tfidf = TfidfVectorizer()


tfidf.fit(df)

# Similar to your syntax
tfidf.transform([df['Tweet'][0]]).todense()

# My preferred syntax:
tfidf.transform(df.loc[0, ['Tweet']]).todense()

However, this is very inefficient in terms of runtime.

Related