当Sklearn朴素贝叶斯与浮点数一起使用时,未知的标签类型错误 [英] Unknown label type error when Sklearn naive bayes used with floating point numbers

查看:139
本文介绍了当Sklearn朴素贝叶斯与浮点数一起使用时,未知的标签类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我的数据应用朴素贝叶斯算法,该算法以浮点数标记.如果我的Y数组由int类型值组成,则预测正确进行.请参见以下代码:

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

但是,如果我将Y的值更改为浮点数,则会收到错误消息.请参见以下代码:

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

我应该如何处理朴素贝叶斯中的浮动数组?我想映射这两个X和Y

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

推荐答案

一个简单的回归示例:

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 

我建议探索linear_model中的其他选项.

I would recommend exploring other options in linear_model.

这篇关于当Sklearn朴素贝叶斯与浮点数一起使用时,未知的标签类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆