# Creating subsets of Epochs for Sleep stage classification from polysomnography (PSG) data exercise

**URL:** <https://mne.discourse.group/t/creating-subsets-of-epochs-for-sleep-stage-classification-from-polysomnography-psg-data-exercise/7078>\
**Category:** Support & Discussions\
**Tags:** eeg\
**Created:** [June 13, 2023, 10:06pm UTC](https://mne.discourse.group/t/creating-subsets-of-epochs-for-sleep-stage-classification-from-polysomnography-psg-data-exercise/7078 "2023-06-13T22:06:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnAtl](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/johnatl/32/1223_2.png) [@JohnAtl](https://mne.discourse.group/u/JohnAtl)\
**Post date:** [June 13, 2023, 10:06pm UTC](https://mne.discourse.group/t/creating-subsets-of-epochs-for-sleep-stage-classification-from-polysomnography-psg-data-exercise/7078/1 "2023-06-13T22:06:06Z")

</div>

I’m working through this exercise:

> Fetch 50 subjects from the Physionet database and run a 5-fold cross-validation leaving each time 10 subjects out in the test set.

Easy-peasy, right? 🙄

I can’t seem to figure out how to go from a bunch of datasets to a bunch of epochs, then use train\_ndx, test\_ndx from skf.split() to select from those epochs and call pipe.fit() with them.  
In the code below, the problem is that X\_train, y\_train are lists of Epochs, and (naturally) don’t have a compute\_psd() method.

I would greatly appreciate any suggestions!

```python
pipe = make_pipeline(
    FunctionTransformer(eeg_power_band, validate=False),
    RandomForestClassifier(n_estimators=100, random_state=42),
)

from sklearn.model_selection import StratifiedKFold

# create lists of epochs and corresponding events
X = []
y = []
skf = StratifiedKFold(n_splits=5)
for ndx in range(len(epochs)):
    epochs[ndx].drop_bad()
    for ep_ndx in range(len(epochs[ndx])):
        X.append(epochs[ndx][ep_ndx])
        y.append(epochs[ndx][ep_ndx].events[:, 2][0])

# skf.split() gives us indices into the lists to create stratified train & test epochs
for train_ndx, test_ndx in skf.split(np.zeros(len(y)), y):
    X_train = []
    y_train = []
    for ndx in train_ndx:
        X_train.append(X[ndx])
        y_train.append(y[ndx])
    X_test = []
    y_test = []
    for ndx in test_ndx:
        X_test.append(X[ndx])
        y_test.append(y[ndx])

    # fit the training data (errors here)
    pipe.fit(X_train, y_train)
    y_pred = pipe.predict(X_test)
    acc = accuracy_score(y_test, y_pred)
    print(f"Accuracy score: {acc}")

```

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [June 14, 2023, 7:11am UTC](https://mne.discourse.group/t/creating-subsets-of-epochs-for-sleep-stage-classification-from-polysomnography-psg-data-exercise/7078/2 "2023-06-14T07:11:11Z")

</div>

just make a big numpy array stacking all the data so you have X = (n\_epochs, n\_channels, n\_times) and y = (n\_epochs,)  
Make a variable called group of size (n\_epochs) so that group[i] = j is epoch i for subject j

then use [https://scikit-learn.org/stable/modules/generated/sklearn.model\_selection.StratifiedGroupKFold.html](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedGroupKFold.html)  
using a cross\_val\_score function

assuming your estimator can take 3D inputs

HTH  
A
