We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
作者在文中提到,将w展开为一个个独立特征,那么就可以将p(w/ci)展开为p(w0, w1, ... | ci)。那么在整个数据集中,应该有 p(w0_0) + p(w0_1) = 1。 但是在实际代码中,计算不同类别 p(w0) 处分母时,却加上了该数据行中单词出现的总次数,这里应该是有误的。如果将每个特征看做独立,这里应该只需要加1。
def trainNB0(trainMatrix, trainCategory): nTrainDocs = len(trainMatrix) nWords = len(trainMatrix[0]) pAbusive = sum(trainCategory) / float(nTrainDocs) p0Num = np.zeros(nWords) p1Num = np.zeros(nWords) p0Denom = 0 p1Denom = 1 for i in range(nTrainDocs): if trainCategory[i] == 1: p1Num += trainMatrix[i] # 此处将分母加上了所有单词出现的次数 # p1Denom += sum(trainMatrix[i]) p1Denom += 1 else: p0Num += trainMatrix[i] # p0Denom += sum(trainMatrix[i]) p0Denom += 1 p1Vect = p1Num / p1Denom p0Vect = p0Num / p0Denom return p0Vect, p1Vect, pAbusive
The text was updated successfully, but these errors were encountered:
Sorry, something went wrong.
No branches or pull requests
作者在文中提到,将w展开为一个个独立特征,那么就可以将p(w/ci)展开为p(w0, w1, ... | ci)。那么在整个数据集中,应该有 p(w0_0) + p(w0_1) = 1。
但是在实际代码中,计算不同类别 p(w0) 处分母时,却加上了该数据行中单词出现的总次数,这里应该是有误的。如果将每个特征看做独立,这里应该只需要加1。
The text was updated successfully, but these errors were encountered: