首页 > 解决方案 > ValueError:未知标签类型:拟合数据时的“连续多输出”

问题描述

scikit-learn我想用's 的一个输入参数来预测多个结果MultiOutputClassifier。出于某种原因,我总是收到此错误,我不知道是什么问题:

ValueError: Unknown label type: '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。零和一应该是类(类别)yesno但如果我将它们更改为“是”和“否”,我会得到错误cannot convert string to 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)

标签: pythonmachine-learningscikit-learn

解决方案


我认为您需要选择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,然后将它们视为分类值。


推荐阅读