x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=1)
时间: 2023-08-03 18:04:33 浏览: 129
数据集分割train和test程序
在没有使用sklearn库的情况下,你可以自己实现一个简单的train_test_split函数来划分训练集和测试集。以下是一个示例代码:
```python
import numpy as np
def train_test_split(x, y, train_size=0.7, random_state=None):
if random_state is not None:
np.random.seed(random_state)
indices = np.arange(len(x))
np.random.shuffle(indices)
train_size = int(train_size * len(x))
train_indices = indices[:train_size]
test_indices = indices[train_size:]
x_train = x[train_indices]
x_test = x[test_indices]
y_train = y[train_indices]
y_test = y[test_indices]
return x_train, x_test, y_train, y_test
```
使用这个函数,你可以将输入的特征向量x和标签y按照指定的train_size比例划分为训练集和测试集。random_state参数用于控制随机划分的种子,以确保结果的可重复性。调用这个函数后,会返回划分好的x_train, x_test, y_train, y_test四个数组。你可以根据自己的需要进行后续的模型训练和评估。
阅读全文