python-3.x - 无法在加州住房数据集上执行 sklearn.naive_bayes GaussianNB

标签 python-3.x machine-learning jupyter-notebook

出现错误 Unknown label type: (array([0.14999, 0.175 , 0.225 , ..., 4.991 , 5. , 5.00001]),)当尝试拟合数据集时。

from sklearn.datasets import fetch_california_housing
from sklearn.datasets import load_iris

cali = fetch_california_housing()

iris = load_iris()   
from sklearn.naive_bayes import GaussianNB


gnb = GaussianNB() # probabilistic
y_pred_cali = gnb.fit(cali.data, cali.target).predict(cali.data)

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-23-71ed3304ef0f> in <module>
     14 
     15 gnb = GaussianNB() # probabilistic
---> 16 y_pred_cali = gnb.fit(cali[0], cali[1]).predict(cali[0])
     17 

~\Anaconda3\lib\site-packages\sklearn\naive_bayes.py in fit(self, X, y, sample_weight)
    189         X, y = check_X_y(X, y)
    190         return self._partial_fit(X, y, np.unique(y), _refit=True,
--> 191                                  sample_weight=sample_weight)
    192 
    193     @staticmethod

~\Anaconda3\lib\site-packages\sklearn\naive_bayes.py in _partial_fit(self, X, y, classes, _refit, sample_weight)
    351             self.classes_ = None
    352 
--> 353         if _check_partial_fit_first_call(self, classes):
    354             # This is the first call to partial_fit:
    355             # initialize various cumulative counters

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in _check_partial_fit_first_call(clf, classes)
    318         else:
    319             # This is the first call to partial_fit
--> 320             clf.classes_ = unique_labels(classes)
    321             return True
    322 

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in unique_labels(*ys)
     92     _unique_labels = _FN_UNIQUE_LABELS.get(label_type, None)
     93     if not _unique_labels:
---> 94         raise ValueError("Unknown label type: %s" % repr(ys))
     95 
     96     ys_labels = set(chain.from_iterable(_unique_labels(y) for y in ys))

ValueError: Unknown label type: (array([0.14999, 0.175  , 0.225  , ..., 4.991  , 5.     , 5.00001]),)

最佳答案

该数据集有一个连续目标变量。

GNB是一种分类方法,而不是回归方法。 Y 需要是离散类,而不是连续变量。

关于python-3.x - 无法在加州住房数据集上执行 sklearn.naive_bayes GaussianNB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58870422/

相关文章:

machine-learning - 机器学习算法的标准化输入

machine-learning - Caffe 未知底部 Blob

matplotlib - IPython 中的 python-ggplot 截断图中的图例

javascript - 如何在 Bokeh/Python/Pywidgets 中使一个 slider /小部件更新多个绘图?

python - 如何在一行for循环中跳过没有值的键

python - 如何用双引号打印列表中的元素

python - 如何将 "Body"添加到 python mime 多部分(有附件)电子邮件

python-3.x - 为什么当我尝试创建自定义命令行选项时 pytest 给我一个 "unrecognized option"错误?

python - 使用 TensorFlowEstimator 进行增量训练

python - 位置应由两个数值组成