├── FNN.py ├── README.md ├── RF.py ├── knn.py ├── pca_capsnet-keras ├── capsule-cifa.py └── capsule-test.py ├── svm.py └── tdt.py /FNN.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | from sklearn.preprocessing import LabelEncoder 4 | from keras.models import Sequential 5 | from keras.layers import Dense 6 | from keras.optimizers import Adam 7 | 8 | 9 | dataset=pd.read_csv('data.csv',header=None) 10 | da_1=dataset.dropna(axis=1) 11 | 12 | X_data = np.array(da_1.iloc[:,2:].values) 13 | 14 | 15 | label=dataset.iloc[:,0].values 16 | encoder=LabelEncoder() 17 | label=encoder.fit_transform(label) 18 | Y_data=pd.get_dummies(label).values 19 | 20 | 21 | model = Sequential() 22 | 23 | model.add(Dense(128, input_shape=(500,), activation='relu')) 24 | model.add(Dense(64, activation='relu')) 25 | model.add(Dense(64, activation='relu')) 26 | model.add(Dense(3, activation='softmax')) 27 | 28 | model.compile(Adam(lr=0.001), 'categorical_crossentropy', metrics=['accuracy']) 29 | model.summary() 30 | model.fit(X_data,Y_data,epochs=1000) 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Classification-of-eeg-signals 2 | A variety of traditional machine learning algorithms and neural networks are used to model and classify eeg signals and predict whether patients are ill or not. 3 | -------------------------------------------------------------------------------- /RF.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | import pandas as pd 5 | import numpy as np 6 | #from numpy import nan as NaN 7 | from sklearn import svm 8 | import matplotlib.pyplot as plt 9 | from pylab import mpl 10 | import numpy as np 11 | import pandas as pd 12 | from sklearn import preprocessing 13 | from sklearn.model_selection import StratifiedKFold 14 | from sklearn.metrics import accuracy_score 15 | from sklearn.neighbors import KNeighborsClassifier 16 | import sklearn.model_selection as ms 17 | import sklearn.metrics as sm 18 | from sklearn import tree 19 | from sklearn.ensemble import RandomForestClassifier 20 | 21 | 22 | 23 | ###import data### 24 | data=pd.read_csv('data.csv',header=None) 25 | data_1=data.dropna(axis=1) 26 | #print(data_1) 27 | features=np.array(data_1.iloc[:,2:].values) 28 | marks =data.iloc[:,0].values 29 | #print(features.shape) 30 | labels= np.zeros(len(marks)) 31 | labels[marks=='R']=1 32 | labels[marks=='F']=2 33 | 34 | 35 | Fold = 10 36 | i=1 37 | skf = StratifiedKFold(n_splits=Fold, random_state=42, shuffle=False) 38 | y_true= np.ones(labels.shape) 39 | y_pred = np.ones(labels.shape) 40 | 41 | for idx1, idx2 in skf.split(features, labels): 42 | print('\nTesting model in fold : %i\n'%(i)) 43 | i = i+1 44 | train_data=features[idx1,:] 45 | # print(train_data.shape) 46 | train_label=labels[idx1] 47 | # print(train_label.shape) 48 | 49 | test_data = features[idx2,:] 50 | test_label = labels[idx2] 51 | 52 | # normalization 53 | scaler = preprocessing.StandardScaler().fit(train_data) 54 | train_data = scaler.transform(train_data) 55 | min_max_scaler = preprocessing.MinMaxScaler() 56 | train_data = min_max_scaler.fit_transform(train_data) 57 | # print(train_data) 58 | train_label =train_label.astype(int) 59 | 60 | test_data = scaler.transform(test_data) 61 | test_data = min_max_scaler.transform(test_data) 62 | test_label =test_label.astype(int) 63 | 64 | ##RF### 65 | clf=RandomForestClassifier(n_estimators=100, n_jobs=4, oob_score=True) 66 | clf.fit(train_data,train_label) 67 | predictions=(clf.predict(test_data)) 68 | acc_test=clf.score(test_data,test_label) 69 | print('test acc is {}'.format(acc_test)) 70 | 71 | ###model evalution#### 72 | y_pred[idx2,] = predictions 73 | y_true[idx2,] = test_label 74 | 75 | print('\n Mean accuracy is {}'.format(accuracy_score(y_true,y_pred))) 76 | -------------------------------------------------------------------------------- /knn.py: -------------------------------------------------------------------------------- 1 | from sklearn import svm 2 | import matplotlib.pyplot as plt 3 | from pylab import mpl 4 | import numpy as np 5 | import pandas as pd 6 | from sklearn import preprocessing 7 | from sklearn.model_selection import StratifiedKFold 8 | from sklearn.metrics import accuracy_score 9 | from sklearn.neighbors import KNeighborsClassifier 10 | 11 | 12 | ###import data### 13 | data=pd.read_csv('data.csv',header=None) 14 | data_1=data.dropna(axis=1) 15 | print(data_1) 16 | features=np.array(data_1.iloc[:,2:].values) 17 | marks =data.iloc[:,0].values 18 | print(features.shape) 19 | labels= np.zeros(len(marks)) 20 | labels[marks=='R']=1 21 | labels[marks=='F']=2 22 | 23 | ###K fold cross validation### 24 | 25 | Fold = 10 26 | i=1 27 | skf = StratifiedKFold(n_splits=Fold, random_state=42, shuffle=False) 28 | y_true= np.ones(labels.shape) 29 | y_pred = np.ones(labels.shape) 30 | 31 | for idx1, idx2 in skf.split(features, labels): 32 | print('\nTesting model in fold : %i\n'%(i)) 33 | i = i+1 34 | train_data=features[idx1,:] 35 | # print(train_data.shape) 36 | train_label=labels[idx1] 37 | # print(train_label.shape) 38 | 39 | test_data = features[idx2,:] 40 | test_label = labels[idx2] 41 | 42 | # normalization 43 | scaler = preprocessing.StandardScaler().fit(train_data) 44 | train_data = scaler.transform(train_data) 45 | min_max_scaler = preprocessing.MinMaxScaler() 46 | train_data = min_max_scaler.fit_transform(train_data) 47 | train_label =train_label.astype(int) 48 | 49 | test_data = scaler.transform(test_data) 50 | test_data = min_max_scaler.transform(test_data) 51 | test_label =test_label.astype(int) 52 | 53 | 54 | ##KNN### 55 | Num_Neighbor = 4 56 | knnModel = KNeighborsClassifier(n_neighbors=Num_Neighbor) 57 | knnModel.fit(train_data, train_label) 58 | predictions = knnModel.predict(test_data) 59 | knn_acc = knnModel.score(test_data,test_label) 60 | print('Test accuracy is {}'.format(knn_acc)) 61 | 62 | y_pred[idx2,] = predictions 63 | y_true[idx2,] = test_label 64 | 65 | print('\n Mean accuracy is {}'.format(accuracy_score(y_true,y_pred))) 66 | -------------------------------------------------------------------------------- /pca_capsnet-keras/capsule-cifa.py: -------------------------------------------------------------------------------- 1 | 2 | """ 3 | This example trains a simple CNN-Capsule Network on the CIFAR10 data set. 4 | Without Data Augmentation: 5 | It gets to 75% validation accuracy in 10 epochs, 79% after 15 epochs, 6 | and overfitting after 20 epochs 7 | With Data Augmentation: 8 | It gets to 75% validation accuracy in 10 epochs, 79% after 15 epochs, 9 | and 83% after 30 epochs. 10 | The highest achieved validation accuracy is 83.79% after 50 epochs. 11 | This is a fast implementation that takes just 20s/epoch on a GTX 1070 GPU. 12 | The paper "Dynamic Routing Between Capsules": https://arxiv.org/abs/1710.09829 13 | """ 14 | from __future__ import print_function 15 | 16 | from keras import activations 17 | from keras import backend as K 18 | from keras import layers 19 | from keras import utils 20 | from keras.datasets import cifar10 21 | from keras.models import Model 22 | from keras.preprocessing.image import ImageDataGenerator 23 | 24 | 25 | def squash(x, axis=-1): 26 | """The Squashing Function. 27 | The nonlinear activation function used in Capsule Network 28 | # Arguments 29 | x: Input Tensor. 30 | axis: Integer axis along which the squashing function is to be applied. 31 | # Returns 32 | Tensor with scaled value of the input tensor 33 | """ 34 | s_squared_norm = K.sum(K.square(x), axis, keepdims=True) + K.epsilon() 35 | scale = K.sqrt(s_squared_norm) / (0.5 + s_squared_norm) 36 | return scale * x 37 | 38 | 39 | def margin_loss(y_true, y_pred): 40 | """Margin loss 41 | # Arguments 42 | y_true: tensor of true targets. 43 | y_pred: tensor of predicted targets. 44 | # Returns 45 | Tensor with one scalar loss entry per sample. 46 | """ 47 | lamb, margin = 0.5, 0.1 48 | return K.sum(y_true * K.square(K.relu(1 - margin - y_pred)) + lamb * ( 49 | 1 - y_true) * K.square(K.relu(y_pred - margin)), axis=-1) 50 | 51 | 52 | class Capsule(layers.Layer): 53 | """Capsule Network 54 | A Capsule Network Layer implementation in Keras 55 | There are two versions of Capsule Networks. 56 | One is similar to dense layer (for the fixed-shape input), 57 | and the other is similar to time distributed dense layer 58 | (for inputs of varied length). 59 | The input shape of Capsule must be (batch_size, 60 | input_num_capsule, 61 | input_dim_capsule 62 | ) 63 | and the output shape is (batch_size, 64 | num_capsule, 65 | dim_capsule 66 | ) 67 | The Capsule implementation is from https://github.com/bojone/Capsule/ 68 | # Arguments 69 | num_capsule: An integer, the number of capsules. 70 | dim_capsule: An integer, the dimensions of the capsule. 71 | routings: An integer, the number of routings. 72 | share_weights: A boolean, sets weight sharing between layers. 73 | activation: A string, the activation function to be applied. 74 | """ 75 | 76 | def __init__(self, 77 | num_capsule, 78 | dim_capsule, 79 | routings=3, 80 | share_weights=True, 81 | activation='squash', 82 | **kwargs): 83 | super(Capsule, self).__init__(**kwargs) 84 | self.num_capsule = num_capsule 85 | self.dim_capsule = dim_capsule 86 | self.routings = routings 87 | self.share_weights = share_weights 88 | if activation == 'squash': 89 | self.activation = squash 90 | else: 91 | self.activation = activations.get(activation) 92 | 93 | def build(self, input_shape): 94 | input_dim_capsule = input_shape[-1] 95 | if self.share_weights: 96 | self.kernel = self.add_weight( 97 | name='capsule_kernel', 98 | shape=(1, input_dim_capsule, 99 | self.num_capsule * self.dim_capsule), 100 | initializer='glorot_uniform', 101 | trainable=True) 102 | else: 103 | input_num_capsule = input_shape[-2] 104 | self.kernel = self.add_weight( 105 | name='capsule_kernel', 106 | shape=(input_num_capsule, input_dim_capsule, 107 | self.num_capsule * self.dim_capsule), 108 | initializer='glorot_uniform', 109 | trainable=True) 110 | 111 | def call(self, inputs, **kwargs): 112 | """Following the routing algorithm from Hinton's paper, 113 | but replace b = b + with b = . 114 | This change can improve the feature representation of the capsule. 115 | However, you can replace 116 | b = K.batch_dot(outputs, hat_inputs, [2, 3]) 117 | with 118 | b += K.batch_dot(outputs, hat_inputs, [2, 3]) 119 | to get standard routing. 120 | """ 121 | 122 | if self.share_weights: 123 | hat_inputs = K.conv1d(inputs, self.kernel) 124 | else: 125 | hat_inputs = K.local_conv1d(inputs, self.kernel, [1], [1]) 126 | 127 | batch_size = K.shape(inputs)[0] 128 | input_num_capsule = K.shape(inputs)[1] 129 | hat_inputs = K.reshape(hat_inputs, 130 | (batch_size, input_num_capsule, 131 | self.num_capsule, self.dim_capsule)) 132 | hat_inputs = K.permute_dimensions(hat_inputs, (0, 2, 1, 3)) 133 | 134 | b = K.zeros_like(hat_inputs[:, :, :, 0]) 135 | print(self.routings) 136 | for i in range(self.routings): 137 | c = K.softmax(b, 1) 138 | o = self.activation(K.batch_dot(c, hat_inputs, [2, 2])) 139 | if i < self.routings - 1: 140 | b = K.batch_dot(o, hat_inputs, [2, 3]) 141 | if K.backend() == 'theano': 142 | o = K.sum(o, axis=1) 143 | return o 144 | 145 | def compute_output_shape(self, input_shape): 146 | return None, self.num_capsule, self.dim_capsule 147 | 148 | 149 | batch_size = 128 150 | num_classes = 10 151 | epochs = 100 152 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 153 | 154 | x_train = x_train.astype('float32') 155 | x_test = x_test.astype('float32') 156 | x_train /= 255 157 | x_test /= 255 158 | y_train = utils.to_categorical(y_train, num_classes) 159 | y_test = utils.to_categorical(y_test, num_classes) 160 | 161 | # A simple Conv2D model 162 | input_image = layers.Input(shape=(None, None, 3)) 163 | x = layers.Conv2D(64, (3, 3), activation='relu')(input_image) 164 | x = layers.Conv2D(64, (3, 3), activation='relu')(x) 165 | x = layers.AveragePooling2D((2, 2))(x) 166 | x = layers.Conv2D(128, (3, 3), activation='relu')(x) 167 | x = layers.Conv2D(128, (3, 3), activation='relu')(x) 168 | 169 | # Now, we reshape it to (batch_size, input_num_capsule, input_dim_capsule) 170 | # then connect a capsule layer. 171 | # The output of final model is the lengths of 10 capsules, which have 16 dimensions. 172 | # The length of the output vector of the capsule expresses the probability of 173 | # existence of the entity, so the problem becomes a 10 two-classification problem. 174 | 175 | x = layers.Reshape((-1, 128))(x) 176 | capsule = Capsule(10, 16, 3, True)(x) 177 | output = layers.Lambda(lambda z: K.sqrt(K.sum(K.square(z), 2)))(capsule) 178 | model = Model(inputs=input_image, outputs=output) 179 | 180 | # Margin loss is used 181 | model.compile(loss=margin_loss, optimizer='adam', metrics=['accuracy']) 182 | model.summary() 183 | 184 | # Compare the performance with and without data augmentation 185 | data_augmentation = True 186 | 187 | if not data_augmentation: 188 | print('Not using data augmentation.') 189 | model.fit( 190 | x_train, 191 | y_train, 192 | batch_size=batch_size, 193 | epochs=epochs, 194 | validation_data=(x_test, y_test), 195 | shuffle=True) 196 | else: 197 | print('Using real-time data augmentation.') 198 | # This will do preprocessing and real-time data augmentation: 199 | datagen = ImageDataGenerator( 200 | featurewise_center=False, # set input mean to 0 over the dataset 201 | samplewise_center=False, # set each sample mean to 0 202 | featurewise_std_normalization=False, # divide inputs by dataset std 203 | samplewise_std_normalization=False, # divide each input by its std 204 | zca_whitening=False, # apply ZCA whitening 205 | zca_epsilon=1e-06, # epsilon for ZCA whitening 206 | rotation_range=0, # randomly rotate images in 0 to 180 degrees 207 | width_shift_range=0.1, # randomly shift images horizontally 208 | height_shift_range=0.1, # randomly shift images vertically 209 | shear_range=0., # set range for random shear 210 | zoom_range=0., # set range for random zoom 211 | channel_shift_range=0., # set range for random channel shifts 212 | # set mode for filling points outside the input boundaries 213 | fill_mode='nearest', 214 | cval=0., # value used for fill_mode = "constant" 215 | horizontal_flip=True, # randomly flip images 216 | vertical_flip=False, # randomly flip images 217 | # set rescaling factor (applied before any other transformation) 218 | rescale=None, 219 | # set function that will be applied on each input 220 | preprocessing_function=None, 221 | # image data format, either "channels_first" or "channels_last" 222 | data_format=None, 223 | # fraction of images reserved for validation (strictly between 0 and 1) 224 | validation_split=0.0) 225 | 226 | # Compute quantities required for feature-wise normalization 227 | # (std, mean, and principal components if ZCA whitening is applied). 228 | datagen.fit(x_train) 229 | 230 | # Fit the model on the batches generated by datagen.flow(). 231 | model.fit_generator( 232 | datagen.flow(x_train, y_train, batch_size=batch_size), 233 | epochs=epochs, 234 | validation_data=(x_test, y_test), 235 | workers=4) -------------------------------------------------------------------------------- /pca_capsnet-keras/capsule-test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from keras.models import load_model 4 | import keras 5 | import pandas as pd 6 | import numpy as np 7 | from sklearn.model_selection import train_test_split 8 | from sklearn.metrics import classification_report 9 | from sklearn.preprocessing import LabelEncoder 10 | from sklearn import preprocessing 11 | 12 | 13 | 14 | dataset=pd.read_csv('data.csv',header=None) 15 | data_1=dataset.dropna(axis=1) 16 | features=np.array(data_1.iloc[:,2:].values) 17 | lle=LocallyLinearEmbedding(n_components=100,n_neighbors=10) 18 | new_feature=lle.fit_transform(features) 19 | scaler_1=preprocessing.StandardScaler().fit(new_feature) 20 | X_data_1= scaler_1.transform(new_feature) 21 | 22 | label=dataset.iloc[:,0].values 23 | encoder=LabelEncoder() 24 | label=encoder.fit_transform(label) 25 | Y_data=pd.get_dummies(label).values 26 | X_train, X_test,y_train, y_test = train_test_split(X_data_1,Y_data, test_size=0.4, random_state=0) 27 | X_train=X_train.reshape((-1,10,10,1)).astype('float32') 28 | X_test=X_test.reshape((-1,10,10,1)).astype('float32') 29 | 30 | model = load_model('trained_model.h5') 31 | model.load 32 | Y_pred=model.predict(X_test) 33 | Y_pred_class=np.argmax(Y_pred,axis=1) 34 | Y_test_class=np.argmax(y_test,axis=1) 35 | report=classification_report(Y_pred_class,Y_test_class) -------------------------------------------------------------------------------- /svm.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | import pandas as pd 5 | import numpy as np 6 | #from numpy import nan as NaN 7 | from sklearn import svm 8 | import matplotlib.pyplot as plt 9 | from pylab import mpl 10 | import numpy as np 11 | import pandas as pd 12 | from sklearn import preprocessing 13 | from sklearn.model_selection import StratifiedKFold 14 | from sklearn.metrics import accuracy_score 15 | from sklearn.neighbors import KNeighborsClassifier 16 | 17 | 18 | ###import data### 19 | data=pd.read_csv('data.csv',header=None) 20 | data_1=data.dropna(axis=1) 21 | #print(data_1) 22 | features=np.array(data_1.iloc[:,2:].values) 23 | marks =data.iloc[:,0].values 24 | #print(features.shape) 25 | labels= np.zeros(len(marks)) 26 | labels[marks=='R']=1 27 | labels[marks=='F']=2 28 | 29 | Fold = 10 30 | i=1 31 | skf = StratifiedKFold(n_splits=Fold, random_state=42, shuffle=False) 32 | y_true= np.ones(labels.shape) 33 | y_pred = np.ones(labels.shape) 34 | 35 | for idx1, idx2 in skf.split(features, labels): 36 | print('\nTesting model in fold : %i\n'%(i)) 37 | i = i+1 38 | train_data=features[idx1,:] 39 | # print(train_data.shape) 40 | train_label=labels[idx1] 41 | # print(train_label.shape) 42 | 43 | test_data = features[idx2,:] 44 | test_label = labels[idx2] 45 | 46 | # normalization 47 | scaler = preprocessing.StandardScaler().fit(train_data) 48 | train_data = scaler.transform(train_data) 49 | min_max_scaler = preprocessing.MinMaxScaler() 50 | train_data = min_max_scaler.fit_transform(train_data) 51 | train_label =train_label.astype(int) 52 | 53 | test_data = scaler.transform(test_data) 54 | test_data = min_max_scaler.transform(test_data) 55 | test_label =test_label.astype(int) 56 | 57 | 58 | ###SVM##### 59 | clf = svm.SVC(kernel='rbf',gamma=0.09) 60 | # start training 61 | clf.fit(train_data, train_label) 62 | # output_train 63 | # tr = clf.predict(train_data) 64 | # prediction_test 65 | predictions = clf.predict(test_data) 66 | 67 | # acc_train = clf.score(intr, labeltr) 68 | 69 | acc_test = clf.score(test_data, test_label) 70 | print('test acc is {}'.format(acc_test)) 71 | 72 | y_pred[idx2,] = predictions 73 | y_true[idx2,] = test_label 74 | 75 | print('\n Mean accuracy is {}'.format(accuracy_score(y_true,y_pred))) 76 | -------------------------------------------------------------------------------- /tdt.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | import pandas as pd 5 | import numpy as np 6 | #from numpy import nan as NaN 7 | from sklearn import svm 8 | import matplotlib.pyplot as plt 9 | from pylab import mpl 10 | import numpy as np 11 | import pandas as pd 12 | from sklearn import preprocessing 13 | from sklearn.model_selection import StratifiedKFold 14 | from sklearn.metrics import accuracy_score 15 | from sklearn.neighbors import KNeighborsClassifier 16 | import sklearn.model_selection as ms 17 | import sklearn.metrics as sm 18 | from sklearn import tree 19 | from sklearn.ensemble import RandomForestClassifier 20 | 21 | 22 | 23 | ###import data### 24 | data=pd.read_csv('data.csv',header=None) 25 | data_1=data.dropna(axis=1) 26 | #print(data_1) 27 | features=np.array(data_1.iloc[:,2:].values) 28 | marks =data.iloc[:,0].values 29 | #print(features.shape) 30 | labels= np.zeros(len(marks)) 31 | labels[marks=='R']=1 32 | labels[marks=='F']=2 33 | 34 | 35 | Fold = 10 36 | i=1 37 | skf = StratifiedKFold(n_splits=Fold, random_state=42, shuffle=False) 38 | y_true= np.ones(labels.shape) 39 | y_pred = np.ones(labels.shape) 40 | 41 | for idx1, idx2 in skf.split(features, labels): 42 | print('\nTesting model in fold : %i\n'%(i)) 43 | i = i+1 44 | train_data=features[idx1,:] 45 | # print(train_data.shape) 46 | train_label=labels[idx1] 47 | # print(train_label.shape) 48 | 49 | test_data = features[idx2,:] 50 | test_label = labels[idx2] 51 | 52 | # normalization 53 | scaler = preprocessing.StandardScaler().fit(train_data) 54 | train_data = scaler.transform(train_data) 55 | min_max_scaler = preprocessing.MinMaxScaler() 56 | train_data = min_max_scaler.fit_transform(train_data) 57 | # print(train_data) 58 | train_label =train_label.astype(int) 59 | 60 | test_data = scaler.transform(test_data) 61 | test_data = min_max_scaler.transform(test_data) 62 | test_label =test_label.astype(int) 63 | 64 | ##The decision tree### 65 | clf=tree.DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None, 66 | max_features=None, max_leaf_nodes=None, 67 | min_impurity_decrease=0.0, min_impurity_split=None, 68 | min_samples_leaf=1, min_samples_split=2, 69 | min_weight_fraction_leaf=0.0, presort=False, random_state=None, 70 | splitter='best') 71 | clf.fit(train_data,train_label) 72 | predictions=(clf.predict(test_data)) 73 | acc_test=clf.score(test_data,test_label) 74 | print('test acc is {}'.format(acc_test)) 75 | 76 | ###model evalution#### 77 | y_pred[idx2,] = predictions 78 | y_true[idx2,] = test_label 79 | 80 | print('\n Mean accuracy is {}'.format(accuracy_score(y_true,y_pred))) 81 | --------------------------------------------------------------------------------