import numpy as npclass NaiveBayes def __init__self alpha=10 selfalpha = alpha # 学习率用于平滑概率 def fitself X y selfclasses = npuniquey selfnum_classes = lenselfclasses
The error is occurring because the line likelihood *= self.likelihoods[i, feature, value] is trying to access the self.likelihoods array using a float value value. However, the array indices must be integers.
To fix this issue, you can convert the value to an integer using the int() function before accessing the array element.
Change the line likelihood *= self.likelihoods[i, feature, value] to likelihood *= self.likelihoods[i, feature, int(value)] in the predict method.
Here's the updated code:
import numpy as np
class NaiveBayes:
def __init__(self, alpha=1.0):
self.alpha = alpha # 学习率,用于平滑概率
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)
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
This should resolve the IndexError and allow the code to run successfully
原文地址: https://www.cveoy.top/t/topic/hXY5 著作权归作者所有。请勿转载和采集!