如何在 sklearn 中执行多类多标签分类?
How to perform multiclass-multilabel classification in sklearn?
我有multiclass multioutput
分类(详见https://scikit-learn.org/stable/modules/multiclass.html)。换句话说,我的数据集如下所示。
node_name, feature1, feature2, ... label_1, label_2
node1, 1.2, 1.8, ..., 0, 2
node2, 1.0, 1.1, ..., 1, 1
node3, 1.9, 1.2, ..., 0, 3
...
...
...
所以,我的 label_1 可能是 0 or 1
,而我的 label_2 可能是 0, 1, or 2
.
因为我有两个标签(即 label_1 和 label_2),我的问题是如何将这些标签适合 sklearn 中的分类器?
在我当前的代码中,我使用 RandomForest
,如下所述。但是,我找不到描述如何将随机森林分类器转换为多类多标签分类的有用资源。如果 RandomForest 不支持多类多标签分类,我完全可以转而使用其他支持它们的分类器。我现在的代码如下。
clf = RandomForestClassifier(random_state = 42, class_weight="balanced")
k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
scores = cross_validate(clf, X, y, cv=k_fold, scoring = ('accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted', 'roc_auc'))
如果需要,我很乐意提供更多详细信息。
查看您提供的 link(在 'Support multiclass-multioutput:' 列表下)和 RandomForestClassifier(适合方法参数),RFC 似乎支持 multiclass-multioutput out of the bag。您需要做的就是在将 y 提供给 RFC 时正确格式化它。应该是:
y = np.array([['0', '2'], ['1', '1'], ['0', '3']])
对于您提供的前 3 个节点。
我有multiclass multioutput
分类(详见https://scikit-learn.org/stable/modules/multiclass.html)。换句话说,我的数据集如下所示。
node_name, feature1, feature2, ... label_1, label_2
node1, 1.2, 1.8, ..., 0, 2
node2, 1.0, 1.1, ..., 1, 1
node3, 1.9, 1.2, ..., 0, 3
...
...
...
所以,我的 label_1 可能是 0 or 1
,而我的 label_2 可能是 0, 1, or 2
.
因为我有两个标签(即 label_1 和 label_2),我的问题是如何将这些标签适合 sklearn 中的分类器?
在我当前的代码中,我使用 RandomForest
,如下所述。但是,我找不到描述如何将随机森林分类器转换为多类多标签分类的有用资源。如果 RandomForest 不支持多类多标签分类,我完全可以转而使用其他支持它们的分类器。我现在的代码如下。
clf = RandomForestClassifier(random_state = 42, class_weight="balanced")
k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
scores = cross_validate(clf, X, y, cv=k_fold, scoring = ('accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted', 'roc_auc'))
如果需要,我很乐意提供更多详细信息。
查看您提供的 link(在 'Support multiclass-multioutput:' 列表下)和 RandomForestClassifier(适合方法参数),RFC 似乎支持 multiclass-multioutput out of the bag。您需要做的就是在将 y 提供给 RFC 时正确格式化它。应该是:
y = np.array([['0', '2'], ['1', '1'], ['0', '3']])
对于您提供的前 3 个节点。