Search code examples
pythonmachine-learningscikit-learngaussiannaivebayes

Unknown label type error when Sklearn naive bayes used with floating point numbers


I am applying Naive Bayes algorithm on my data which is labelled by floating point numbers. If my Y array consists of int type value then the prediction is coming correctly. See the below code:

import numpy as np

X = np.array([[0], [1]])

Y = np.array([1, 2])

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X, Y)

print (clf.predict([[0]]))

Output is [1]

String values are also working. See the below code:

import numpy as np

X = np.array([[0], [1]])

Y = np.array(['A', 'B'])

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X, Y)

print (clf.predict([[0]]))

Output is ['A']

But if I change the value of Y to floating numbers then I am getting an error. See the below code:

import numpy as np

X = np.array([[0], [1]])

Y = np.array([0.1, 0.2])

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X, Y)

print (clf.predict([[0]]))


Error : ValueError: Unknown label type: array([ 0.1,  0.2]) 

How should I deal with floating arrays in Naive Bayes? I want to map these two X and Y

X = np.array([[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30]])

Y = np.array([0.0, 0.03333333333333333, 0.06666666666666667, 0.1, 0.13333333333333333, 0.16666666666666666, 0.2, 0.23333333333333334, 0.26666666666666666, 0.3, 0.3333333333333333, 0.36666666666666664, 0.4, 0.43333333333333335, 0.4666666666666667, 0.5, 0.5333333333333333, 0.5666666666666667, 0.6, 0.6333333333333333, 0.6666666666666666, 0.7, 0.7333333333333333, 0.7666666666666667, 0.8, 0.8333333333333334, 0.8666666666666667, 0.9, 0.9333333333333333, 0.9666666666666667, 1.0])

Solution

  • A simple regression example:

    from sklearn import linear_model                                                                                                                                              
    from sklearn import datasets                                                                                                                                                  
    from sklearn import metrics    
    boston = datasets.load_boston()                                                                                                                                                      
    
    
    X_train = boston.data[:450]  #define training X set                                                                                                                           
    y_train = boston.target[:450] #define training y set                                                                                                                          
    
    X_test = boston.data[450:]  #define test X set                                                                                                                                
    y_test = boston.target[450:] #define test y set                                                                                                                               
    
    lin = linear_model.LinearRegression() #initialize regressor                                                                                                                   
    
    lin.fit(X_train, y_train) #fit training data                                                                                                                                  
    preds = lin.predict(X_test) #make prediction on X test set                                                                                                                    
    
    print metrics.mean_absolute_error(y_test, preds) #evaluate performance 
    

    I would recommend exploring other options in linear_model.