import numpy as np


def train_test_split(X, y, test_ratio=0.2, seed=None):
"""将数据 X 和 y 按照test_ratio分割成X_train, X_test, y_train, y_test"""
assert X.shape[0] == y.shape[0], \
"the size of X must be equal to the size of y"
assert 0.0 <= test_ratio <= 1.0, \
"test_ration must be valid"

if seed:
np.random.seed(seed)

shuffled_indexes = np.random.permutation(len(X))

test_size = int(len(X) * test_ratio)
test_indexes = shuffled_indexes[:test_size]
train_indexes = shuffled_indexes[test_size:]

X_train = X[train_indexes]
y_train = y[train_indexes]

X_test = X[test_indexes]
y_test = y[test_indexes]

return X_train, X_test, y_train, y_test

相关文章:

  • 2021-07-15
  • 2021-08-01
  • 2021-04-15
  • 2021-06-04
  • 2021-07-26
猜你喜欢
  • 2021-12-14
  • 2021-12-20
  • 2021-12-18
  • 2021-12-09
  • 2022-12-23
  • 2021-10-27
  • 2021-07-19
相关资源
相似解决方案