代码之家  ›  专栏  ›  技术社区  ›  Mizlul

朴素的拜斯是如何运作的

  •  1
  • Mizlul  · 技术社区  · 6 年前

    我已经读过NaiveBayes,它是一种分类技术算法,可以根据您提供的数据进行预测,但是在这个例子中,我无法得到输出[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]是如何产生的吗?这意味着什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   LOrD_ARaGOrN    6 年前

    请仔细阅读下面的示例代码。

    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

    推荐文章