Search code examples
pythonnaivebayes

Getting TF/Count vector with Naive Bayes


I am using Python 3.7 (Windows, 64 bit). Although I can get TF-IDF result but I cant get TF/Count vector after that. Heres the sample code where I get TF-IDF result,

DOC, LBL = read_corpus()

# Splits the dataset into training (75%) and test set(25%)
split_point = int(0.75*len(DOC))
trainDoc = DOC[:split_point]
trainClass = LBL[:split_point]
testDoc = DOC[split_point:]
testClass = LBL[split_point:]

# Calling the classifier (use the tf-idf/count feature/vectorizer)
Multinomial_Naive_Bayes(trainDoc, trainClass, testDoc, testClass, tfIdf=True)

Solution

  • After doing some google search, I can't find any library contains Multinomial_Naive_Bayes() method.

    You may need edit your question and paste your own method like Multinomial_Naive_Bayes() and read_corpus() here.

    But if you are using scikit-learn, you can do this way:

    vectorizer = CountVectorizer()
    transformer = TfidfTransformer()
    word_freq_matrix = vectorizer.fit_transform(corpus)
    tfidf_result = transformer.fit_transform(word_freq_matrix)