【问题标题】:SciKit-learn--Gaussian Naive Bayes ImplementantionSciKit-learn--高斯朴素贝叶斯实现
【发布时间】:2015-09-16 04:29:55
【问题描述】:

我已经开始使用 Scikit-learn,我正在尝试训练和预测一个高斯朴素贝叶斯分类器。我不知道我做得很好,我希望有人可以帮助我。

问题:我输入了 X 个类型 1 的项目,我得到的响应是它们是类型 0

我是怎么做到的: 为了生成训练数据,我这样做:

 #this is of type 1
    ganado={
            "Hora": "16:43:35",
            "Fecha": "19/06/2015",
            "Tiempo": 10,
            "Brazos": "der",
            "Sentado": "no",
            "Puntuacion Final Pasteles": 50,
            "Nombre": "usuario1",
            "Puntuacion Final Botellas": 33
        }
    #this is type 0
    perdido={
            "Hora": "16:43:35",
            "Fecha": "19/06/2015",
            "Tiempo": 10,
            "Brazos": "der",
            "Sentado": "no",
            "Puntuacion Final Pasteles": 4,
            "Nombre": "usuario1",
            "Puntuacion Final Botellas": 3
        }
    train=[]
    for repeticion in range(0,400):
        train.append(ganado)

    for repeticion in range(0,1):
            train.append(perdido)

我用这个弱条件标记数据:

listLabel=[]
for data in train:
    condition=data["Puntuacion Final Pasteles"]+data["Puntuacion Final Botellas"]       
    if condition<20:
        listLabel.append(0)
    else:
        listLabel.append(1)

然后我生成这样的测试数据:

  #this should be type 1
    pruebaGanado={
            "Hora": "16:43:35",
            "Fecha": "19/06/2015",
            "Tiempo": 10,
            "Brazos": "der",
            "Sentado": "no",
            "Puntuacion Final Pasteles": 10,
            "Nombre": "usuario1",
            "Puntuacion Final Botellas": 33
        }
    #this should be type 0
    pruebaPerdido={
            "Hora": "16:43:35",
            "Fecha": "19/06/2015",
            "Tiempo": 10,
            "Brazos": "der",
            "Sentado": "no",
            "Puntuacion Final Pasteles": 2,
            "Nombre": "usuario1",
            "Puntuacion Final Botellas": 3
        }
        test=[]
        for repeticion in range(0,420):
            test.append(pruebaGanado)
            test.append(pruebaPerdido)

之后,我使用trainlistLabel来训练分类器:

vec = DictVectorizer()
X=vec.fit_transform(train)
gnb = GaussianNB()
trained=gnb.fit(X.toarray(),listLabel)

一旦我训练了分类器,我就会使用数据进行测试

testX=vec.fit_transform(test)
predicted=trained.predict(testX.toarray())

最后结果总是0。你能告诉我我做错了什么以及如何解决它吗?

【问题讨论】:

  • 如果对你有帮助,请采纳答案,让其他人也可以借鉴...

标签: python machine-learning scikit-learn


【解决方案1】:

首先,由于您的数据具有不提供信息的特征(所有数据的值相同),因此我对其进行了一些清理:

ganado={
    "a": 50,
    "b": 33
}
perdido={
        "a": 4,
        "b": 3
    }
pruebaGanado={
        "a": 10,
        "b": 33
    }
pruebaPerdido={
        "a": 2,
        "b": 3
    }

其余的都不重要,清理代码将帮助您专注于重要的事情。

现在,高斯朴素贝叶斯全都与概率有关:您可能会注意到,分类器试图告诉您:

P((a,b)=(10,33)|class=0)*P(class=0)   >   P((a,b)=(10,33)|class=1)*P(class=1)

因为它假设ab 都具有正态分布,并且这种情况下的概率非常低,所以你给它的先验-(1,400) 可以忽略不计。您可以看到公式本身here。 顺便说一下,你可以得到确切的概率:

t = [pruebaGanado,pruebaPerdido]
t = vec.fit_transform(t)
print model.predict_proba(t.toarray())
#prints:
[[ 1.  0.]
[ 1.  0.]]

所以分类器确定 0 是正确的类。现在,让我们稍微改变一下测试数据:

pruebaGanado={
    "Puntuacion Final Pasteles": 20,
    "Puntuacion Final Botellas": 33
}

现在我们有了:

[[ 0.  1.]
[ 1.  0.]]

所以你没有做错任何事,这都是计算的问题。顺便说一句,我挑战你用MultinomialNB 替换GaussianNB,看看先验如何改变这一切。

另外,除非您有充分的理由在这里使用GaussianNB,否则我会考虑使用某种树分类,因为我认为它可能更适合您的问题。

【讨论】:

    猜你喜欢
    • 2012-04-09
    • 2017-06-21
    • 2013-01-02
    • 2021-02-09
    • 2016-05-01
    • 2015-08-27
    • 2017-11-13
    • 2013-12-10
    • 2018-06-19
    相关资源
    最近更新 更多