我已经开始使用Scikit-学习,我正在尝试训练和预测一个高斯朴素贝叶斯分类器。我不知道我做的很好,我希望有人能帮我。
PROBLEM:我输入了类型1的项目的X数量,作为响应,它们是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)在此之后,我使用train和listLabel训练分类器:
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。你能告诉我我做错了什么吗?请告诉我怎么解决?
发布于 2015-06-29 17:08:13
首先,由于您的数据具有信息不丰富的特性(所有数据的值相同),所以我稍微清理了一下:
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)因为它假设a和b都有正态分布,而且这种情况下的概率很低,所以你给出的前项-(1,400)是可以忽略不计的。您可以看到公式本身这里。顺便说一句,你可以得到确切的概率:
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.]]所以你没做错,这都是算计的问题。顺便说一句,我要求您用GaussianNB替换MultinomialNB,看看前面的内容是如何改变这一切的。
另外,除非您有很好的理由在这里使用GaussianNB,否则我会考虑使用某种树分类,因为在我看来,它可能更适合您的问题。
https://stackoverflow.com/questions/31120272
复制相似问题