0% found this document useful (0 votes)
9 views

Clp

The document outlines a Python script for training a Naive Bayes classifier using a dataset loaded from a CSV file. It processes text data from the 'Title' column to predict the 'Type' column, splitting the data into training and testing sets. The script evaluates the model's performance using a classification report and accuracy score.

Uploaded by

Md Asaduzzaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Clp

The document outlines a Python script for training a Naive Bayes classifier using a dataset loaded from a CSV file. It processes text data from the 'Title' column to predict the 'Type' column, splitting the data into training and testing sets. The script evaluates the model's performance using a classification report and accuracy score.

Uploaded by

Md Asaduzzaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import pandas as pd

from sklearn.model_selection import train_test_split


from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report, accuracy_score

# Load the dataset


file_path = 'dataset.csv' # Adjust the path as needed
dataset = pd.read_csv('/dataset.csv')

# Fix column names if necessary


dataset.rename(columns=lambda x: x.strip(), inplace=True)

# Use 'Title' as features and 'Type' as the target


X = dataset['Title']
y = dataset['Type']

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Convert text data to numerical data using CountVectorizer


vectorizer = CountVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)

# Train a Naive Bayes classifier


nb_classifier = MultinomialNB()
nb_classifier.fit(X_train_vec, y_train)

# Make predictions
y_pred = nb_classifier.predict(X_test_vec)

# Evaluate the model


classification_results = classification_report(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)

# Print the results


print("Classification Report:\n", classification_results)
print("Accuracy:", accuracy)

You might also like