【问题标题】:How can I solve this unknown label type error?如何解决此未知标签类型错误?
【发布时间】:2019-10-11 16:50:25
【问题描述】:

我正在从 NSL-KDD 数据集中进行特征选择。预处理后,我的 X-DoS 数据类型如下:

type_of_target(X_newDoS)
'continuous-multioutput'

和 Y_DoS 一样

type_of_target(Y_DoS)
'unkonwn'

我将特征选择部分运行为:

from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier

clf =RandomForestClassifier( n_jobs = 2)

rfe = RFE(clf, n_features_to_select=1)
rfe.fit(X_newDoS, Y_DoS)

错误信息:

ValueError                                Traceback (most recent call 
last)
<ipython-input-31-6c22f9cc2bba> in <module>()
     12 rfe = RFE(clf, n_features_to_select=1)
---> 13 rfe.fit(X_newDoS, Y_DoS)
     14

4 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/multiclass.py in 
check_classification_targets(y)
    167     if y_type not in ['binary', 'multiclass', 'multiclass- 
multioutput',
    168                       'multilabel-indicator', 'multilabel- 
sequences']:
--> 169         raise ValueError("Unknown label type: %r" % y_type)
    170  

ValueError: Unknown label type: 'unknown'

X_newDoS 是一个 numpy 数组,Y_DoS 是一个维度数组 (125972,2)。单击 multiclass.py 文件,我看到列表中没有“未知”类型。我尝试将 Y_DoS 数组转换为 numpy 数组:

Y_DoS = np.array(Y_DoS)

它仍然是未知的数据类型,无法被 multiclass.py 文件识别。我有什么方法可以解决这个问题?如何将 Y_DoS 变量设置为 multiclass.py 文件可识别的另一种类型,而不会丢失其内容和结构? 作为参考,我使用了此链接中的代码,并为预处理完成了相同的步骤。 https://github.com/CynthiaKoopman/Network-Intrusion-Detection/blob/master/DecisionTree_IDS.ipynb

我对机器学习很陌生。该程序在 numpy 1.11.3、sklearn 0.18.1 和 pandas 1.19.2 上运行良好。使用当前预装的 colab 库版本(numpy 0.24.2、sklearn 1.16.3、pandas 0.21.1)时,会引发上述错误。

【问题讨论】:

    标签: python-3.x scikit-learn google-colaboratory feature-selection


    【解决方案1】:

    没关系。似乎 Y_DoS 变量恰好是一个未定义的对象,因此 sklearn 无法识别它的类型。添加

    Y_DoS = Y_DoS.astype('int') 
    

    在学习步骤解决问题并将 Y_DoS 归类为“二进制”类型之前。

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2019-10-16
      • 2014-01-10
      • 2021-12-26
      • 2020-07-14
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多