【问题标题】:Error: Supported target types are: ('binary', 'multiclass')错误:支持的目标类型是:('binary', 'multiclass')
【发布时间】:2020-11-08 10:42:55
【问题描述】:

如何处理错误ValueError: Supported target types are: ('binary', 'multiclass'). Got 'continuous-multioutput' instead

我尝试了from sklearn.utils.multiclass import type_of_targetx[0],y[0],但没有成功...

X 的可视化:

Y 的可视化:

X.shape, Y.shape

((336, 10), (336, 5))

深度学习模型:

for train, test in kfold.split(X, Y):

    model = Sequential()
    model.add(Dense(10, input_dim=20, 
                kernel_regularizer=l2(0.001),
                kernel_initializer=VarianceScaling(), 
                activation='sigmoid'))
    model.add(Dense(5, 
                kernel_regularizer=l2(0.01),
                kernel_initializer=VarianceScaling(),                 
                activation='sigmoid'))
    
    model.compile(loss='binary_crossentropy', optimizer='adam', 
              metrics=['acc'])
    
    model.fit(X[train], Y[train], epochs=50, batch_size=25, verbose = 0,
              validation_data=(X[test], Y[test]))

    scores = model.evaluate(X[test], Y[test], verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[2], scores[2]*100))
    cvscores.append(scores[2] * 100)
---------------------------------------------------------------------------
ValueError: Supported target types are: ('binary', 'multiclass'). Got 'continuous-multioutput' instead.

【问题讨论】:

标签: python keras scikit-learn neural-network sequential


【解决方案1】:

StratifiedKFold 不打算用于多标签目标,正如 here 已经指出的那样。它需要一个一维数组来确定如何拆分索引。

我想您想根据概率最高的标签拆分目标。实现这一目标的一种方法是创建一个指示具有最高概率的目标的一维数组,并将其传递给StratifiedKFold,而不是多标签目标。

假设您在 pandas DataFrame y 中有示例数据,它看起来像这样:

       0      1    2    3    4
0  0.966  0.000  0.0  0.2  0.0
1  0.966  0.000  0.0  0.0  0.2
2  0.000  0.966  0.5  0.0  0.0
3  0.000  0.966  0.0  0.0  0.0
4  0.966  0.000  0.0  0.0  0.0

然后,使用idxmax 创建一个新对象,以找到概率最高的目标:

y_max = y.idxmax(axis=1)

这会给你这样的输出:

0    0
1    0
2    1
3    1
4    0
dtype: int64

现在您可以将此数组传递给StratifiedKFold 并获取您需要的索引:

for train, test in kfold.split(X, y_max):
    ...

    model.fit(X[train], Y[train], epochs=50, batch_size=25, verbose = 0,
              validation_data=(X[test], Y[test]))

    scores = model.evaluate(X[test], Y[test], verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[2], scores[2]*100))
    cvscores.append(scores[2] * 100)

这样,您可以从一维数组中获取索引,并且仍然使用原始数据进行训练和测试。如果你的数据恰好在一个 numpy 数组中,同样可以使用 numpy 的argmax 函数来实现。

【讨论】:

  • 非常感谢您的帮助,但是在如上所述进行测试时,返回输出:Found input variables with inconsistent numbers of samples: [6, 336]
  • 有什么建议吗?
  • [6, 336] 很有趣。如果是[5, 336],我可能已经猜到轴有问题了......你能确认错误再次指向拆分函数吗?生成的一维数组的形状和内容是什么?是 numpy 数组还是 pandas 系列?
  • 我的 Y 是 pandas 数据框 pd.DataFrame (Y) .idxmax (axis = 1)
  • Length: 336, dtype: int64
猜你喜欢
  • 2021-05-03
  • 2021-12-18
  • 2020-02-23
  • 2021-10-02
  • 2018-07-08
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
相关资源
最近更新 更多