我有一个包含数千个标签的极端多标签数据集,每个标签至少存在10次。
用分层的方式分割数据的最好方法是什么?
我尝试了scikit-multilearn的iterative_train_test_split函数,但是它没有成功。
有时内核崩溃,有时我会遇到一些奇怪的错误,比如KeyError: 'key of type tuple not found and not a MultiIndex'
。
我在带有M1处理器的mac上工作,如果它有任何改变的话。
谢谢
发布于 2021-09-22 15:36:39
通常,如果数据只包含用于分层的给定标签的一个数据条目,就会出现问题。因此,在执行分层之前,删除具有唯一标签的所有行。您可以使用collections.Counter类来完成这个任务。在删除这些行之后,很容易对数据进行分层,您正在使用我假设的数据,例如,
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)
发布于 2021-09-22 20:39:19
1000‘S的目标标签将导致问题的大多数标准桌面ML算法实现,我怀疑。一些公共DL神经网络可能能够根据您的数据和用例来处理这个问题。
对于桌面解决方案,我建议将您的问题分解为每个目标的单个二进制分类任务的1000‘S。
https://datascience.stackexchange.com/questions/102327
复制相似问题