这是为 x_test 和 y_test 获得相同个体的方法吗?

数据挖掘 Python scikit-学习 交叉验证
2022-03-10 09:55:53
x_train, x_test = train_test_split(x, test_size = 0.3,random_state=250)
y_train, y_test = train_test_split(y, test_size = 0.3,random_state=250)

这是使同一个人在x_testand 和 andy_testx_train重合的方法y_train吗?

2个回答

train_test_split内置此功能。只需在第一次传递所有数据,例如:

代码:

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=250)

测试代码:

from sklearn.model_selection import train_test_split

x = list(range(100))
y = list(range(100, 200))
x_train, x_test, y_train, y_test = train_test_split(
    x, y, test_size=0.3, random_state=250)

assert(all(xx == yy - 100 for xx, yy in zip(x_train, y_train)))
assert(all(xx == yy - 100 for xx, yy in zip(x_test, y_test)))

我建议使用索引将数据集拆分为训练并测试特征和目标。