划分测试机和训练集的代码
时间: 2023-09-07 22:17:31 浏览: 40
以下是Python代码,用于将数据集划分为训练集和测试集:
```
import numpy as np
from sklearn.model_selection import train_test_split
# Load data
X = ... # input data
y = ... # output data
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Print the shapes of the training and testing sets
print("X_train shape:", X_train.shape)
print("y_train shape:", y_train.shape)
print("X_test shape:", X_test.shape)
print("y_test shape:", y_test.shape)
```
在代码中,我们使用了`train_test_split`函数来将数据集划分为训练集和测试集。`test_size`参数指定测试集的比例。在上述代码中,我们将测试集的比例设置为20%,即将80%的数据用于训练,20%的数据用于测试。`random_state`参数用于指定随机数生成器的种子,以便在每次运行代码时得到相同的结果。最后,我们打印出训练集和测试集的形状,以确保它们都正确地被划分。