【发布时间】:2022-12-24 09:29:33
【问题描述】:
对于一个学校项目,我需要将一个数据集分成训练集和测试集,并给出一定的比例。该比率是用作训练集的数据量,而其余的将用作测试集。我根据教授的要求创建了一个基本实现,但我无法让它通过他创建的测试。下面是我的实现以及参数和返回变量代表什么
def splitData(X, y, split_ratio = 0.8):
'''
X: numpy.ndarray. Shape = [n+1, m]
y: numpy.ndarray. Shape = [m, ]
split_ratio: the ratio of examples go into the Training, Validation, and Test sets.
Split the whole dataset into Training, Validation, and Test sets.
:return: return (training_X, training_y), (test_X, test_y).
training_X is a (n+1, m_tr) matrix with m_tr training examples;
training_y is a (m_tr, ) column vector;
test_X is a (n+1, m_test) matrix with m_test test examples;
test_y is a (m_test, ) column vector.
'''
## Need to possible shuffle X array and Y array
## amount used for training
m_tr = len(X) * train_ratio
##m_test = len(X) - m_tr Amount that is used for testing
training_X = X[1:m_tr]
training_y = y[1:m_tr]
test_X = [m_tr:len(X)]
test_y = [m_tr:len(y)]
return training_X, training_y, test_X, test_y
由于说明,我包含了声明 m_test 的评论,但我很确定将数组从第一个元素拆分为 m_tr 给出了总训练量,其余部分是测试数据。通过迭代从 m_tr 到 len(x) 或 len(y) 的每个列表来找到测试数据。我误解了拆分的工作原理吗?
PS - 教授说我们可以跳过验证的拆分。
【问题讨论】:
标签: python numpy machine-learning