1from mltrain.supervised.NaiveBayes import NaiveBayes 2 3# Initialize the model 4model = NaiveBayes() 5 6# Train the model 7model.train(X_train, y_train) 8 9# Make predictions 10y_pred = model.predict(X_test) 11 12# Get Confusion Matrix 13model.confusion_matrix(y_test,y_pred) 14 15
The NaiveBayes
class implements the Naive Bayes algorithm for classification tasks. It calculates class statistics such as mean, variance, and prior probabilities to make predictions based on the provided training data. The class provides methods to train the model, make predictions, and evaluate performance using accuracy and confusion matrix.
__init__(self)
Initializes the Naive Bayes model with empty dictionaries for storing class statistics.
train(self, X, y)
Trains the Naive Bayes model by calculating the mean, variance, and prior probability for each class based on the training data.
predict(self, X)
Predicts the class labels for the provided input samples based on the trained model.
accuracy(self, y_true, y_pred)
Calculates the accuracy of the model's predictions.
confusion_matrix(self, y_true, y_pred)
Computes the confusion matrix to evaluate the performance of the classification model.
- The predict
method calculates the likelihood of each class for a given sample and selects the class with the highest likelihood.
- The accuracy
and confusion_matrix
methods are used to evaluate the model's performance on the test data.