shuffle矩阵合并
import numpy as np
def train_test_split(X, y, ratio, seed):
assert X.shape[0] == y.shape[0]
assert 0.0 <= ratio <= 1.0
if seed:
np.random.seed(seed)
data=np.hstack((X,y.reshape(X.shape[0],-1)))
np.random.shuffle(data)
train=data[:int(len(y) * ratio)]
test=data[int(len(y) * ratio):]
return train[:,:-1], train[:,-1], test[:,:-1], test[:,-1]
shuffle索引
import numpy as np
def train_test_split(X, y, ratio, seed):
assert X.shape[0] == y.shape[0]
assert 0.0 <= ratio <= 1.0
if seed:
np.random.seed(seed)
shuffle_index = np.random.permutation(len(y))
train_index = shuffle_index[:int(len(y) * ratio)]
test_index = shuffle_index[int(len(y) * ratio):]
return X[train_index], y[train_index], X[test_index], y[test_index]