L有一个有10个类的200个例子的数据集。L希望将数据集分成50%的训练集和50%的测试集。
每节课L都有20个例子。因此,L希望每堂课都能得到10个训练范例和10个测试范例。
以下是我的课程:
classes=['BenchPress', 'ApplyLipstick', 'BabyCrawling', 'BandMarching', 'Archery', 'Basketball', 'ApplyEyeMakeup', 'BalanceBeam', 'BaseballPitch', 'BasketballDunk']L尝试了以下几点:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(final_data, true_label, test_size=0.50, random_state=42)然而,它返回50%的训练集和50 %的测试集,而不尊重每个班级的比例(l希望在测试集中得到10个例子,在每个班级中得到10个例子)。以下是由此产生的分裂:

发布于 2017-11-24 12:50:31
从0.17版本开始,train_test_split应该使用分层参数给出分层结果。
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(final_data, true_label, test_size=0.50, random_state=42, stratify=true_label)从有关参数分层的文档中:
分层:类似数组或无(默认为无),如果不是无,数据将以分层的方式分割,使用它作为标签数组。新版本0.17:分层分裂
希望这能有所帮助!
https://datascience.stackexchange.com/questions/25073
复制相似问题