import numpy as np

class NaiveBayes: 'Init method for Naive Bayes class' def init(self, alpha=1.0): self.alpha = alpha # 学习率,用于平滑概率

'Fit method for training the Naive Bayes model'
def fit(self, X, y):
    self.classes = np.unique(y)
    self.num_classes = len(self.classes)
    self.num_features = X.shape[1]
    
    # 计算每个类别的先验概率
    self.priors = np.zeros(self.num_classes)
    for i, c in enumerate(self.classes):
        self.priors[i] = np.sum(y == c) / len(y)
    
    # 计算每个特征的条件概率
    self.likelihoods = np.zeros((self.num_classes, self.num_features, 256))
    for i, c in enumerate(self.classes):
        X_c = X[y == c]
        for feature in range(self.num_features):
            for value in range(256):
                self.likelihoods[i, feature, value] = (np.sum(X_c[:, feature] == value) + self.alpha) / (len(X_c) + self.alpha * 256)

'Predict method for making predictions using the trained model'
def predict(self, X):
    y_pred = []
    for x in X:
        posteriors = []
        for i, c in enumerate(self.classes):
            likelihood = 1.0
            for feature, value in enumerate(x):
                likelihood *= self.likelihoods[i, feature, int(value)]
            posterior = self.priors[i] * likelihood
            posteriors.append(posterior)
        y_pred.append(self.classes[np.argmax(posteriors)])
    return y_pred

Load the Iris dataset

iris = datasets.load_iris() X = iris.data Y = iris.target

Split the dataset into training, validation, and test sets

X_train, X_remain, y_train, y_remain = train_test_split(X, Y, test_size=0.3, random_state=42) # 训练集划分为训练集和验证集 X_val, X_test, y_val, y_test = train_test_split(X_remain, y_remain, test_size=0.3/(0.3+0.1), random_state=42)

Create a Naive Bayes classifier

naive_bayes = NaiveBayes(alpha=1.0)

Train the model

naive_bayes.fit(X_train, y_train)

Predict on the validation set

y_val_pred = naive_bayes.predict(X_val) print('Validation Accuracy:', np.mean(y_val_pred == y_val))

Predict on the test set

y_test_pred = naive_bayes.predict(X_test) print('Test Accuracy:', np.mean(y_test_pred == y_test))

Naive Bayes Classifier Implementation in Python

原文地址: https://www.cveoy.top/t/topic/pG5S 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录