朴素贝叶斯的工作原理

How the Naive Bayes works

我已经了解了朴素贝叶斯,它是一种分类技术算法,可以根据您提供的数据进行预测,但在这个例子中,我只是无法理解输出 [3,4] 是如何产生的。

下面的例子:

#assigning predictor and target variables
x= np.array([[-3,7],[1,5], [1,2], [-2,0], [2,3], [-4,0], [-1,1], [1,1], [-2,2], [2,7], [-4,1], [-2,7]])
Y = np.array([3, 3, 3, 3, 4, 3, 3, 4, 3, 4, 4, 4]

#Create a Gaussian Classifier
model = GaussianNB()

# Train the model using the training sets 
model.fit(x, y)

#Predict Output 
predicted= model.predict([[1,2],[3,4]])
print predicted

Output: ([3,4])

谁能解释一下在这种情况下 [3,4] 是如何生成的,这是什么意思?

请查看下面的示例代码。

from sklearn.naive_bayes import GaussianNB
import numpy as np

model = GaussianNB()
#assigning predictor and target variables
x= np.array([[-3,7],[1,5], [1,2], [-2,0], [2,3], [-4,0], [-1,1], [1,1], [-2,2], [2,7], [-4,1], [-2,7]])
Y = np.array([3, 3, 3, 3, 4, 3, 3, 4, 3, 4, 4, 4])
print x

model.fit(x, Y)

#Predict Output 
predicted= model.predict([[1,2],[35,6], [2,6]])
print predicted

输出:

[3 4 4]

这里我们得到了 3 个值。 3对应[1,2],4对应[35,6],以此类推

因此,根据您的样本量,您可以看到您得到 3 或 4 个值。因此,根据它为您的测试数据提供 [3,4] 的测试数据。希望这能澄清。

例如,根据您的代码,我只取前 3 个条目。你可以看到下面的图表。 X_1 和 X_2 是特征向量(输入),Y 是您的输出。基于输入和输出,算法生成一个数学公式。当您提供测试数据时,它使用相同的公式生成输出 (Y)。这就是您如何获得 [3,4]。