朴素贝叶斯如何运作

问题描述 投票:1回答:1

我已经读过天真的贝叶斯,它是一种分类技术算法,可以根据你给出的数据进行预测,但在这个例子中我只是不知道输出[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]这是什么意思?

python scikit-learn statistics naivebayes
1个回答
0
投票

请仔细阅读以下示例代码。

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]。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.