gpt4 book ai didi

python - 随机化数据拆分以训练和测试此功能

转载 作者:行者123 更新时间:2023-11-28 22:16:50 25 4
gpt4 key购买 nike

我写了一个函数,根据总大小的百分比将 numpy ndarrays x_datay_data 分成训练和测试数据。

函数如下:

def split_data_into_training_testing(x_data, y_data, percentage_split):
number_of_samples = x_data.shape[0]
p = int(number_of_samples * percentage_split)

x_train = x_data[0:p]
y_train = y_data[0:p]

x_test = x_data[p:]
y_test = y_data[p:]

return x_train, y_train, x_test, y_test

在此函数中,根据percentage_split,顶部的数据样本进入训练数据集,底部的数据样本进入测试数据集。在输入机器学习模型之前,如何使这种数据拆分更加随机?

最佳答案

假设您自己实现这个而不是使用 sklearn.train_test_split 是有原因的,您可以打乱一组索引(这不会影响训练数据)并对其进行索引。

def split_data_into_training_testing(x_data, y_data, split, shuffle=True):
idx = np.arange(len(x_data))
if shuffle:
np.random.shuffle(idx)

p = int(len(x_data) * split)
x_train = x_data[idx[:p]]
x_test = x_data[idx[p:]]
... # Similarly for y_train and y_test.

return x_train, x_test, y_train, y_test

关于python - 随机化数据拆分以训练和测试此功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com