手动实现train_test_split函数
时间: 2023-10-13 18:06:10 浏览: 41
可以使用 Python 的 random 模块手动实现 train_test_split 函数,具体实现方法如下:
```python
import random
def train_test_split(data, labels, test_size=0.2, random_state=42):
"""
Split dataset into train set and test set.
Parameters:
data: list, array or DataFrame, the input dataset
labels: list, array or Series, the labels of the input dataset
test_size: float, optional (default=0.2), the proportion of the test set
random_state: int, optional (default=42), the random seed
Returns:
train_data: list, array or DataFrame, the training set of the input dataset
test_data: list, array or DataFrame, the test set of the input dataset
train_labels: list, array or Series, the training labels of the input dataset
test_labels: list, array or Series, the test labels of the input dataset
"""
random.seed(random_state)
indices = list(range(len(data)))
random.shuffle(indices)
split_index = int(len(data) * (1 - test_size))
train_data = [data[i] for i in indices[:split_index]]
test_data = [data[i] for i in indices[split_index:]]
train_labels = [labels[i] for i in indices[:split_index]]
test_labels = [labels[i] for i in indices[split_index:]]
return train_data, test_data, train_labels, test_labels
```
以上是一个简单的手动实现train_test_split函数的示例,你可以根据自己的需求进行修改。