首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于极端多标签分类的测试分割

用于极端多标签分类的测试分割
EN

Data Science用户
提问于 2021-09-22 14:13:18
回答 2查看 1.1K关注 0票数 2

我有一个包含数千个标签的极端多标签数据集,每个标签至少存在10次。

用分层的方式分割数据的最好方法是什么?

我尝试了scikit-multilearn的iterative_train_test_split函数,但是它没有成功。

有时内核崩溃,有时我会遇到一些奇怪的错误,比如KeyError: 'key of type tuple not found and not a MultiIndex'

我在带有M1处理器的mac上工作,如果它有任何改变的话。

谢谢

EN

回答 2

Data Science用户

发布于 2021-09-22 15:36:39

通常,如果数据只包含用于分层的给定标签的一个数据条目,就会出现问题。因此,在执行分层之前,删除具有唯一标签的所有行。您可以使用collections.Counter类来完成这个任务。在删除这些行之后,很容易对数据进行分层,您正在使用我假设的数据,例如,

代码语言:javascript
运行
复制
from sklearn import datasets
from sklearn.model_selection import train_test_split

iris = datasets.load_iris()
X = pd.DataFrame(iris.data)
y = iris.target
# these labels will not cause any problems
X['cat'] = np.random.choice(['label1','label2','label3','label4'],len(X))

# but these ones will, because they are unique
X.loc[37, 'cat'] = 'label5'
X.loc[137, 'cat'] = 'label6'

# this row will raise an exception if uncommented
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 
#                                                    random_state=43, stratify = X.cat)

# let's drop rows with unique labels
from collections import Counter
unique_labels = [lab for lab, count in Counter(X.cat).items() if count == 1]

print(f"unique labels to be dropped: {unique_labels}")

# drop rows with unique labels
X = X[~X.cat.isin(unique_labels)]
y = y[X.index]

# now datasets X and y can be used in train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 
                                                    random_state=43, stratify = X.cat)
票数 3
EN

Data Science用户

发布于 2021-09-22 20:39:19

1000‘S的目标标签将导致问题的大多数标准桌面ML算法实现,我怀疑。一些公共DL神经网络可能能够根据您的数据和用例来处理这个问题。

对于桌面解决方案,我建议将您的问题分解为每个目标的单个二进制分类任务的1000‘S。

票数 0
EN
页面原文内容由Data Science提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://datascience.stackexchange.com/questions/102327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档