python - 值错误 : Unknown label type: 'continuous-multioutput' when fitting data

标签 python machine-learning scikit-learn

我想使用 scikit-learnMultiOutputClassifier 根据一个输入参数预测多个结果。 出于某种原因,我总是收到此错误,而且我不知道是什么问题:

ValueError:未知标签类型:'continuous-multioutput'

我试过使 my_data['Clicked'] 成为分类数据,我试过这个 my_data['Clicked'] = my_data['Clicked'].astype(' category'),但它给了我同样的错误。

我已经在一些简单的虚拟数据库上尝试了相同的代码并且它运行良好。 这是有效的代码:

from sklearn.multioutput import MultiOutputClassifier
from sklearn.linear_model import LogisticRegression

dic = {'par_1': [10, 30, 13, 19, 25, 33, 23],
       'par_2': [1, 3, 1, 2, 3, 3, 2],
       'outcome': [101, 905, 182, 268, 646, 624, 465]}

df = pd.DataFrame(dic)

variables = df.iloc[:,:-1]
results = df.iloc[:,-1]

multi_output_clf = MultiOutputClassifier(LogisticRegression(solver='lbfgs'))
multi_output_clf.fit(results.values.reshape(-1, 1),variables)

x = multi_output_clf.predict([[100]])
print(x)

对于上面的代码,一切正常,但是对于下面的代码,我得到了错误。 我不知道问题出在哪里,因为我刚刚使用了更大的数据集和值,据此我预测参数仅为 0 和 1。 Zeros 和 Ones 应该是像 yesno 这样的类(类别),但是如果我将它们更改为“yes”和“no”,我会得到 无法转换的错误要 float 的字符串。为什么这不是连续的“结果”:[101, 905, 182, 268, 646, 624, 465] 但一系列 0 和 1 是连续的?

from sklearn.multioutput import MultiOutputClassifier
from sklearn.linear_model import LogisticRegression

variables = my_data[['Clicked']] #values are integers, only 0 and 1 (0 = not clicked , 1 = clicked)
results = my_data[['Daily Time on Site', 'Age', 'Gender']] #values are integers and floats

multi_output_clf = MultiOutputClassifier(LogisticRegression(solver='lbfgs'))
multi_output_clf.fit(variables.values.reshape(-1, 1),results)

x = multi_output_clf.predict([1])
print(x)

下面是我使用的完整数据集的一部分(它给了我同样的错误):

dic = {'Daily Time on Site': [59.99, 88.91, 66.00, 74.53, 69.88, 47.64, 83.07, 69.57],
       'Age': [23,33,48,30,20,49,37,48],
       'Gender': [1, 0, 1, 1, 1, 0, 1, 1],
       'Clicked': [0, 0, 1, 0, 0, 1, 0, 1]}

my_data = pd.DataFrame(dic)

variables = my_data[['Clicked']] #values are only 0 and 1 (0 = not clicked , 1 = clicked)
results = my_data[['Daily Time on Site', 'Age', 'Gender']] #values are integers and floats

multi_output_clf = MultiOutputClassifier(LogisticRegression(solver='lbfgs', multi_class='ovr'))
multi_output_clf.fit(variables.values.reshape(-1, 1),results)

x = multi_output_clf.predict([1])
print(x)

最佳答案

我认为您需要使用 MultiOutputRegressor(),因为您的输出变量似乎是连续的。

尝试以下更改:


variables  = my_data[['Clicked']] #values are only 0 and 1 (0 = not clicked , 1 = clicked)
results = my_data[['Daily Time on Site', 'Age', 'Gender']] #values are integers and floats

multi_output_clf = MultiOutputRegressor(LinearRegression())
multi_output_clf.fit(variables.values.reshape(-1, 1),results)

更新:

>>> pd.cut(my_data['Daily Time on Site'],
...        3, labels=["low", "medium", "high"])

0       low
1      high
2    medium
3    medium
4    medium
5       low
6      high
7    medium

注意:不建议使用整数作为您的类别,因为当您的变量值范围很大时,类别数量可能会非常高。请将它们分成更小的组,比如 10 或 20,然后将它们视为分类值。

关于python - 值错误 : Unknown label type: 'continuous-multioutput' when fitting data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657650/

相关文章:

python - 根据有趣单词的字典从电子邮件列表中识别最常见的单词

python - Discord Bot 无法离开语音 channel 'NoneType' 对象没有属性 'disconnect'

machine-learning - 朴素贝叶斯分类器——长度差异很大的文档

python - 用神经网络预测圆的半径

python - SKLearn 朴素贝叶斯 : add feature after tfidf vectorization

python - sklearn SVM 自定义内核

python - 如何检测参数网格中允许哪些值?

python - hvplot.box 中的不同颜色

python - 如何正确制作我自己的模块以在其中导入另一个模块?

python - 使用第一个形状元素作为 None 创建 Tensorflow 2.0 输入