Skip to content
New issue

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

朴素贝叶斯代码中的一个问题,个人看法。 #20

Open
mike96265 opened this issue Dec 13, 2021 · 1 comment
Open

朴素贝叶斯代码中的一个问题,个人看法。 #20

mike96265 opened this issue Dec 13, 2021 · 1 comment

Comments

@mike96265
Copy link

作者在文中提到,将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
@lingjjcn
Copy link

lingjjcn commented Dec 13, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants