提供几个用于数据集划分的python代码
时间: 2023-03-25 16:02:27 浏览: 97
Python数据集切分实例
5星 · 资源好评率100%
以下是几个用于数据集划分的 Python 代码示例:
1. 随机划分数据集:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=42)
```
2. 分层划分数据集:
```python
from sklearn.model_selection import StratifiedShuffleSplit
split = StratifiedShuffleSplit(n_splits=1, test_size=.2, random_state=42)
for train_index, test_index in split.split(X, y):
X_train = X[train_index]
y_train = y[train_index]
X_test = X[test_index]
y_test = y[test_index]
```
3. 时间序列划分数据集:
```python
from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5)
for train_index, test_index in tscv.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
```
希望这些代码能够帮助你进行数据集划分。
阅读全文