我正在尝试拟合机器学习NB模型,并且一切都很干净,但是可以将我的功能参考标签以适应模型:
labels = [[0,0,0,1,1,0],[0,0,1,0,1,1],...]]
features = [[[0.1,0.2,0.3,0.4,0.5],[0.11,0.21,0.31,0.41,0.51],[0.12,0.22,0.32,0.42,0.52],[0.12,0.22,0.32,0.42,0.52],[0.12,0.22,0.32,0.43,0.53]],[0.13,0.23,0.33,0.43,0.53]...]]]
在我的问题中,[0.1,0.2,0.3,0.4,0.5]必须引用标签中的第一个0,因此分类器给出了否,[0.11,0.21,0.31,0.41,0.51]引用了第二个0 ,也为否,[0.12,0.22,0.32,0.42,0.52]表示前1个,因此分类器为是。
如何拟合NB分类器或重新排列列表以适合模型?
非常感谢您。
答案 0 :(得分:1)
您可能可以利用精彩的numpy
库,通过该库可以以多种不同方式重新组织输入数据的形状。可能的决定之一如下:
import numpy as np
from sklearn.naive_bayes import GaussianNB
labels = [[0,0,0,1,1,0],
[0,0,1,0,1,1]]
features = [[[0.1,0.2,0.3,0.4,0.5],
[0.11,0.21,0.31,0.41,0.51],
[0.12,0.22,0.32,0.42,0.52],
[0.12,0.22,0.32,0.42,0.52],
[0.12,0.22,0.32,0.43,0.53],
[0.13,0.23,0.33,0.43,0.53]],
[[0.1,0.2,0.3,0.4,0.5],
[0.11,0.21,0.31,0.41,0.51],
[0.12,0.22,0.32,0.42,0.52],
[0.12,0.22,0.32,0.42,0.52],
[0.12,0.22,0.32,0.43,0.53],
[0.13,0.23,0.33,0.43,0.53]]]
labels = np.ravel(labels)
features = np.reshape(features, (-1, 5))
gnb = GaussianNB()
gnb.fit(features, labels)