Commit ed5e98f1 authored by slavenc's avatar slavenc
Browse files

removed unnecessary comments

parent 160d393f
...@@ -561,9 +561,7 @@ ...@@ -561,9 +561,7 @@
"def AR(signal, order=6):\n", "def AR(signal, order=6):\n",
" ar, _, _ = arburg(signal, order) # only save AR coefs\n", " ar, _, _ = arburg(signal, order) # only save AR coefs\n",
" return ar\n", " return ar\n",
"\n", "\n"
"# Wavelets analysis\n",
"# import pywt\n"
] ]
}, },
{ {
...@@ -1041,7 +1039,9 @@ ...@@ -1041,7 +1039,9 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 8,
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
...@@ -1154,7 +1154,9 @@ ...@@ -1154,7 +1154,9 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 148, "execution_count": 148,
"metadata": {}, "metadata": {
"collapsed": true
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Project 4 # Project 4
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Dependencies and Constants ### Dependencies and Constants
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import time import time
import numpy as np import numpy as np
from numpy.fft import fft # to get amplitudes from numpy.fft import fft # to get amplitudes
import pandas as pd import pandas as pd
import scipy.signal as ss # for psd import scipy.signal as ss # for psd
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from sklearn.svm import SVC from sklearn.svm import SVC
from sklearn.metrics import balanced_accuracy_score from sklearn.metrics import balanced_accuracy_score
from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedShuffleSplit from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedShuffleSplit
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier, ExtraTreesClassifier from sklearn.ensemble import BaggingClassifier, RandomForestClassifier, ExtraTreesClassifier
from biosppy.signals import eeg # signal processing from biosppy.signals import eeg # signal processing
from biosppy.signals import emg # signal processing from biosppy.signals import emg # signal processing
from spectrum import arburg from spectrum import arburg
import keras import keras
from keras.models import Sequential from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout, BatchNormalization from keras.layers import LSTM, Dense, Dropout, BatchNormalization
PROTOTYPING = False PROTOTYPING = False
``` ```
%%%% Output: stream %%%% Output: stream
C:\Users\made_\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. C:\Users\made_\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters from ._conv import register_converters as _register_converters
Using TensorFlow backend. Using TensorFlow backend.
C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)]) _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)]) _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)]) _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)]) _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)]) _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. C:\Users\made_\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)]) np_resource = np.dtype([("resource", np.ubyte, 1)])
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Read data ### Read data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
start = time.time() start = time.time()
# import train sets # import train sets
train_eeg1_raw = pd.read_csv('files/train_eeg1.csv').drop('Id', axis=1).values train_eeg1_raw = pd.read_csv('files/train_eeg1.csv').drop('Id', axis=1).values
train_eeg2_raw = pd.read_csv('files/train_eeg2.csv').drop('Id', axis=1).values train_eeg2_raw = pd.read_csv('files/train_eeg2.csv').drop('Id', axis=1).values
train_emg_raw = pd.read_csv('files/train_emg.csv').drop('Id', axis=1).values train_emg_raw = pd.read_csv('files/train_emg.csv').drop('Id', axis=1).values
# import test sets # import test sets
test_eeg1_raw = pd.read_csv('files/test_eeg1.csv').drop('Id', axis=1).values test_eeg1_raw = pd.read_csv('files/test_eeg1.csv').drop('Id', axis=1).values
test_eeg2_raw = pd.read_csv('files/test_eeg2.csv').drop('Id', axis=1).values test_eeg2_raw = pd.read_csv('files/test_eeg2.csv').drop('Id', axis=1).values
test_emg_raw = pd.read_csv('files/test_emg.csv').drop('Id', axis=1).values test_emg_raw = pd.read_csv('files/test_emg.csv').drop('Id', axis=1).values
# import eeg features directly # import eeg features directly
eeg_train = pd.read_csv('files/eeg_feats_train.csv').values eeg_train = pd.read_csv('files/eeg_feats_train.csv').values
eeg_test = pd.read_csv('files/eeg_feats_test.csv').values eeg_test = pd.read_csv('files/eeg_feats_test.csv').values
# import emg features directly # import emg features directly
emg_feats_train = pd.read_csv('files/emg_feats_train.csv').values emg_feats_train = pd.read_csv('files/emg_feats_train.csv').values
emg_feats_test = pd.read_csv('files/emg_feats_test.csv').values emg_feats_test = pd.read_csv('files/emg_feats_test.csv').values
# import reduced eeg features by pca (to 45 components - already scaled) # import reduced eeg features by pca (to 45 components - already scaled)
eeg_train_red = pd.read_csv('files/eeg_train_pca45.csv').values eeg_train_red = pd.read_csv('files/eeg_train_pca45.csv').values
eeg_test_red = pd.read_csv('files/eeg_test_pca45.csv').values eeg_test_red = pd.read_csv('files/eeg_test_pca45.csv').values
# import labels # import labels
train_labels_raw = pd.read_csv('files/train_labels.csv').drop('Id', axis=1).values train_labels_raw = pd.read_csv('files/train_labels.csv').drop('Id', axis=1).values
print(train_eeg1_raw.shape, train_eeg2_raw.shape, train_emg_raw.shape) print(train_eeg1_raw.shape, train_eeg2_raw.shape, train_emg_raw.shape)
print(test_eeg1_raw.shape, test_eeg2_raw.shape, test_emg_raw.shape) print(test_eeg1_raw.shape, test_eeg2_raw.shape, test_emg_raw.shape)
print(train_labels_raw.shape) print(train_labels_raw.shape)
print(eeg_train.shape, eeg_test.shape) print(eeg_train.shape, eeg_test.shape)
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: stream %%%% Output: stream
(64800, 512) (64800, 512) (64800, 512) (64800, 512) (64800, 512) (64800, 512)
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-2-60963c3fdbae> in <module> <ipython-input-2-60963c3fdbae> in <module>
27 27
28 print(train_eeg1_raw.shape, train_eeg2_raw.shape, train_emg_raw.shape) 28 print(train_eeg1_raw.shape, train_eeg2_raw.shape, train_emg_raw.shape)
---> 29 print(test_eeg1_raw.shape, test_eeg2_raw.shape, test_emg_raw.shape) ---> 29 print(test_eeg1_raw.shape, test_eeg2_raw.shape, test_emg_raw.shape)
30 print(train_labels_raw.shape) 30 print(train_labels_raw.shape)
31 print(eeg_train.shape, eeg_test.shape) 31 print(eeg_train.shape, eeg_test.shape)
NameError: name 'test_emg_raw' is not defined NameError: name 'test_emg_raw' is not defined
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Feature extraction for EEG signals ### Feature extraction for EEG signals
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
start = time.time() start = time.time()
def calculate_statistics(list_values): def calculate_statistics(list_values):
n5 = np.nanpercentile(list_values, 5) n5 = np.nanpercentile(list_values, 5)
n25 = np.nanpercentile(list_values, 25) n25 = np.nanpercentile(list_values, 25)
n75 = np.nanpercentile(list_values, 75) n75 = np.nanpercentile(list_values, 75)
n95 = np.nanpercentile(list_values, 95) n95 = np.nanpercentile(list_values, 95)
median = np.nanpercentile(list_values, 50) median = np.nanpercentile(list_values, 50)
mean = np.nanmean(list_values) mean = np.nanmean(list_values)
std = np.nanstd(list_values) std = np.nanstd(list_values)
var = np.nanvar(list_values) var = np.nanvar(list_values)
rms = np.nanmean(np.sqrt(list_values**2)) rms = np.nanmean(np.sqrt(list_values**2))
return [n5, n25, n75, n95, median, mean, std, var, rms] return [n5, n25, n75, n95, median, mean, std, var, rms]
def calculate_crossings(list_values): def calculate_crossings(list_values):
zero_crossing_indices = np.nonzero(np.diff(np.array(list_values) > 0))[0] zero_crossing_indices = np.nonzero(np.diff(np.array(list_values) > 0))[0]
no_zero_crossings = len(zero_crossing_indices) no_zero_crossings = len(zero_crossing_indices)
mean_crossing_indices = np.nonzero(np.diff(np.array(list_values) > np.nanmean(list_values)))[0] mean_crossing_indices = np.nonzero(np.diff(np.array(list_values) > np.nanmean(list_values)))[0]
no_mean_crossings = len(mean_crossing_indices) no_mean_crossings = len(mean_crossing_indices)
return [no_zero_crossings, no_mean_crossings] return [no_zero_crossings, no_mean_crossings]
def get_features(list_values): def get_features(list_values):
crossings = calculate_crossings(list_values) crossings = calculate_crossings(list_values)
statistics = calculate_statistics(list_values) statistics = calculate_statistics(list_values)
return crossings + statistics return crossings + statistics
def extract_features(eeg1, eeg2, emg): def extract_features(eeg1, eeg2, emg):
features = None features = None
for i in range(eeg1.shape[0]): for i in range(eeg1.shape[0]):
if i % 1000 == 0: if i % 1000 == 0:
print(i, "/", eeg1.shape[0]) print(i, "/", eeg1.shape[0])
row = np.array([]) row = np.array([])
signal = np.array([eeg1[i], eeg2[i]]).T signal = np.array([eeg1[i], eeg2[i]]).T
analysis = eeg.eeg(signal=signal, sampling_rate=128, show=False) analysis = eeg.eeg(signal=signal, sampling_rate=128, show=False)
# theta # theta
row = np.append(row, get_features(analysis["theta"])) row = np.append(row, get_features(analysis["theta"]))
# row = np.append(row, get_features(analysis["theta"][:, 1])) # row = np.append(row, get_features(analysis["theta"][:, 1]))
# alpha low # alpha low
row = np.append(row, get_features(analysis["alpha_low"])) row = np.append(row, get_features(analysis["alpha_low"]))
# row = np.append(row, get_features(analysis["alpha_low"][:, 1])) # row = np.append(row, get_features(analysis["alpha_low"][:, 1]))
# alpha low # alpha low
row = np.append(row, get_features(analysis["alpha_high"])) row = np.append(row, get_features(analysis["alpha_high"]))
# row = np.append(row, get_features(analysis["alpha_high"][:, 1])) # row = np.append(row, get_features(analysis["alpha_high"][:, 1]))
# beta # beta
row = np.append(row, get_features(analysis["beta"])) row = np.append(row, get_features(analysis["beta"]))
# row = np.append(row, get_features(analysis["beta"][:, 1])) # row = np.append(row, get_features(analysis["beta"][:, 1]))
# gamma # gamma
row = np.append(row, get_features(analysis["gamma"][:, 0])) row = np.append(row, get_features(analysis["gamma"][:, 0]))
# row = np.append(row, get_features(analysis["gamma"])) # row = np.append(row, get_features(analysis["gamma"]))
# format # format
row = row.reshape((1, -1)) row = row.reshape((1, -1))
# concatenate # concatenate
if features is None: if features is None:
features = row features = row
else: else:
features = np.concatenate((features, row), axis=0) features = np.concatenate((features, row), axis=0)
return features return features
X_train = extract_features(train_eeg1_raw, train_eeg2_raw, train_emg_raw) X_train = extract_features(train_eeg1_raw, train_eeg2_raw, train_emg_raw)
if not PROTOTYPING: if not PROTOTYPING:
X_test = extract_features(test_eeg1_raw, test_eeg2_raw, test_emg_raw) X_test = extract_features(test_eeg1_raw, test_eeg2_raw, test_emg_raw)
print("X_test", X_test.shape) print("X_test", X_test.shape)
print("X_train", X_train.shape) print("X_train", X_train.shape)
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: stream %%%% Output: stream
0 / 64800 0 / 64800
1000 / 64800 1000 / 64800
2000 / 64800 2000 / 64800
3000 / 64800 3000 / 64800
4000 / 64800 4000 / 64800
5000 / 64800 5000 / 64800
6000 / 64800 6000 / 64800
7000 / 64800 7000 / 64800
8000 / 64800 8000 / 64800
9000 / 64800 9000 / 64800
10000 / 64800 10000 / 64800
11000 / 64800 11000 / 64800
12000 / 64800 12000 / 64800
13000 / 64800 13000 / 64800
14000 / 64800 14000 / 64800
15000 / 64800 15000 / 64800
16000 / 64800 16000 / 64800
17000 / 64800 17000 / 64800
18000 / 64800 18000 / 64800
19000 / 64800 19000 / 64800
20000 / 64800 20000 / 64800
21000 / 64800 21000 / 64800
22000 / 64800 22000 / 64800
23000 / 64800 23000 / 64800
24000 / 64800 24000 / 64800
25000 / 64800 25000 / 64800
26000 / 64800 26000 / 64800
27000 / 64800 27000 / 64800
28000 / 64800 28000 / 64800
29000 / 64800 29000 / 64800
30000 / 64800 30000 / 64800
31000 / 64800 31000 / 64800
32000 / 64800 32000 / 64800
33000 / 64800 33000 / 64800
34000 / 64800 34000 / 64800
35000 / 64800 35000 / 64800
36000 / 64800 36000 / 64800
37000 / 64800 37000 / 64800
38000 / 64800 38000 / 64800
39000 / 64800 39000 / 64800
40000 / 64800 40000 / 64800
41000 / 64800 41000 / 64800
42000 / 64800 42000 / 64800
43000 / 64800 43000 / 64800
44000 / 64800 44000 / 64800
45000 / 64800 45000 / 64800
46000 / 64800 46000 / 64800
47000 / 64800 47000 / 64800
48000 / 64800 48000 / 64800
49000 / 64800 49000 / 64800
50000 / 64800 50000 / 64800
51000 / 64800 51000 / 64800
52000 / 64800 52000 / 64800
53000 / 64800 53000 / 64800
54000 / 64800 54000 / 64800
55000 / 64800 55000 / 64800
56000 / 64800 56000 / 64800
57000 / 64800 57000 / 64800
58000 / 64800 58000 / 64800
59000 / 64800 59000 / 64800
60000 / 64800 60000 / 64800
61000 / 64800 61000 / 64800
62000 / 64800 62000 / 64800
63000 / 64800 63000 / 64800
64000 / 64800 64000 / 64800
0 / 43200 0 / 43200
1000 / 43200 1000 / 43200
2000 / 43200 2000 / 43200
3000 / 43200 3000 / 43200
4000 / 43200 4000 / 43200
5000 / 43200 5000 / 43200
6000 / 43200 6000 / 43200
7000 / 43200 7000 / 43200
8000 / 43200 8000 / 43200
9000 / 43200 9000 / 43200
10000 / 43200 10000 / 43200
11000 / 43200 11000 / 43200
12000 / 43200 12000 / 43200
13000 / 43200 13000 / 43200
14000 / 43200 14000 / 43200
15000 / 43200 15000 / 43200
16000 / 43200 16000 / 43200
17000 / 43200 17000 / 43200
18000 / 43200 18000 / 43200
19000 / 43200 19000 / 43200
20000 / 43200 20000 / 43200
21000 / 43200 21000 / 43200
22000 / 43200 22000 / 43200
23000 / 43200 23000 / 43200
24000 / 43200 24000 / 43200
25000 / 43200 25000 / 43200
26000 / 43200 26000 / 43200
27000 / 43200 27000 / 43200
28000 / 43200 28000 / 43200
29000 / 43200 29000 / 43200
30000 / 43200 30000 / 43200
31000 / 43200 31000 / 43200
32000 / 43200 32000 / 43200
33000 / 43200 33000 / 43200
34000 / 43200 34000 / 43200
35000 / 43200 35000 / 43200
36000 / 43200 36000 / 43200
37000 / 43200 37000 / 43200
38000 / 43200 38000 / 43200
39000 / 43200 39000 / 43200
40000 / 43200 40000 / 43200
41000 / 43200 41000 / 43200
42000 / 43200 42000 / 43200
43000 / 43200 43000 / 43200
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last) AttributeError Traceback (most recent call last)
<ipython-input-3-2aadf25068c8> in <module> <ipython-input-3-2aadf25068c8> in <module>
72 X_test = extract_features(test_eeg1_raw, test_eeg2_raw, test_emg_raw) 72 X_test = extract_features(test_eeg1_raw, test_eeg2_raw, test_emg_raw)
73 # save features for future imports 73 # save features for future imports
---> 74 pd.DataFrame.to_csv(X_train, 'files/eeg_feats_train.csv') ---> 74 pd.DataFrame.to_csv(X_train, 'files/eeg_feats_train.csv')
75 pd.DataFrame.to_csv(X_tests, 'files/eeg_feats_test.csv') 75 pd.DataFrame.to_csv(X_tests, 'files/eeg_feats_test.csv')
76 print("X_test", X_test.shape) 76 print("X_test", X_test.shape)
~\Anaconda3\lib\site-packages\pandas\core\generic.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal) ~\Anaconda3\lib\site-packages\pandas\core\generic.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal)
3200 """ 3200 """
3201 3201
-> 3202 df = self if isinstance(self, ABCDataFrame) else self.to_frame() -> 3202 df = self if isinstance(self, ABCDataFrame) else self.to_frame()
3203 3203
3204 from pandas.io.formats.csvs import CSVFormatter 3204 from pandas.io.formats.csvs import CSVFormatter
AttributeError: 'numpy.ndarray' object has no attribute 'to_frame' AttributeError: 'numpy.ndarray' object has no attribute 'to_frame'
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# save features for future imports # save features for future imports
pd.DataFrame.to_csv(pd.DataFrame(X_train), 'files/eeg_feats_train.csv', index=False) pd.DataFrame.to_csv(pd.DataFrame(X_train), 'files/eeg_feats_train.csv', index=False)
pd.DataFrame.to_csv(pd.DataFrame(X_test), 'files/eeg_feats_test.csv', index=False) pd.DataFrame.to_csv(pd.DataFrame(X_test), 'files/eeg_feats_test.csv', index=False)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# obtain features by simply doing a FFT on the data # obtain features by simply doing a FFT on the data
# probably more suitable for a neural network approach # probably more suitable for a neural network approach
eeg1_freqs_train = [] eeg1_freqs_train = []
eeg2_freqs_train = [] eeg2_freqs_train = []
eeg1_freqs_test = [] eeg1_freqs_test = []
eeg2_freqs_test = [] eeg2_freqs_test = []
for i in range(train_eeg1_raw.shape[0]): for i in range(train_eeg1_raw.shape[0]):
eeg1_freqs_train.append(np.real(fft(train_eeg1_raw[i]))) eeg1_freqs_train.append(np.real(fft(train_eeg1_raw[i])))
eeg2_freqs_train.append(np.real(fft(train_eeg2_raw[i]))) eeg2_freqs_train.append(np.real(fft(train_eeg2_raw[i])))
for i in range(test_eeg1_raw.shape[0]): for i in range(test_eeg1_raw.shape[0]):
eeg1_freqs_test.append(np.real(fft(test_eeg1_raw[i]))) eeg1_freqs_test.append(np.real(fft(test_eeg1_raw[i])))
eeg2_freqs_test.append(np.real(fft(test_eeg2_raw[i]))) eeg2_freqs_test.append(np.real(fft(test_eeg2_raw[i])))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# concatenate frequency features from fft # concatenate frequency features from fft
start = time.time() start = time.time()
eeg_freqs_train = np.array(np.column_stack((eeg1_freqs_train, eeg2_freqs_train))) eeg_freqs_train = np.array(np.column_stack((eeg1_freqs_train, eeg2_freqs_train)))
eeg_freqs_test = np.array(np.column_stack((eeg1_freqs_test, eeg2_freqs_test))) eeg_freqs_test = np.array(np.column_stack((eeg1_freqs_test, eeg2_freqs_test)))
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
# save features for future imports # save features for future imports
#pd.DataFrame.to_csv(pd.DataFrame(eeg_freqs_train), 'files/eeg_freqs_train.csv', index=False) #pd.DataFrame.to_csv(pd.DataFrame(eeg_freqs_train), 'files/eeg_freqs_train.csv', index=False)
#pd.DataFrame.to_csv(pd.DataFrame(eeg_freqs_test), 'files/eeg_freqs_test.csv', index=False) #pd.DataFrame.to_csv(pd.DataFrame(eeg_freqs_test), 'files/eeg_freqs_test.csv', index=False)
``` ```
%%%% Output: stream %%%% Output: stream
Time: 2.0603151321411133 Time: 2.0603151321411133
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### homemade Feature Extraction for EMG signals ### homemade Feature Extraction for EMG signals
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# functions are implemented from this paper: # functions are implemented from this paper:
# https://www.researchgate.net/publication/323587464_A_Comprehensive_Study_on_EMG_Feature_Extraction_and_Classifiers # https://www.researchgate.net/publication/323587464_A_Comprehensive_Study_on_EMG_Feature_Extraction_and_Classifiers
# https://www.researchgate.net/publication/224148281_Evaluation_of_EMG_Feature_Extraction_for_Hand_Movement_Recognition_Based_on_Euclidean_Distance_and_Standard_Deviation # https://www.researchgate.net/publication/224148281_Evaluation_of_EMG_Feature_Extraction_for_Hand_Movement_Recognition_Based_on_Euclidean_Distance_and_Standard_Deviation
# Functions for the TIME Domain # Functions for the TIME Domain
# integrated EMG is the area under the rectified EMG signal # integrated EMG is the area under the rectified EMG signal
def IEMG(signal): def IEMG(signal):
iemg = np.sum(np.abs(signal)) iemg = np.sum(np.abs(signal))
return iemg return iemg
# Mean Absolute Value # Mean Absolute Value
# PRE : Requires rectified signal # PRE : Requires rectified signal
def MAV(signal, N): def MAV(signal, N):
mav = np.sum(np.abs(signal))/N mav = np.sum(np.abs(signal))/N
return mav return mav
# Mean Absolute Value Slope (potentially computationally very expensive) # Mean Absolute Value Slope (potentially computationally very expensive)
def MAVS(signal, N): def MAVS(signal, N):
temp = 0 temp = 0
for i in range(signal.shape[0]-1): for i in range(signal.shape[0]-1):
temp += np.abs(signal[i+1] - signal[i]) temp += np.abs(signal[i+1] - signal[i])
mavs = temp/N mavs = temp/N
return mavs return mavs
# modified mean absolute value type 1 # modified mean absolute value type 1
def MAV1(signal, N): def MAV1(signal, N):
# interval borders # interval borders
lower = 0.25 * N lower = 0.25 * N
upper = 0.75 * N upper = 0.75 * N
temp = 0 temp = 0
for i in range(signal.shape[0]): for i in range(signal.shape[0]):
if i >= lower and i <= upper: if i >= lower and i <= upper:
temp += 1 * np.abs(signal[i]) temp += 1 * np.abs(signal[i])
else: else:
temp += 0.5 * np.abs(signal[i]) temp += 0.5 * np.abs(signal[i])
mav1 = temp/N mav1 = temp/N
return mav1 return mav1
# modified mean absolute value type 2 # modified mean absolute value type 2
def MAV2(signal, N): def MAV2(signal, N):
# interval borders # interval borders
lower = 0.25 * N lower = 0.25 * N
upper = 0.75 * N upper = 0.75 * N
temp = 0 temp = 0
for i in range(signal.shape[0]): for i in range(signal.shape[0]):
if i >= lower and i <= upper: if i >= lower and i <= upper:
temp += 1 * np.abs(signal[i]) temp += 1 * np.abs(signal[i])
elif i < lower: elif i < lower:
temp += (4*i/N) * np.abs(signal[i]) temp += (4*i/N) * np.abs(signal[i])
elif i > upper: elif i > upper:
temp += (4*(i-N)/N) * np.abs(signal[i]) temp += (4*(i-N)/N) * np.abs(signal[i])
mav2 = temp/N mav2 = temp/N
return mav2 return mav2
# Simple Square Integral (SSI) expresses the energy of the EMG signal # Simple Square Integral (SSI) expresses the energy of the EMG signal
# PRE : Requires rectified signal # PRE : Requires rectified signal
def SSI(signal, N): def SSI(signal, N):
ssi = np.sum(np.abs(signal)**2)/N # should square every value in signal element-wise ssi = np.sum(np.abs(signal)**2)/N # should square every value in signal element-wise
return ssi return ssi
# The variance of EMG signal # The variance of EMG signal
# PRE : Requires rectified signal # PRE : Requires rectified signal
def VAREMG(signal, N): def VAREMG(signal, N):
varemg = np.sum(signal**2)/(N-1) # should square every value in signal element-wise varemg = np.sum(signal**2)/(N-1) # should square every value in signal element-wise
return varemg return varemg
# Root Mean Square # Root Mean Square
# PRE : Requires rectified signal # PRE : Requires rectified signal
def RMS(signal, N): def RMS(signal, N):
rms = np.sqrt(np.sum(np.abs(signal)**2)/N) # should square every value in signal element-wise rms = np.sqrt(np.sum(np.abs(signal)**2)/N) # should square every value in signal element-wise
return rms return rms
# the 3rd temporal moment # the 3rd temporal moment
def TM3(signal, N): def TM3(signal, N):
tm3 = np.sum(np.abs(signal**3))/N tm3 = np.sum(np.abs(signal**3))/N
return tm3 return tm3
# the 4th temporal moment # the 4th temporal moment
def TM4(signal, N): def TM4(signal, N):
tm4 = np.sum(np.abs(signal**4))/N tm4 = np.sum(np.abs(signal**4))/N
return tm4 return tm4
# the 5th temporal moment # the 5th temporal moment
def TM5(signal, N): def TM5(signal, N):
tm5 = np.sum(np.abs(signal**5))/N tm5 = np.sum(np.abs(signal**5))/N
return tm5 return tm5
# Waveform Length # Waveform Length
def WL(signal, N): def WL(signal, N):
wl = 0 wl = 0
temp = 0 temp = 0
for j in range(signal.shape[0]-1): for j in range(signal.shape[0]-1):
temp = np.abs(signal[j+1] - signal[j]) temp = np.abs(signal[j+1] - signal[j])
wl += temp wl += temp
return wl return wl
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# https://www.researchgate.net/publication/51997893_Techniques_for_Feature_Extraction_from_EMG_Signal # https://www.researchgate.net/publication/51997893_Techniques_for_Feature_Extraction_from_EMG_Signal
# Functions for the FREQUENCY Domain # Functions for the FREQUENCY Domain
# frequency median : requires the power spectrum density # frequency median : requires the power spectrum density
def FMD(psd): def FMD(psd):
fmd = 0.5 * np.sum(psd) fmd = 0.5 * np.sum(psd)
return fmd return fmd
# frequency mean : requires psd, freqs and frequency median for faster computation # frequency mean : requires psd, freqs and frequency median for faster computation
def FMN(psd, freqs, fmd): def FMN(psd, freqs, fmd):
fmd = fmd * 2 # simply sum of all psd elements fmd = fmd * 2 # simply sum of all psd elements
fmn = np.sum(np.multiply(psd, freqs))/fmd fmn = np.sum(np.multiply(psd, freqs))/fmd
return fmn return fmn
# same as FMD(), but based on amplitudes # same as FMD(), but based on amplitudes
def MMFD(amplitudes): def MMFD(amplitudes):
mmfd = 0.5 * np.sum(amplitudes) mmfd = 0.5 * np.sum(amplitudes)
return mmfd return mmfd
# same as FMD(), but based on amplitudes # same as FMD(), but based on amplitudes
def MMNF(signal, amplitudes, mmfd): def MMNF(signal, amplitudes, mmfd):
freqs = np.fft.fftfreq(amplitudes.size) # freqs based on fourier transform freqs = np.fft.fftfreq(amplitudes.size) # freqs based on fourier transform
mmnf = np.sum(np.multiply(amplitudes, freqs))/mmfd mmnf = np.sum(np.multiply(amplitudes, freqs))/mmfd
return mmnf return mmnf
# estimate the AR coefficients of k-th order (k=6 based on literature research) # estimate the AR coefficients of k-th order (k=6 based on literature research)
def AR(signal, order=6): def AR(signal, order=6):
ar, _, _ = arburg(signal, order) # only save AR coefs ar, _, _ = arburg(signal, order) # only save AR coefs
return ar return ar
# Wavelets analysis
# import pywt
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# PRE : raw emg signal # PRE : raw emg signal
# POST: returns the extracted features # POST: returns the extracted features
def extract_features_emg(data): def extract_features_emg(data):
N = data.shape[0] N = data.shape[0]
#onsets_list = [] # save onsets of EMG signals #onsets_list = [] # save onsets of EMG signals
#filtered_list = [] #filtered_list = []
# generate more features # generate more features
mav_list = [] mav_list = []
ssi_list = [] ssi_list = []
vemg_list= [] vemg_list= []
rms_list = [] rms_list = []
wl_list = [] wl_list = []
iemg_list= [] iemg_list= []
mavs_list= [] mavs_list= []
mav1_list= [] mav1_list= []
mav2_list= [] mav2_list= []
tm3_list = [] tm3_list = []
tm4_list = [] tm4_list = []
tm5_list = [] tm5_list = []
fmd_list = [] fmd_list = []
fmn_list = [] fmn_list = []
mmfd_list= [] mmfd_list= []
mmnf_list= [] mmnf_list= []
ar_list = [] ar_list = []
start = time.time() start = time.time()
for i in range(data.shape[0]): for i in range(data.shape[0]):
_, filt_emg, _ = emg.emg(signal=data[i].T, sampling_rate=512, show=False) # obtain only filtered signal _, filt_emg, _ = emg.emg(signal=data[i].T, sampling_rate=512, show=False) # obtain only filtered signal
freqs, psd = ss.welch(data[i], fs=512) # get the PSD of the signal for the frequencies and amplitudes freqs, psd = ss.welch(data[i], fs=512) # get the PSD of the signal for the frequencies and amplitudes
amplitudes = np.abs(fft(data[i])) amplitudes = np.abs(fft(data[i]))
#filtered_list.append(filt_emg) #filtered_list.append(filt_emg)
#onsets_list.append(onsets_emg) #onsets_list.append(onsets_emg)
# compute features # compute features
mav_list.append(MAV(filt_emg, N)) mav_list.append(MAV(filt_emg, N))
ssi_list.append(SSI(filt_emg, N)) ssi_list.append(SSI(filt_emg, N))
vemg_list.append(VAREMG(filt_emg, N)) vemg_list.append(VAREMG(filt_emg, N))
rms_list.append(RMS(filt_emg, N)) rms_list.append(RMS(filt_emg, N))
wl_list.append(WL(filt_emg, N)) wl_list.append(WL(filt_emg, N))
iemg_list.append(IEMG(filt_emg)) iemg_list.append(IEMG(filt_emg))
mavs_list.append(MAVS(filt_emg, N)) mavs_list.append(MAVS(filt_emg, N))
mav1_list.append(MAV1(filt_emg, N)) mav1_list.append(MAV1(filt_emg, N))
mav2_list.append(MAV2(filt_emg, N)) mav2_list.append(MAV2(filt_emg, N))
tm3_list.append(TM3(filt_emg, N)) tm3_list.append(TM3(filt_emg, N))
tm4_list.append(TM4(filt_emg, N)) tm4_list.append(TM4(filt_emg, N))
tm5_list.append(TM5(filt_emg, N)) tm5_list.append(TM5(filt_emg, N))
fmd_res = FMD(psd) fmd_res = FMD(psd)
fmd_list.append(fmd_res) fmd_list.append(fmd_res)
fmn_list.append(FMN(psd, freqs, fmd_res)) fmn_list.append(FMN(psd, freqs, fmd_res))
mmfd_res = MMFD(amplitudes) mmfd_res = MMFD(amplitudes)
mmfd_list.append(mmfd_res) mmfd_list.append(mmfd_res)
mmnf_list.append(MMNF(data[i], amplitudes, mmfd_res)) mmnf_list.append(MMNF(data[i], amplitudes, mmfd_res))
ar_list.append(AR(filt_emg)) ar_list.append(AR(filt_emg))
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
emg_features = [mav_list,ssi_list,vemg_list,rms_list,wl_list,iemg_list,mavs_list,mav1_list,mav2_list, emg_features = [mav_list,ssi_list,vemg_list,rms_list,wl_list,iemg_list,mavs_list,mav1_list,mav2_list,
tm3_list,tm4_list,tm5_list,fmd_list,fmn_list,mmfd_list,mmnf_list,ar_list] tm3_list,tm4_list,tm5_list,fmd_list,fmn_list,mmfd_list,mmnf_list,ar_list]
return emg_features return emg_features
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# get emg features for X_train and X_test # get emg features for X_train and X_test
emg_feats_train = extract_features_emg(train_emg_raw) emg_feats_train = extract_features_emg(train_emg_raw)
emg_feats_test = extract_features_emg(test_emg_raw) emg_feats_test = extract_features_emg(test_emg_raw)
``` ```
%%%% Output: stream %%%% Output: stream
Time: 1361.9812316894531 Time: 1361.9812316894531
Time: 859.1871929168701 Time: 859.1871929168701
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# extract the coefs and save them in separate lists # extract the coefs and save them in separate lists
def extract_ar_coefs(features): def extract_ar_coefs(features):
ar_feats_0 = [] ar_feats_0 = []
ar_feats_1 = [] ar_feats_1 = []
ar_feats_2 = [] ar_feats_2 = []
ar_feats_3 = [] ar_feats_3 = []
ar_feats_4 = [] ar_feats_4 = []
ar_feats_5 = [] ar_feats_5 = []
# 17th idx is where the AR coefs list is in # 17th idx is where the AR coefs list is in
# we only care for the real part. the complex part is 0j anyway # we only care for the real part. the complex part is 0j anyway
for i in range(len(features[16])): for i in range(len(features[16])):
ar_feats_0.append(np.real(features[16][i][0])) ar_feats_0.append(np.real(features[16][i][0]))
ar_feats_1.append(np.real(features[16][i][1])) ar_feats_1.append(np.real(features[16][i][1]))
ar_feats_2.append(np.real(features[16][i][2])) ar_feats_2.append(np.real(features[16][i][2]))
ar_feats_3.append(np.real(features[16][i][3])) ar_feats_3.append(np.real(features[16][i][3]))
ar_feats_4.append(np.real(features[16][i][4])) ar_feats_4.append(np.real(features[16][i][4]))
ar_feats_5.append(np.real(features[16][i][5])) ar_feats_5.append(np.real(features[16][i][5]))
return ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 return ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# remove the AR features list and substitute them with the individual data lists # remove the AR features list and substitute them with the individual data lists
# else, scaling will not work properly # else, scaling will not work properly
start = time.time() start = time.time()
ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 = extract_ar_coefs(emg_feats_train) ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 = extract_ar_coefs(emg_feats_train)
emg_feats_train_mod = np.column_stack((np.transpose(emg_feats_train[0:16]),ar_feats_0,ar_feats_1,ar_feats_2, emg_feats_train_mod = np.column_stack((np.transpose(emg_feats_train[0:16]),ar_feats_0,ar_feats_1,ar_feats_2,
ar_feats_3,ar_feats_4,ar_feats_5)) ar_feats_3,ar_feats_4,ar_feats_5))
ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 = extract_ar_coefs(emg_feats_test) ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 = extract_ar_coefs(emg_feats_test)
emg_feats_test_mod = np.column_stack((np.transpose(emg_feats_test[0:16]),ar_feats_0,ar_feats_1,ar_feats_2, emg_feats_test_mod = np.column_stack((np.transpose(emg_feats_test[0:16]),ar_feats_0,ar_feats_1,ar_feats_2,
ar_feats_3,ar_feats_4,ar_feats_5)) ar_feats_3,ar_feats_4,ar_feats_5))
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-10-13d1648b70fd> in <module> <ipython-input-10-13d1648b70fd> in <module>
2 # else, scaling will not work properly 2 # else, scaling will not work properly
3 start = time.time() 3 start = time.time()
----> 4 ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 = extract_ar_coefs(emg_feats_train) ----> 4 ar_feats_0, ar_feats_1, ar_feats_2, ar_feats_3, ar_feats_4, ar_feats_5 = extract_ar_coefs(emg_feats_train)
5 emg_feats_train_mod = np.column_stack((np.transpose(emg_feats_train[0:16]),ar_feats_0,ar_feats_1,ar_feats_2, 5 emg_feats_train_mod = np.column_stack((np.transpose(emg_feats_train[0:16]),ar_feats_0,ar_feats_1,ar_feats_2,
6 ar_feats_3,ar_feats_4,ar_feats_5)) 6 ar_feats_3,ar_feats_4,ar_feats_5))
NameError: name 'emg_feats_train' is not defined NameError: name 'emg_feats_train' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# remove certain features (which are believed to be "bad") # remove certain features (which are believed to be "bad")
temp = emg_feats_train_mod[:,0:6] temp = emg_feats_train_mod[:,0:6]
temp2= emg_feats_train_mod[:,16:] temp2= emg_feats_train_mod[:,16:]
emg_feats_train_mod2 = np.array(np.column_stack((temp, temp2))) emg_feats_train_mod2 = np.array(np.column_stack((temp, temp2)))
temp_ = emg_feats_test_mod[:,0:6] temp_ = emg_feats_test_mod[:,0:6]
temp2_= emg_feats_test_mod[:,16:] temp2_= emg_feats_test_mod[:,16:]
emg_feats_test_mod2 = np.array(np.column_stack((temp_, temp2_))) emg_feats_test_mod2 = np.array(np.column_stack((temp_, temp2_)))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# save emg features for future imports # save emg features for future imports
pd.DataFrame.to_csv(pd.DataFrame(emg_feats_train), 'files/emg_feats_train.csv', index=False) pd.DataFrame.to_csv(pd.DataFrame(emg_feats_train), 'files/emg_feats_train.csv', index=False)
pd.DataFrame.to_csv(pd.DataFrame(emg_feats_test), 'files/emg_feats_test.csv', index=False) pd.DataFrame.to_csv(pd.DataFrame(emg_feats_test), 'files/emg_feats_test.csv', index=False)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# create full train and testsets # create full train and testsets
X_train_ = np.array(np.column_stack((eeg_train, emg_feats_train_mod2))) X_train_ = np.array(np.column_stack((eeg_train, emg_feats_train_mod2)))
X_test_ = np.array(np.column_stack((eeg_test, emg_feats_test_mod2))) X_test_ = np.array(np.column_stack((eeg_test, emg_feats_test_mod2)))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Splitting ### Splitting
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
start = time.time() start = time.time()
def split(X_train, y_train): def split(X_train, y_train):
return train_test_split( return train_test_split(
X_train, X_train,
y_train, y_train,
test_size=0.1, test_size=0.1,
shuffle=False, shuffle=False,
random_state=0) random_state=0)
print(X_train_.shape, train_labels_raw.shape) print(X_train_.shape, train_labels_raw.shape)
if PROTOTYPING: if PROTOTYPING:
X_train, X_test, y_train, y_test = split(X_train, train_labels_raw) X_train, X_test, y_train, y_test = split(X_train, train_labels_raw)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
else: else:
y_train = train_labels_raw y_train = train_labels_raw
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-7-15af0b5a8527> in <module> <ipython-input-7-15af0b5a8527> in <module>
9 random_state=0) 9 random_state=0)
10 10
---> 11 print(X_train_.shape, train_labels_raw.shape) ---> 11 print(X_train_.shape, train_labels_raw.shape)
12 if PROTOTYPING: 12 if PROTOTYPING:
13 X_train, X_test, y_train, y_test = split(X_train, train_labels_raw) 13 X_train, X_test, y_train, y_test = split(X_train, train_labels_raw)
NameError: name 'X_train_' is not defined NameError: name 'X_train_' is not defined
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Feature scaling ### Feature scaling
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# trick(?): scale eeg feats first, then add emg feats and scale again... # trick(?): scale eeg feats first, then add emg feats and scale again...
start = time.time() start = time.time()
def scale(X_train, X_test): def scale(X_train, X_test):
scaler = StandardScaler().fit(X_train) scaler = StandardScaler().fit(X_train)
# scale # scale
X_train_scaled = scaler.transform(X_train) X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test) X_test_scaled = scaler.transform(X_test)
return X_train_scaled, X_test_scaled return X_train_scaled, X_test_scaled
X_train_s, X_test_s = scale(X_train_, X_test_) X_train_s, X_test_s = scale(X_train_, X_test_)
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-5-646202010b2f> in <module> <ipython-input-5-646202010b2f> in <module>
10 return X_train_scaled, X_test_scaled 10 return X_train_scaled, X_test_scaled
11 11
---> 12 X_train_s, X_test_s = scale(X_train_, X_test_) ---> 12 X_train_s, X_test_s = scale(X_train_, X_test_)
13 13
14 print("Time: ", time.time() - start) 14 print("Time: ", time.time() - start)
NameError: name 'X_train_' is not defined NameError: name 'X_train_' is not defined
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Training ### Training
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# GridSearch for SVC # GridSearch for SVC
start = time.time() start = time.time()
kernels = ("rbf", "sigmoid") kernels = ("rbf", "sigmoid")
C_values = np.logspace(0, 1.5, num=2) C_values = np.logspace(0, 1.5, num=2)
param_grid = {"kernel" : kernels, param_grid = {"kernel" : kernels,
"C" : C_values} "C" : C_values}
scoring_method = "balanced_accuracy" scoring_method = "balanced_accuracy"
cv = StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=0) cv = StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=0)
classifier = SVC(kernel="rbf", class_weight="balanced", gamma="auto", decision_function_shape="ovo") classifier = SVC(kernel="rbf", class_weight="balanced", gamma="auto", decision_function_shape="ovo")
grid = GridSearchCV(estimator=classifier, param_grid=param_grid, scoring=scoring_method, cv=cv, verbose=11) grid = GridSearchCV(estimator=classifier, param_grid=param_grid, scoring=scoring_method, cv=cv, verbose=11)
grid.fit(X_train_s, np.ravel(y_train)) grid.fit(X_train_s, np.ravel(y_train))
best = grid.best_params_ best = grid.best_params_
print("Grid with best params: %s and score %f" % (grid.best_params_, grid.best_score_)) print("Grid with best params: %s and score %f" % (grid.best_params_, grid.best_score_))
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: stream %%%% Output: stream
Fitting 1 folds for each of 4 candidates, totalling 4 fits Fitting 1 folds for each of 4 candidates, totalling 4 fits
[CV] C=1.0, kernel=rbf ............................................... [CV] C=1.0, kernel=rbf ...............................................
%%%% Output: stream %%%% Output: stream
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers. [Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
%%%% Output: stream %%%% Output: stream
[CV] ................... C=1.0, kernel=rbf, score=0.926, total= 1.9min [CV] ................... C=1.0, kernel=rbf, score=0.926, total= 1.9min
[CV] C=1.0, kernel=sigmoid ........................................... [CV] C=1.0, kernel=sigmoid ...........................................
%%%% Output: stream %%%% Output: stream
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 1.9min remaining: 0.0s [Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 1.9min remaining: 0.0s
%%%% Output: stream %%%% Output: stream
[CV] ............... C=1.0, kernel=sigmoid, score=0.753, total= 2.3min [CV] ............... C=1.0, kernel=sigmoid, score=0.753, total= 2.3min
[CV] C=31.622776601683793, kernel=rbf ................................ [CV] C=31.622776601683793, kernel=rbf ................................
%%%% Output: stream %%%% Output: stream
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 4.1min remaining: 0.0s [Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 4.1min remaining: 0.0s
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last) KeyboardInterrupt Traceback (most recent call last)
<ipython-input-259-7d3d89b86907> in <module> <ipython-input-259-7d3d89b86907> in <module>
11 classifier = SVC(kernel="rbf", class_weight="balanced", gamma="auto", decision_function_shape="ovo") 11 classifier = SVC(kernel="rbf", class_weight="balanced", gamma="auto", decision_function_shape="ovo")
12 grid = GridSearchCV(estimator=classifier, param_grid=param_grid, scoring=scoring_method, cv=cv, verbose=11) 12 grid = GridSearchCV(estimator=classifier, param_grid=param_grid, scoring=scoring_method, cv=cv, verbose=11)
---> 13 grid.fit(X_train_s, np.ravel(y_train)) ---> 13 grid.fit(X_train_s, np.ravel(y_train))
14 best = grid.best_params_ 14 best = grid.best_params_
15 print("Grid with best params: %s and score %f" % (grid.best_params_, grid.best_score_)) 15 print("Grid with best params: %s and score %f" % (grid.best_params_, grid.best_score_))
~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params) ~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
686 return results 686 return results
687 687
--> 688 self._run_search(evaluate_candidates) --> 688 self._run_search(evaluate_candidates)
689 689
690 # For multi-metric evaluation, store the best_index_, best_params_ and 690 # For multi-metric evaluation, store the best_index_, best_params_ and
~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates) ~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates)
1147 def _run_search(self, evaluate_candidates): 1147 def _run_search(self, evaluate_candidates):
1148 """Search all candidates in param_grid""" 1148 """Search all candidates in param_grid"""
-> 1149 evaluate_candidates(ParameterGrid(self.param_grid)) -> 1149 evaluate_candidates(ParameterGrid(self.param_grid))
1150 1150
1151 1151
~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params) ~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params)
665 for parameters, (train, test) 665 for parameters, (train, test)
666 in product(candidate_params, 666 in product(candidate_params,
--> 667 cv.split(X, y, groups))) --> 667 cv.split(X, y, groups)))
668 668
669 if len(out) < 1: 669 if len(out) < 1:
~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable) ~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
1004 self._iterating = self._original_iterator is not None 1004 self._iterating = self._original_iterator is not None
1005 1005
-> 1006 while self.dispatch_one_batch(iterator): -> 1006 while self.dispatch_one_batch(iterator):
1007 pass 1007 pass
1008 1008
~\Anaconda3\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, iterator) ~\Anaconda3\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, iterator)
832 return False 832 return False
833 else: 833 else:
--> 834 self._dispatch(tasks) --> 834 self._dispatch(tasks)
835 return True 835 return True
836 836
~\Anaconda3\lib\site-packages\joblib\parallel.py in _dispatch(self, batch) ~\Anaconda3\lib\site-packages\joblib\parallel.py in _dispatch(self, batch)
751 with self._lock: 751 with self._lock:
752 job_idx = len(self._jobs) 752 job_idx = len(self._jobs)
--> 753 job = self._backend.apply_async(batch, callback=cb) --> 753 job = self._backend.apply_async(batch, callback=cb)
754 # A job can complete so quickly than its callback is 754 # A job can complete so quickly than its callback is
755 # called before we get here, causing self._jobs to 755 # called before we get here, causing self._jobs to
~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in apply_async(self, func, callback) ~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in apply_async(self, func, callback)
199 def apply_async(self, func, callback=None): 199 def apply_async(self, func, callback=None):
200 """Schedule a func to be run""" 200 """Schedule a func to be run"""
--> 201 result = ImmediateResult(func) --> 201 result = ImmediateResult(func)
202 if callback: 202 if callback:
203 callback(result) 203 callback(result)
~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in __init__(self, batch) ~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in __init__(self, batch)
580 # Don't delay the application, to avoid keeping the input 580 # Don't delay the application, to avoid keeping the input
581 # arguments in memory 581 # arguments in memory
--> 582 self.results = batch() --> 582 self.results = batch()
583 583
584 def get(self): 584 def get(self):
~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self) ~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self)
254 with parallel_backend(self._backend, n_jobs=self._n_jobs): 254 with parallel_backend(self._backend, n_jobs=self._n_jobs):
255 return [func(*args, **kwargs) 255 return [func(*args, **kwargs)
--> 256 for func, args, kwargs in self.items] --> 256 for func, args, kwargs in self.items]
257 257
258 def __len__(self): 258 def __len__(self):
~\Anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0) ~\Anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
254 with parallel_backend(self._backend, n_jobs=self._n_jobs): 254 with parallel_backend(self._backend, n_jobs=self._n_jobs):
255 return [func(*args, **kwargs) 255 return [func(*args, **kwargs)
--> 256 for func, args, kwargs in self.items] --> 256 for func, args, kwargs in self.items]
257 257
258 def __len__(self): 258 def __len__(self):
~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, error_score) ~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, error_score)
514 estimator.fit(X_train, **fit_params) 514 estimator.fit(X_train, **fit_params)
515 else: 515 else:
--> 516 estimator.fit(X_train, y_train, **fit_params) --> 516 estimator.fit(X_train, y_train, **fit_params)
517 517
518 except Exception as e: 518 except Exception as e:
~\Anaconda3\lib\site-packages\sklearn\svm\base.py in fit(self, X, y, sample_weight) ~\Anaconda3\lib\site-packages\sklearn\svm\base.py in fit(self, X, y, sample_weight)
207 207
208 seed = rnd.randint(np.iinfo('i').max) 208 seed = rnd.randint(np.iinfo('i').max)
--> 209 fit(X, y, sample_weight, solver_type, kernel, random_seed=seed) --> 209 fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
210 # see comment on the other call to np.iinfo in this file 210 # see comment on the other call to np.iinfo in this file
211 211
~\Anaconda3\lib\site-packages\sklearn\svm\base.py in _dense_fit(self, X, y, sample_weight, solver_type, kernel, random_seed) ~\Anaconda3\lib\site-packages\sklearn\svm\base.py in _dense_fit(self, X, y, sample_weight, solver_type, kernel, random_seed)
266 cache_size=self.cache_size, coef0=self.coef0, 266 cache_size=self.cache_size, coef0=self.coef0,
267 gamma=self._gamma, epsilon=self.epsilon, 267 gamma=self._gamma, epsilon=self.epsilon,
--> 268 max_iter=self.max_iter, random_seed=random_seed) --> 268 max_iter=self.max_iter, random_seed=random_seed)
269 269
270 self._warn_from_fit_status() 270 self._warn_from_fit_status()
KeyboardInterrupt: KeyboardInterrupt:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# testing logspace for more refined C estimates # testing logspace for more refined C estimates
np.logspace(0.32,0.45,num=10) np.logspace(0.32,0.45,num=10)
``` ```
%%%% Output: execute_result %%%% Output: execute_result
array([2.08929613, 2.15995371, 2.23300085, 2.30851836, 2.38658979, array([2.08929613, 2.15995371, 2.23300085, 2.30851836, 2.38658979,
2.4673015 , 2.55074278, 2.63700596, 2.72618645, 2.81838293]) 2.4673015 , 2.55074278, 2.63700596, 2.72618645, 2.81838293])
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# SVM approach # SVM approach
start = time.time() start = time.time()
classifier = SVC(C=1, class_weight="balanced", gamma="auto", decision_function_shape="ovo") classifier = SVC(C=1, class_weight="balanced", gamma="auto", decision_function_shape="ovo")
classifier.fit(X_train_s, np.ravel(y_train)) classifier.fit(X_train_s, np.ravel(y_train))
y_predict = classifier.predict(X_test_s) y_predict = classifier.predict(X_test_s)
if PROTOTYPING: if PROTOTYPING:
print(balanced_accuracy_score(y_test, y_predict)) print(balanced_accuracy_score(y_test, y_predict))
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: stream %%%% Output: stream
C:\Users\made_\Anaconda3\lib\site-packages\sklearn\utils\validation.py:724: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). C:\Users\made_\Anaconda3\lib\site-packages\sklearn\utils\validation.py:724: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True) y = column_or_1d(y, warn=True)
%%%% Output: stream %%%% Output: stream
Time: 165.16580438613892 Time: 165.16580438613892
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# bagging # bagging
y_train = train_labels_raw y_train = train_labels_raw
start = time.time() start = time.time()
classifier = BaggingClassifier(SVC(C=1, class_weight="balanced", gamma="auto", decision_function_shape="ovo"), classifier = BaggingClassifier(SVC(C=1, class_weight="balanced", gamma="auto", decision_function_shape="ovo"),
n_estimators=100, random_state=0) n_estimators=100, random_state=0)
classifier.fit(X_train_s, np.ravel(y_train)) classifier.fit(X_train_s, np.ravel(y_train))
y_predict = classifier.predict(X_test_s) y_predict = classifier.predict(X_test_s)
if PROTOTYPING: if PROTOTYPING:
print(balanced_accuracy_score(y_test, y_predict)) print(balanced_accuracy_score(y_test, y_predict))
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: stream %%%% Output: stream
Time: 8023.956521987915 Time: 8023.956521987915
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# scale freqs from fft alone # scale freqs from fft alone
eeg_freqs_train_s, eeg_freqs_test_s = scale(eeg_freqs_train, eeg_freqs_test) eeg_freqs_train_s, eeg_freqs_test_s = scale(eeg_freqs_train, eeg_freqs_test)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
y_train = train_labels_raw y_train = train_labels_raw
z,s = np.shape(eeg_freqs_train_s) z,s = np.shape(eeg_freqs_train_s)
# define the model architecture # define the model architecture
lahead = s lahead = s
ann = Sequential() ann = Sequential()
ann.add(LSTM(200, input_shape = (lahead,1), return_sequences = True)) ann.add(LSTM(200, input_shape = (lahead,1), return_sequences = True))
#ann.add(Dropout(0.2)) #ann.add(Dropout(0.2))
ann.add(LSTM(100)) ann.add(LSTM(100))
#ann.add(Dropout(0.25)) #ann.add(Dropout(0.25))
#ann.add(LSTM(512, activation = 'relu')) #ann.add(LSTM(512, activation = 'relu'))
#ann.add(Dropout(0.25)) #ann.add(Dropout(0.25))
# #
#ann.add(LSTM(256, activation = 'relu')) #ann.add(LSTM(256, activation = 'relu'))
#ann.add(Dropout(0.25)) #ann.add(Dropout(0.25))
ann.add(Dense(50, activation = 'relu')) ann.add(Dense(50, activation = 'relu'))
#ann.add(Dropout(0.25)) #ann.add(Dropout(0.25))
ann.add(Dense(4, activation='softmax')) ann.add(Dense(4, activation='softmax'))
opt = keras.optimizers.Adam(lr = 1e-3, decay = 1e-5) opt = keras.optimizers.Adam(lr = 1e-3, decay = 1e-5)
ann.compile(loss = 'sparse_categorical_crossentropy', ann.compile(loss = 'sparse_categorical_crossentropy',
optimizer = opt, optimizer = opt,
metrics = ['accuracy']) metrics = ['accuracy'])
# reshape to fulfill LSTM shape requirements # reshape to fulfill LSTM shape requirements
reshape_1 = lambda x: x.reshape((x.shape[0], x.shape[1], 1)) # reshape x trainset reshape_1 = lambda x: x.reshape((x.shape[0], x.shape[1], 1)) # reshape x trainset
reshape_2 = lambda y: y.reshape((y.shape[0], 1)) # reshape y reshape_2 = lambda y: y.reshape((y.shape[0], 1)) # reshape y
X_reshaped = reshape_1(eeg_freqs_train_s) X_reshaped = reshape_1(eeg_freqs_train_s)
X_test_reshaped = reshape_1(eeg_freqs_test_s) X_test_reshaped = reshape_1(eeg_freqs_test_s)
y_reshaped = reshape_2(y_train) y_reshaped = reshape_2(y_train)
# fit and predict # fit and predict
ann.fit(X_reshaped, y_reshaped, epochs = 15, batch_size = 10000, class_weight = 'balanced') ann.fit(X_reshaped, y_reshaped, epochs = 15, batch_size = 10000, class_weight = 'balanced')
y_predict = ann.predict_classes(X_test_reshaped) y_predict = ann.predict_classes(X_test_reshaped)
``` ```
%%%% Output: stream %%%% Output: stream
Epoch 1/15 Epoch 1/15
%%%% Output: error %%%% Output: error
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last) KeyboardInterrupt Traceback (most recent call last)
~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py in __call__(self, *args) ~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py in __call__(self, *args)
1450 return tf_session.TF_SessionRunCallable( 1450 return tf_session.TF_SessionRunCallable(
-> 1451 self._session._session, self._handle, args, status, None) -> 1451 self._session._session, self._handle, args, status, None)
1452 else: 1452 else:
KeyboardInterrupt: KeyboardInterrupt:
During handling of the above exception, another exception occurred: During handling of the above exception, another exception occurred:
ResourceExhaustedError Traceback (most recent call last) ResourceExhaustedError Traceback (most recent call last)
<ipython-input-8-8bb9b645a9ff> in <module> <ipython-input-8-8bb9b645a9ff> in <module>
35 35
36 # fit and predict (later add class weights and also optimize them) 36 # fit and predict (later add class weights and also optimize them)
---> 37 ann.fit(X_reshaped, y_reshaped, epochs = 15, batch_size = 10000, class_weight = 'balanced') ---> 37 ann.fit(X_reshaped, y_reshaped, epochs = 15, batch_size = 10000, class_weight = 'balanced')
38 y_predict = ann.predict_classes(X_test_reshaped) 38 y_predict = ann.predict_classes(X_test_reshaped)
~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) ~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch, 1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch, 1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps) -> 1039 validation_steps=validation_steps)
1040 1040
1041 def evaluate(self, x=None, y=None, 1041 def evaluate(self, x=None, y=None,
~\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps) ~\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray() 197 ins_batch[i] = ins_batch[i].toarray()
198 198
--> 199 outs = f(ins_batch) --> 199 outs = f(ins_batch)
200 outs = to_list(outs) 200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs): 201 for l, o in zip(out_labels, outs):
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs) ~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs) 2713 return self._legacy_call(inputs)
2714 2714
-> 2715 return self._call(inputs) -> 2715 return self._call(inputs)
2716 else: 2716 else:
2717 if py_any(is_tensor(x) for x in inputs): 2717 if py_any(is_tensor(x) for x in inputs):
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs) ~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata) 2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else: 2674 else:
-> 2675 fetched = self._callable_fn(*array_vals) -> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)] 2676 return fetched[:len(self.outputs)]
2677 2677
~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py in __call__(self, *args) ~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py in __call__(self, *args)
1452 else: 1452 else:
1453 return tf_session.TF_DeprecatedSessionRunCallable( 1453 return tf_session.TF_DeprecatedSessionRunCallable(
-> 1454 self._session._session, self._handle, args, status, None) -> 1454 self._session._session, self._handle, args, status, None)
1455 1455
1456 def __del__(self): 1456 def __del__(self):
~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg) ~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
517 None, None, 517 None, None,
518 compat.as_text(c_api.TF_Message(self.status.status)), 518 compat.as_text(c_api.TF_Message(self.status.status)),
--> 519 c_api.TF_GetCode(self.status.status)) --> 519 c_api.TF_GetCode(self.status.status))
520 # Delete the underlying status object from memory otherwise it stays alive 520 # Delete the underlying status object from memory otherwise it stays alive
521 # as there is a reference to status from this from the traceback due to 521 # as there is a reference to status from this from the traceback due to
ResourceExhaustedError: OOM when allocating tensor with shape[10000,200] and type float on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu ResourceExhaustedError: OOM when allocating tensor with shape[10000,200] and type float on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu
[[Node: lstm_3/while/MatMul_1 = MatMul[T=DT_FLOAT, _class=["loc:@training_1/Adam/gradients/lstm_3/while/MatMul_1_grad/MatMul_1"], transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](lstm_3/while/TensorArrayReadV3, lstm_3/while/MatMul_1/Enter)]] [[Node: lstm_3/while/MatMul_1 = MatMul[T=DT_FLOAT, _class=["loc:@training_1/Adam/gradients/lstm_3/while/MatMul_1_grad/MatMul_1"], transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](lstm_3/while/TensorArrayReadV3, lstm_3/while/MatMul_1/Enter)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info. Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Write result ### Write result
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
start = time.time() start = time.time()
output = pd.read_csv('files/sample.csv') output = pd.read_csv('files/sample.csv')
for i in range(output.shape[0]): for i in range(output.shape[0]):
output.iat[i, 1] = y_predict[i] output.iat[i, 1] = y_predict[i]
output.to_csv("files/SVM_OvO_3_otherfeats_bagged.csv", index=False) output.to_csv("files/SVM_OvO_3_otherfeats_bagged.csv", index=False)
print("Time: ", time.time() - start) print("Time: ", time.time() - start)
``` ```
%%%% Output: stream %%%% Output: stream
Time: 0.5066447257995605 Time: 0.5066447257995605
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Experimental Testing ### Experimental Testing
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
best best
``` ```
%%%% Output: execute_result %%%% Output: execute_result
{'C': 2.154434690031882, 'kernel': 'rbf'} {'C': 2.154434690031882, 'kernel': 'rbf'}
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
grid.best_estimator_ grid.best_estimator_
``` ```
%%%% Output: execute_result %%%% Output: execute_result
SVC(C=2.154434690031882, cache_size=200, class_weight='balanced', coef0=0.0, SVC(C=2.154434690031882, cache_size=200, class_weight='balanced', coef0=0.0,
decision_function_shape='ovo', degree=3, gamma='auto', kernel='rbf', decision_function_shape='ovo', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True, max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False) tol=0.001, verbose=False)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment