要实现不使用随机化且保持原始顺序的train_test_split,可以按照以下步骤进行:
import numpy as np
def train_test_split(X, y, test_size):
# 计算测试集的样本数量
test_samples = int(len(X) * test_size)
# 划分训练集和测试集
X_train = X[:-test_samples]
y_train = y[:-test_samples]
X_test = X[-test_samples:]
y_test = y[-test_samples:]
return X_train, X_test, y_train, y_test
# 生成示例数据
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])
y = np.array([0, 1, 0, 1, 0])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
这样就可以在不使用随机化且保持原始顺序的情况下划分训练集和测试集。