You can download this code by clicking the button below.
This code is now available for download.
This function trains a Naive Bayes classifier using text data to classify comments.
Technology Stack : scikit-learn
Code Type : Machine learning classification
Code Difficulty : Intermediate
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
def classify_reviews(texts, labels):
"""
This function takes in a list of texts and their corresponding labels and
trains a Multinomial Naive Bayes classifier to classify the texts.
"""
# Create a pipeline that vectorizes the text and then trains a Naive Bayes classifier
model = make_pipeline(CountVectorizer(), MultinomialNB())
# Split the data into training and testing sets
texts_train, texts_test, labels_train, labels_test = train_test_split(texts, labels, test_size=0.2, random_state=42)
# Train the model
model.fit(texts_train, labels_train)
# Predict the labels for the test set
predictions = model.predict(texts_test)
# Return the accuracy of the classifier
return model.score(texts_test, labels_test)