Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

B.E.

PROJECT
ON
MUSIC EVOKED EMOTION
CLASSIFICATION OF EEG SIGNAL
USING MACHINE LEARNING
Submitted by

VIVEK MAJUMDAR (572/IC/14)


KARTIK NISHAD (471/IC/14)
VIVEK NIGAM (573/IC/14)
VISHAL CHOPRA (568/IC/14)

(In partial fulfillment of B.E. (Instrumentation and Control Engineering) degree


of University of Delhi

Under the Guidance of


MR. VICKY SURI

DIVISION OF INSTRUMENTATION AND CONTROL ENGINEERING


NETAJI SUBHAS INSTITUTE OF TECHNOL0GY
UNIVERSITY OF DELHI, DELHI
MAY 2018
ACKNOWLEDGEMENTS

It is our greatest pleasure to thank everyone who was related with our thesis project. It wouldn‟t

have been possible without their effort and encouragement. We would like to thank our supervisors, Mr. Vicky

Suri for helping us and guiding us with the topic and for their constant support through any problems which we

faced while proceeding with the project. We are deeply grateful for all the encouragement and support during

the entire course of work.

We extend our heartfelt thanks to all the faculty members. We are grateful to the institute for providing

us with the environment for research and development. Finally before closing, we would like to thank our

parents who have helped us throughout the project and encouraged us to aim for the best..

Vivek Majumdar Kartik Nishad Vivek Nigam Vishal Chopra


(572/IC/14) (471/IC/14) (563/IC/14) (568/IC/14)

Instrumentation and Control Engineering Division


Netaji Subhas Institute of Technology (NSIT)
Azad Hind Fauj Marg
Sector-3, Dwarka, New Delhi
PIN - 110078
DECLARATION

This is to certify that the project entitled, “Music Evoked Emotion Classification Of EEG Signal

Using Machine Learning” by Vivek Majumdar, Kartik Nishad, Vivek Nigam, Vishal Chopra is a record

of bonafide work carried out by us, in the Division of Instrumentation and Control Engineering, Netaji Subhas

Institute of Technology, University of Delhi, New Delhi, in partial fulfillment of requirements for the award

of the degree of Bachelor of Engineering in Instrumentation and Control Engineering, University of Delhi in

the academic year 2014-2018.

The results presented in this thesis are original and have not been submitted to any other university in

any form for the award of any other degree.

Vivek Majumdar Kartik Nishad Vivek Nigam Vishal Chopra


(572/IC/14) (471/IC/14) (573/IC/14) (568/IC/14)

Instrumentation and Control Engineering Division


Netaji Subhas Institute of Technology (NSIT)
Azad Hind Fauj Marg
Sector-3, Dwarka, New Delhi
PIN - 110078
CERTIFICATE

This is to certify that the project entitled, “Music Evoked Emotion Classification Of EEG Signal

Using Machine Learning” by Vivek Majumdar, Kartik Nishad, Vivek Nigam, Vishal Chopra is a record

of bonafide work carried out by them, in the Division of Instrumentation and Control Engineering, Netaji

Subhas Institute of Technology, University of Delhi, New Delhi, under our supervision and guidance in partial

fulfillment of requirements for the award of the degree of Bachelor of Engineering in Instrumentation and

Control Engineering, University of Delhi in the academic year 2014-2018.

The results presented in this thesis are original and have not been submitted to any other university in

any form for the award of any other degree.

Mr. Vicky Suri


Assistant Professor
Instrumentation and Control Engineering Division
Netaji Subhas Institute of Technology (NSIT)
Azad Hind FaujMarg
Sector-3, Dwarka, New Delhi
PIN - 110078
CERTIFICATE

This is to certify that the project entitled, “Music Evoked Emotion Classification Of EEG Signal

Using Machine Learning” by Vivek Majumdar, Kartik Nishad, Vivek Nigam, Vishal Chopra is a record

of bonafide work carried out by them, in the division of Instrumentation and Control Engineering, Netaji

Subhas Institute of Technology, University of Delhi, New Delhi, in partial fulfillment of requirements for the

award of the degree of Bachelor of Technology in Instrumentation and Control Engineering, University of

Delhi in the academic year 2014-2018.

Prof. SMRITI SRIVASTAVA


Head of the Division
Division of Instrumentation and Control Engineering
Netaji Subhas Institute of Technology (NSIT)
Azad Hind Fauj Marg
Sector-3, Dwarka, New Delhi
PIN – 110078
BTP thesis Vivek Nigam
ORIGINALITY REPORT

% 19
SIMILARITY INDEX
% 14
INTERNET SOURCES
% 7
PUBLICATIONS
% 11
STUDENT PAPERS

PRIMARY SOURCES

1
Submitted to Vel Tech University
Student Paper
% 2

2
ctms.engin.umich.edu
Internet Source
% 1

3
es.mathworks.com
Internet Source
% 1
Submitted to Edith Cowan University % 1
4 Student Paper

www.circuitstoday.com
% 1
5 Internet Source

www.mathworks.com
% 1
6 Internet Source

en.wikibooks.org
% 1
7 Internet Source

% 1
Submitted to Clemson University
8 Student Paper
Internet Source

16
Submitted to RMIT University
Student Paper
<%1

17
fachschaft.etec.uni-karlsruhe.de
Internet Source
<%1

18
www.monografias.com
Internet Source
<%1

19
www.dtic.mil
Internet Source
<%1

20
www.coursehero.com
Internet Source
<%1

21
www.cl.cam.ac.uk
Internet Source
<%1

22
en.wikipedia.org
Internet Source
<%1

23 Submitted to Netaji Subhas Institute of <%1


Technology
Student Paper

Submitted to TAR University College <%1


24 Student Paper

www.slideshare.net <%1
25 Internet Source
26
www.scribd.com
Internet Source <%1
ABSTRACT

Emotions play an important role in the daily life of human beings, the need and

importance of automatic emotion recognition has grown with increasing role of Brain

Computer Interface (BCI) applications. Here the emotions are generated by musical signal

stimuli and detected using Electroencephalography (EEG) signals. We use Independent

Component Analysis (ICA) to remove artifacts from the signal. We use logistic regression

(LR) and support vector machine (SVM) to classify emotions

i
LIST OF TABLES
Table 2.1: Nomenclature of electrodes in 10 – 20 system ..........................................................................5

Table 2.2: New MCN code for intermediate electrodes ..............................................................................7

ii
LIST OF FIGURES

Figure 2.1:10-20 Placement of EEG electrodesl ........................................................................................... 4

Figure 2.2: High Resolution of EEG ................................................................................................................. 6

Figure 2.3:Block diagram of EEG. .................................................................................................................... 8

Figure 2.4: Emotion classification graph . ..................................................................................................... 11

Figure 3.1: Channel location using 10-20 electrode system..................................................................... 13

Figure 3.2: EEGLAB toolbox in MATLAB ................................................................................................. 14

Figure 3.3:pop.import(), Import data dailogbox .......................................................................................... 15

Figure 3.4:After importing dataset into EEGLABv14.1.2 ........................................................................ 16

Figure 3.5FIR filter dialogbox: ......................................................................................................................... 17

Figure 3.6: Frequency response plot of filtered EEG................................................................................. 17

Figure 3.7:Importing dataset into MATLAB ................................................................................................ 18

Figure 3.8: Power spectral density ................................................................................................................... 20

Figure 4.1: Select mode of ICAl. ..................................................................................................................... 27

Figure 4.2: .Window showing ICA weights is equal to YES ................................................................... 28

Figure 4.3:Visulaizationo of ICA component maps. .................................................................................. 29

Figure 4.4: Marking the channel compoent maps manually .................................................................... 30

Figure 4.5:Removing the channel components . .......................................................................................... 31

Figure 4.6: Remove the EEG plot .................................................................................................................... 31

Figure 4.7:Channel components maps after removing artefacts . ........................................................... 32

Figure 4.8:Export data dialogbox . .................................................................................................................. 33

iii
LIST OF ABBREVIATIONS AND SYMBOLS

If you do not have any symbols, abbreviations, or specific nomenclature in your thesis, you

do not need to fill out this table.

Abbreviations Definition

BCI Brain computer interface


EEG Electroencephalography
ERP Event related potential
MCN Modified combinatorial nomenclature
LDA Linear discriminant analysis
ICA Independent components analysis
PCA Principle components analysis
SBM Support vector machine
LR Logistics regression
FFT Fast Fourier transform
PSD Power spectral density
BPC Band power change
HOC Higher order crossing

iv
INDEX OF EQUATIONS

Equation Caption Page

Equation 4.1 Standardization 21

Equation 4.2 Normalization 22

Equation 4.3 Principle component waveform 23

Equation 4.4 Gaussian approximation 24

v
TABLES OF CONTENT

ACKNOWLEDGEMENTS ........................................................................................................................... i
DECLARATION ............................................................................................................................................. ii
CERTIFICATE............................................................................................................................................... iii
CERTIFICATE................................................................................................................................................ iv
PLAGIRISM REPORT .................................................................................................................................. v
ABSTRACT ..................................................................................................................................................... vi
LIST OF TABLES ........................................................................................................................................vii
LIST OF FIGURES ..................................................................................................................................... viii
LIST OF ABBREVIATIONS AND SYMBOLS .................................................................................. ix
INDEX OF EQUATIONS ............................................................................................................................ x
TABLES OF CONTENTS........................................................................................................................... xi
Chapter 1
Introduction ..................................................................................................................................................... 1
1.1 motivation ..................................................................................................................................... 1
1.2 outline of the thesis .................................................................................................................... 2

Chapter 2
Introduction to EEG and emotions ........................................................................................................ 3
2.1 introduction .................................................................................................................................. 3
2.2 types of electrode placement ................................................................................................... 3
2.2.1 international 10-20 system ..................................................................................... 4
2.2.2 higher resolution system ......................................................................................... 5
2.3 types of EEG waves patterns .................................................................................................. 6
2.4 Emotions classification ...........................................................................................................10

Chapter 3
Signal processing of EEG .........................................................................................................................12
3.1 introduction ................................................................................................................................12
3.2 data acquisition..........................................................................................................................12
3.3 signal processing in MATLAB ............................................................................................14
3.4 spectral analysis of EEG using MATLAB ........................................................................18

vi
Chapter 4

Data pre-processing ....................................................................................................................................21

4.1 introdution ..................................................................................................................................21

4.2 data standardization .................................................................................................................21

4.3 data normalization ....................................................................................................................22

4.4 dimensionality reduction ........................................................................................................22


4.4.1feature selection ......................................................................................................22
4.4.2feature extraction ....................................................................................................23
4.4.2.1Principal component analysis ..................................................................23
4.4.2.2Independent component analysis ...........................................................24
4.5 applying ICA to artefact correction .....................................................................................26

4.6 artefacts removal using MATLAB ......................................................................................27

Chapter 5
Introduction to Machine Learning ...................................................................................................... 34
5.1 Introduction ............................................................................................................................................. 34
5.1.1Supervised Learning…………………………………………………………….34
5.1.2Unsupervised Learning…………………………………………………………35
5.1.3Reinforced Learning……………………………………………………………35

5.2Classification of EEG data ................................................................................................................... 34


5.2.1Source code using LR…………………………………………………………..36

5.2.2Source code using SVM without kernel………………………………………...37

5.2.3 LR source code…………………………………………………………………39


5.2.4.SVM source code………………………………………………………………43
5.2.5Visualization code to plot decision line .......................................................................... 48

vii
CHAPTER 1
INTRODUCTION

This chapter entails the motivation behind the study and a basic outline of the

thesis. The literature review and recent advancements in this field of study has also

been discussed.

1.1 Motivation
The stress is a serious problem in today's society because of brisk traffic

facilities and developed information society. Thus, chronic stress has a bad influence

on mind and body, and causes various illness. In order to solve such a problem,

researchers have studied healing, especially musical therapy [1].

Brain–computer interfaces (BCIs) are emerging interfaces that have a lot of potential

as an application of signal processing and machine learning techniques. BCIs are not

only used to control a computer or a device but also for music therapy and

recommendations [2].

To investigate brain responses to sound, spectral analysis based on fast Fourier

transformed (FFT) is used to analyze EEG. Different bands of EEG have been defined

to describe the different physiological conditions.

In music-related researches, the band powers are important parameters. The EEG is

the signal measured by sensors attached in the head and then amplified. It is said the

EEG is generated by brain's electric activity. The signal changes by external stimulus

1
as sound and inner stimulus as feelings, therefore an EEG pattern can change by

listening different music.

In this research EEG data is processed with removal of the artifacts using ICA.

Classification of the EEG signal is done using different machine learning algorithms

to correlate with manual assessment of the musical stimuli, calculating the accuracy

achieved in different algorithms.

1.2 Outline of the thesis

The rest of the thesis is organised as follows. In chapter 1, a basic introduction of the

research is shown. It tells about the motivation and purpose of the thesis outlining the

topics which are going to covered in it. In chapter 2, detailed information of

electroencephalography (EEG) and emotion recognition is introduced. In chapter 3,

signal processing of EEG using matlab with EEGLAB toolbox is studied. In chapter

4, how the data is pre-processed that is feature extraction and selection is done using

ICA.In chapter 5, a brief introduction of machine learning and classification of

emotion using EEG signal is described.

2
CHAPTER 2
INTRODUCTION TO EEG AND EMOTIONS

2.1 Introduction
Electroencephalography is a technique that records the electrical activity of

the brain. During an EEG test, small electrodes like cup or disc type are placed on the

scalp. They pick up the brain's electrical signals and send them to a machine called

electroencephalogram. It records the signals as wavy lines on to a computer screen or

paper in order of microvolt.

In 1875, Sir Richard Caton presented his finding about electrical phenomena

of the exposed cerebral hemispheres of rabbits and monkeys. In 1890, Sir Adolf Beck

published an investigation of spontaneous electrical activity of the brain of rabbits and

dogs. In 1924, Sir Hans Berger recorded the first human EEG.

An EEG is mainly used when there is a need to diagnose and manage epilepsy.

It can also be used to investigate other conditions such as encephalitis, dementia, head

injuries, brain tumors, hemorrhage. An EEG can identify areas of the brain that are

not working properly. EEGs are also used to determine the level of brain function in

people who are in a coma.

2.2 Types of EEG electrode placement

3
2.2.1 International 10-20 system

The 10-20 system or International 10-20 is an internationally standardised

method to describe and apply allocation of electrodes while performing EEG,

polysomnograph, and voluntary lab research/experiment this method was introduce to

maintain the standardized testing insuring data subject study could be compiled,

reproduced an analyzed using scientific method.

Fig. 2.1 10-20 placement of EEG electrodes

The 10 and 20 prefers that the actual distance between adjacent electrodes are either

10% or 20% of the total front back or right left distance of the skull. Each electrode

has unique letter to locate different areas of the brain as shown in table 2.1.

4
Table 2.1 Nomenclature of Electrodes in 10 – 20 System

S.NO. NAME OF ELECTRODE ABBREVATION

1 Pre – frontal Pf

2 Frontal F

3 Temporal T

4 Parietal P

5 Occipital O

6 Central C

All the right hand side electrodes of the head are referred using even numbers whereas all

the left handed electrodes are odd numbers. All the mid line electrodes are referred with

“Z”. Z electrodes are used as „ground‟ or „references‟ in diagnostics of EEG.

Measurement is done separately for each electrodes that are F3, F4, P3 and P4.

It measure front to back and side to side and then diagonally through C3 and C4

points.

2.2.2 Higher Resolution System

High resolution system is another type of electrodes placement similar to 10 –

20 system using more electrodes in it. The spacing between all electrodes is done in

5
10% division. The new electrodes naming is done according to Modified

Combinatorial Nomenclature (MCN).

Fig. 2.2 Higher resolution system of EEG

The MCN system uses 1,3,5,7,9 for left hemisphere of skull which represents 10%-

50% inions-nation distance respectively. The new MCN code for intermediate

electrodes are shown in table 2.2.

6
Table 2.2 New MCN codes for intermediate electrodes

S.NO. NAME OF ELECTRODES POSITION

BETWEEEN

ELECTRODES

1 AF Fp and F

2 FC F and C

3 FT F and T

4 CP C and P

5 TP T and P

6 PO P and O

New MCN codes after introduction of new electrodes in the former electrode system.

 T3 is now T7

 T4 is now T8

 T5 is now P7

 T6 is now P8

7
8
 2.3 Type of EEG wave patterns


  DELTA WAVE

  Frequency range 0.2-4 Hz

  Deep sleep condition of adults


  THETA WAVES

  Frequency range 4-8 Hz

  Relaxing, day dreaming etc.




  ALPHA WAVES

  Frequency range from 8-16 Hz

  These waves represent non-arousal emotions.

  BETA WAVES

  Frequency range 16– 32 Hz

  These waves represent arousal emotion.

  GAMMA WAVES

  Frequency range approximately 32–48 Hz

  Perception that combines two different senses, such as sound and sight

 
Shortsensations-termmemory matching of recognized objects, sounds, or tactile

2.4 EMOTIONS CLASSIFICATION

9
Emotion classification is done on the basis of two factors that is (1) arousal and (2)

valence. It states that we react to a stimulus and experience the associated emotion at

the same time. We feel emotions and experience physiological reactions such as

sweating, trembling and muscle tension simultaneously.It states that emotions occur

as a result of physiological reactions to events. Purposes will interpret physical

reactions and conclude that one is frightened of.

2.4.1 Valence

Valence emotion refers to how strongly emotion is experienced as positive and

negative. It tells about how much positive or negative the data is. Vlalence

include mental emotions like happiness, sadness etc. marking of valence

emotion is done according to how pleasant and unpleasant emotion is. For

pleasant emotion vaalence would be positive whereas for unpleasant it would

be unpleasant.

2.4.2 Arousal

Physical arousal is a emotion comprises mental as well as physical emotion of

the body. It include the data of EEG + heart rate + skin conductance (EMG) to

classify Emotion we consider 2-D map in which valence is allotted x-axis

whereas arousal is allotted in y-axis.

10
Fig 2.4 Emotion Classification

11
CHAPTER 3

SIGNAL PROCESSING OF EEG

3.1 Introduction
This chapter includes the data acquisition of EEG signal, workings of

EEGLAB toolbox using MATLAB for signal processing. Musical signal are used as

stimuli to evoked EEG potential. The data is then filtered out according to EEG

frequency range. After filtering the data is ready for removing unwanted artefacts,

Artefacts removal can be done using Independent Component Analysis (ICA),

Principal Component Analysis (PCA) and Linear Discriminate Analysis (LDA).

3.2 Data Acquisition

Datasets is achieved from online site BNCI Horizon. BNCI Horizon is an

online platform all EEG datasets collected in BCI competition.The data collected in a

database from Auditory Repository Competition. In this competition six healthy

subjects are placed in a noise free room. There were three sessions and each of them

incorporated four parts:

 15 seconds natural EEG.



 30 seconds music clips, during which central optical complex was requested to

decrease eyes movement.

12
 Another 15 seconds impulsive EEG.

 At the end of each trial, participants perform a self – evaluation of valence
and arousal.

The EEG signals were continuously recorded by 16 channel EEG system as shown

in fig 3.1, with a sampling rate of 256Hz. The electrodes followed the

international 10-20 system and their impedances were less than10kΩ.

Fig 3.1 Channel location using 10-20 electrode system

13
3.3 Signal processing in MATLAB

To process the given EEG dataset, we have to first install EEGLAB toolbox in

MATLAB. After installing EEGLAB, add EEGLAB source file destination path to

MATLAB. Then, RUN “eeglab.m” in MATLAB editor by writing >>eeglab in

command window as shown in fig3.2.

Fig3.2 EEGLAB Toolbox in MATLAB

14
Next step after importing EEGLAB in MATLAB is to import dataset into EEGLAB.

To import dataset, click on File→ Import data→ Using EEGLAB functions and

plugins→ From ASCII/float file or MATLAB array. After that a dialog box will open,

in which you have to select dataset file, sampling rate and channel location as shown

in Fig 3.3.

Fig 3.3 pop.importdata(), Import data dialog box

15
Fig 3.4 After importing dataset into EEGLABv14.1.2

After importing dataset into EEGLAB as shown in fig 3.4, the data is filter out using

FIR filters. The pattern of EEG signal spread over the frequency range between

0.2 - 50Hz is comprising of various frequency bands such as Delta(0.2- 4Hz),

Theta(4-8Hz), Alpha(8-16Hz), Beta(16-32Hz), and Gamma(32-48Hz). In order to

gather only EEG signal we have to filter out the data using 0.2- 50Hz band pass filter

(BPF). To filter the data in EEGLAB toolbox we have to click on Tool→ Filter the

data → Basic FIR filter (new, default). After that a dialog box will appear in which,

we have to fill the data according to our requirement as shown in fig3.5.

16
Fig 3.5 FIR filter window

Fig 3.6 Frequency response plot of filtered EEG

17
After filling all the data plot of frequency response is popped out showing filtered

data as shown in the fig 3.6.

3.4 Spectral analysis of EEG using MATLAB

Spectral analysis of EEG is done to retrieve the power and energy signal from the

EEG data. It is a kind of feature extraction in which we extract power spectral density

using FFT.

Power spectral analysis can be done in MATLAB by importing EEG

signal and then analyze it. To start with the processing, first we import the dataset

from the system as shown in the fig 3.7.

Fig 3.7 importing dataset into MATLAB

18
After inserting the data, we have to write a code for power spectral density (PSD)

applying fast Fourier transform(FFT) to get the value in frequency domain as shown

below.

%%%%%%%%%%%%%%%%

%%Source code PSD

Ns=256; %lenght of FFT

N=50184; %lenght of input sequence

psd = abs(fft(xtest{:,1},N).^2/Ns);

stem((0:N-1)/N,psd); %plot psd

xlim([0 1]);

%%%%%%%%%%%%%%%%

19
Then after writing the code we will plot the power spectral density as shown in fig

3.8.

Fig 3.8 Power spectral density plot

20
CHAPTER 4

Data Pre-Processing

4.1 Introduction
In this chapter, we will get to know how and why the data is being pre-

processed. Data pre-processing includes Standardization, Normalization,

Dimensionality reduction, etc. Dimensionality reduction includes Feature extraction

and Feature selection. Feature extraction includes ICA, PCA, LDA, etc. whereas

Feature Selection includes regularization etc.

For removal of artefacts from the EEG signal we have to apply ICA into

the dataset. There is a simpler way for applying ICA in MATLAB i.e using EEGLAB

toolbox.

4.2 Data Standardization


Data standardization is done to reduce the difference between the values

of the data. It done by subtracting the mean value from the individual values then

divide by its standard deviation.


( − )
=

Where , = mean values , s =standard deviation.

21
Standardized values lie between -1 to 1.

4.3 Data Normalization


Data Normalization is done to reduce the difference between the values of

the data. It done by subtracting the minimum value from the individual values then

divides by the difference of maximum value to the minimum value.

= ( − min( ))
(max( ) − min( ))

Where, min(x) = minimum value in the whole dataset (column),

Max(x) = maximum value in the whole dataset (column).

4.4 Dimensionality Reduction


Dimensionality reduction is done when we have a large dataset with too

many attributes. It includes feature extraction and selection. Feature extraction

comprises ICA, PCA, and LDA. It is a method in which we reduce number of

attribute values by obtaining its principal value.

4.4.1 Feature selection

In Feature selection, we find the subset of original attributes values. It has three

strategies to apply feature selection approach are:

  Filter Strategy
  Wrapper Strategy
 Embedded Strategy(Regularization)

In machine learning, for regression and classification of the large data, output is more

accurate for reduced space as compared to original space.

22
4.4.2 Feature extraction

In Feature extraction, data is transformed into high dimensional space of fewer

dimensions. The data is transformation could be linear like Principal component

analysis (PCA) or non-linear as in Independent component analysis (ICA).

4.4.2.1 Principal component analysis

It is used to derive the principal components of dataset. Multi-feature dataset can be

expressed by a m (variables), n (attributes) matrix, X and decomposed as a product of

three matrixes,

X= US

Where U is an m X n matrix such that

U=I

S is an N X N diagonal matrix, and V is an n X n matrix such that

V=V = I.

If X is any dataset of n features and p variables, U contains its n normalized

principal component waveforms which are linearly decorrelated and can be remixed

to rebuild the original data. S contains the N amplitudes of the N principal

component waveforms.We can describe the 'non-normalized' principal component

waveforms as the columns of

M = US.

23
The eigenvector matrix, V is essentially a set of topographic scalp maps, similar to

the columns of the matrix found by ICA.

4.4.2.2 Independent component analysis

Bell and Sejnowski have proposed a simple neural network algorithm that blindly

separates mixtures, x, of independent sources, s, using infomax. They show that

maximizing the joint entropy, H(y), of the output of a neural processor minimizes the

mutual information among the output components, is an invertible bounded

nonlinearity and

u = Wx,

a version of the original sources, s, identical save for scaling and permutation. This

implies that the distribution of the output approximates a uniform density. Independence

is achieved through the nonlinear squashing function which provides necessary higher-

order statistics through its Taylor series expansion. The learning rule can be derived by

maximizing output joint entropy, H(y), with respect to W giving,

W = [I + p] W

The `natural gradient' W term avoids matrix inversions and speeds convergence. The

form of the nonlinearity g(u) plays an essential role in the success of the algorithm.

The ideal form for g() is the cumulative density function (cdf ) of the distributions of

the independent sources. algorithm. The ideal form for g() is the cumulative density

function (cdf ) of the distributions of the independent sources.

24
4.5 Applying ICA to artefact correction

ICA is suitable for performing blind source separation on EEG data

because: It is plausible that EEG data recorded at multiple scalp sensors are linear

sums of temporally independent components arising from spatiallyfixed, distinct or

overlapping brain or extra-brain networks, and, spatial smearing of EEG data by

volume conduction does not involve significant time delays1 . For EEG analysis,

the rows of the input matrix x are the EEG signals recorded at different electrodes,

the rows of the output data matrix

are time courses of activation of the ICA components, and the columns of the

inverse matrix give the projection strengths of the respective components onto the

scalp sensors. The scalp topographies of the components allow us to examine their

biological feasibility. Corrected" EEG signals can then be derived as

Where is the matrix of activation waveforms, u, with rows representing

artifactual components set to zero. The rank of corrected EEG data is less than

that of the original data.

26
4.6 Artefacts removal using MATLAB
To remove eye blinking artefact using MATLAB we use EEGLAB toolbox

for ICA analysis. So after importing dataset and processing EEG data for further

process as explained above, we apply ICA into the dataset.

To imply ICA we have to select Tool→ Run ICA then a dialog box will

appear to select which algorithm to be used as shown in the fig 4.1.

Fig 4.1 Select mode of ICA

After selecting the mode of the ICA, click OK to RUN the program. After running

ICA there will conformation box will appear as shown in fig 4.2.

27
Fig 4.2 window showing ICA weights is equal to YES

Now to visualize the EEG component map Goto the Plot→ Component Map→ In 2-D

as shown in the fig 4.3.

28
Fig 4.3 Visualization of ICA component maps

Now Select the channel manually in which the intensity around eyes is maximum and

then mark the channel by Selecting the channel and mark it to Reject as shown in the

fig 4.4.

29
Fig 4.4 marking the channel component map manually

After selecting the channels to be rejected, remove the channel manually by Selecting

Tool → Reject component by Eye as shown in figure 4.5

30
Fig 4.5 removing the channel component

Click Ok to remove channel component. To show the new EEG plot select Plot→

component activation as shown in fig 4.6.

Fig 4.6 removed EEG plot;blue plot is original EEG; Red plot shows removed artefact EEG

plot

31
Now to visualize the EEG component map Goto the Plot→ Component Map→ In 2-D

as shown in the fig 4.7.

Fig 4.7 channel component map after removing artefact

After removing all the artefact export the data from EEGLAB, Goto File →

Export data as shown in fig 4.8.

32
Fig 4.8 Export data dialog box

33
CHAPTER 5

Introduction to Machine Learning

5.1 Introduction
In this chapter, we will learn about machine learning algorithm and

approaches to classify the data to reduce the difference between the assessment of the

human v/s prediction of the system. There are three type of learning:

  Supervised learning
  Unsupervised learning
 Reinforcement learning

5.1.1 Supervised Learning

In supervised learning, we train the dataset giving all the feature and command the

system to learn according to change of dataset. In this learning, we have input

variable i.e. X and an output variable i.e. Y and made a hypothesis model i.e.

The goal is to predict the mapping func in such a way that when we input new data

variables new_X, we can predict output variable to that data. Supervised learning

includes:

  Regression
  Simple Linear Regression
 Multiple Linear Regression

34
 Support Vector Regression

  Classification

  LR
  SVM

  SVM – kernel
 K - NN

5.1.2 Unsupervised Learning

In unsupervised learning, we don‟t give the output variable to classify the data.

System learning by clustering the data variable and then classify it. Unsupervised

learning is different from supervised one, as there no given correct answer for the

input data. Unsupervised learning includes:

  Classification
 Association

5.3.Reinforcement Learning

In reinforcement learning, the system learns through its experience. It marks the

accuracy and modifies to get optimised prediction. Reinforcement learning includes:

  Upper confidence bound (UCB)


 Thomson sampling

35
5.2 Classification of the EEG data
So after pre-processing the EEG dataset in chapter 4, we convert the

dataset into .csv file. To convert .mat file into .csv use online convertor from internet.

After converting into .csv file open Python IDE – PyCharm Community Edition

208.1.1x64. and start writing the code as shown below.

5.2.1 Source code using logistic regression

_____________________________________________________________________

#importing libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

#importing the dataset

dataset=pd.read_csv('xtest')

dataset1=pd.read_csv('ytest')

x = dataset.iloc[,].values

y = dataset1.iloc[:,1].values

#Splitting the dataset into the Training Set and Test Set

from sklearn.cross_validation import train_test_split

x_train,y_train,x_test,y_test = train_test_split(x,y,test_size = 0.25, random_state = 0)

#Feature Scaling

36
from sklearn.preprocessing import

StandardScaler sc_x= StandardScaler();

x_train = sc_x.fittransform(x_train)

xtest = sc_x.transform(x_test)

#fitting logistic regression to training set

from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression( random_state = 0 )

classifier.fit(xtrain , ytrain) #Predicting the test set

result

y_pred = classifier.predict(x_test)

#Making confusion matrix

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test,y_pred)

#adding all the diagnol values results in accuracy of the model

5.2.2 Source code using SVM without kernel

_____________________________________________________________________

#importing libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

37
#importing the dataset

dataset=pd.read_csv('xtest')

dataset1=pd.read_csv('ytest')

x = dataset.iloc[,].values

y = dataset1.iloc[:,1].values

#Splitting the dataset into the Training Set and Test Set

from sklearn.cross_validation import train_test_split

x_train,y_train,x_test,y_test = train_test_split(x,y,test_size = 0.25, random_state = 0)

#Feature Scaling

from sklearn.preprocessing import

StandardScaler sc_x= StandardScaler();

x_train = sc_x.fittransform(x_train)

xtest = sc_x.transform(x_test)

#fitting SVM to training set

from sklearn.svm import SVC

classifier = SVC(kernel = 'linear', random_state = 0 )

classifier.fit(xtrain , ytrain)

38
#Predicting the test set result

y_pred = classifier.predict(x_test)

#Making confusion matrix

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test,y_pred)

#adding all the diagonal values results in accuracy of the model

_____________________________________________________________________

5.2.3 Logistic Regression source code

_____________________________________________________________________

import math

import numpy as np

import pandas as pd

from pandas import DataFrame

from sklearn import preprocessing

from sklearn.linear_model import LogisticRegression

from sklearn.cross_validation import train_test_split

from numpy import loadtxt, where

from pylab import scatter, show, legend, xlabel, ylabel

min_max_scaler = preprocessing.MinMaxScaler(feature_range=(-1,1))

df = pd.read_csv("data.csv", header=0)

39
df.columns = ["grade1","grade2","label"]

x = df["label"].map(lambda x: float(x.rstrip(';')))

X = df[["grade1","grade2"]]

X = np.array(X)

X = min_max_scaler.fit_transform(X)

Y = df["label"].map(lambda x: float(x.rstrip(';')))

Y = np.array(Y)

X_train,X_test,Y_train,Y_test =

train_test_split(X,Y,test_size=0.33) clf = LogisticRegression()

clf.fit(X_train,Y_train)

print 'score Scikit learn: ',

clf.score(X_test,Y_test) pos = where(Y ==

1) neg = where(Y == 0)

scatter(X[pos, 0], X[pos, 1], marker='o',

c='b') scatter(X[neg, 0], X[neg, 1],

marker='x', c='r') xlabel('Exam 1 score')

ylabel('Exam 2 score')

legend(['Not Admitted',

'Admitted']) show() def

Sigmoid(z):

G_of_Z = float(1.0 / float((1.0 +

math.exp(-1.0*z)))) return G_of_Z

40
def Hypothesis(theta, x):

z=0

for i in xrange(len(theta)):

z += x[i]*theta[i]

return Sigmoid(z)

def Cost_Function(X,Y,theta,m):

sumOfErrors = 0

for i in xrange(m):

xi = X[i]

hi = Hypothesis(theta,xi)

if Y[i] == 1:

error = Y[i] * math.log(hi)

elif Y[i] == 0:

error = (1-Y[i]) * math.log(1-hi)

sumOfErrors += error

const = -1/m

J = const * sumOfErrors

print 'cost is ', J

return J

def Cost_Function_Derivative(X,Y,theta,j,m,alpha):

sumErrors = 0

for i in xrange(m):

41
xi = X[i]

xij = xi[j]

hi = Hypothesis(theta,X[i])

error = (hi - Y[i])*xij

sumErrors += error

m = len(Y)

constant = float(alpha)/float(m)

J = constant * sumErrors

return J

def Gradient_Descent(X,Y,theta,m,alpha):

new_theta = []

constant = alpha/m

for j in xrange(len(theta)):

CFDerivative = Cost_Function_Derivative(X,Y,theta,j,m,alpha)

new_theta_value = theta[j] - CFDerivative

new_theta.append(new_theta_value)

return new_theta

def Logistic_Regression(X,Y,alpha,theta,num_iters):

m = len(Y)

for x in xrange(num_iters):

42
new_theta = Gradient_Descent(X,Y,theta,m,alpha)

theta = new_theta

if x % 100 == 0:

Cost_Function(X,Y,theta,m)

print 'theta ', theta

print 'cost is ', Cost_Function(X,Y,theta,m)

5.2.3 SVM source code

import numpy as np

import cvxopt.solvers

import logging

MIN_SUPPORT_VECTOR_MULTIPLIER = 1e-5

class SVMTrainer(object):

def __init__(self, kernel, c):

self._kernel = kernel

self._c = c

def train(self, X, y):

lagrange_multipliers = self._compute_multipliers(X, y)

return self._construct_predictor(X, y, lagrange_multipliers)

43
def _gram_matrix(self, X):

n_samples, n_features = X.shape

K = np.zeros((n_samples, n_samples))

for i, x_i in enumerate(X):

for j, x_j in enumerate(X):

K[i, j] = self._kernel(x_i, x_j)

return K

def _construct_predictor(self, X, y, lagrange_multipliers):

support_vector_indices = \

lagrange_multipliers > MIN_SUPPORT_VECTOR_MULTIPLIER

support_multipliers = lagrange_multipliers[support_vector_indices]

support_vectors = X[support_vector_indices] support_vector_labels =

y[support_vector_indices]

# http://www.cs.cmu.edu/~guestrin/Class/10701-S07/Slides/kernels.pdf

# bias = y_k - \sum z_i y_i K(x_k, x_i)

# Thus we can just predict an example with bias of zero, and

# compute error.

bias = np.mean(

[y_k - SVMPredictor(

44
kernel=self._kernel,

bias=0.0,

weights=support_multipliers,

support_vectors=support_vectors,

support_vector_labels=support_vector_labels).predict(x_k)

for (y_k, x_k) in zip(support_vector_labels, support_vectors)])

return SVMPredictor(

kernel=self._kernel,

bias=bias,

weights=support_multipliers,

support_vectors=support_vectors,

support_vector_labels=support_vector_labels)

def _compute_multipliers(self, X, y):

n_samples, n_features = X.shape

K = self._gram_matrix(X)

P = cvxopt.matrix(np.outer(y, y) * K)

q = cvxopt.matrix(-1 * np.ones(n_samples))

45
G_std = cvxopt.matrix(np.diag(np.ones(n_samples) * -1))

h_std = cvxopt.matrix(np.zeros(n_samples))

G_slack = cvxopt.matrix(np.diag(np.ones(n_samples)))

h_slack = cvxopt.matrix(np.ones(n_samples) * self._c)

G = cvxopt.matrix(np.vstack((G_std, G_slack)))

h = cvxopt.matrix(np.vstack((h_std, h_slack)))

A = cvxopt.matrix(y, (1, n_samples))

b = cvxopt.matrix(0.0)

solution = cvxopt.solvers.qp(P, q, G, h, A, b)

return np.ravel(solution['x'])

class SVMPredictor(object):

def __init__(self,

kernel,

bias,

weights,

support_vectors,

support_vector_labels):

self._kernel = kernel

self._bias = bias

self._weights = weights

self._support_vectors = support_vectors

self._support_vector_labels = support_vector_labels

46
assert len(support_vectors) == len(support_vector_labels)

assert len(weights) == len(support_vector_labels)

logging.info("Bias: %s", self._bias)

logging.info("Weights: %s", self._weights)

logging.info("Support vectors: %s", self._support_vectors)

logging.info("Support vector labels: %s", self._support_vector_labels)

def predict(self, x):

"""

Computes the SVM prediction on the given features x.

"""

result = self._bias

for z_i, x_i, y_i in zip(self._weights,

self._support_vectors,

self._support_vector_labels):

result += z_i * y_i * self._kernel(x_i, x)

return np.sign(result).item()

47
5.2.3 Visualization code to plot the decision line

# Visualising the Training set results

from matplotlib.colors import ListedColormap

X_set, y_set = X_train, y_train

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max()

+ 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() +

1, step = 0.01))

plt.contourf(X1,X2,classifier.predictnp.array ([X1.ravel (), X2. ravel ()]).T).

reshape (X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))

plt.xlim(X1.min(), X1.max())

plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(y_set)):

plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],

c = ListedColormap(('red', 'green'))(i), label = j)

plt.title('SVM (Training set)')

plt.xlabel('Age')

plt.ylabel('Estimated Salary')

plt.legend()

plt.show()

48
# Visualising the Test set results

from matplotlib.colors import ListedColormap

X_set, y_set = X_test, y_test

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max()

+ 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1,

step = 0.01))

plt.contourf(X1,X2,classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.

shape), alpha = 0.75, cmap = ListedColormap(('red', 'green'))) plt.xlim(X1.min(),

X1.max())

plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(y_set)):

plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],

c = ListedColormap(('red', 'green'))(i), label = j)

plt.title('SVM (Test set)')

plt.xlabel('Age')

plt.ylabel('Estimated Salary')

plt.legend()

plt.show()

49
REFERENCES
1. P.Chiu, and A.Kumar, «Music Therapy: Loud Noise or Soothing Notes?,"

inlernalional Pedialrks, Vol. 18, no. 4, pp.204-20S, 2003

2. Removing Electroencephalographic Artifacts : Comparison between ICA and

PCA Tzyy-Ping Jung1 , Colin Humphries1 , Te-Won Lee1 , Scott Makeig2;3 ,

Martin J. McKeown1 , Vicente Iragui3 , Terrence J. Sejnowski1

3. I. Daly, D. Williams, A. Kirke, J. Weaver, A. Malik, F. Hwang, E. Miranda, and S. J.

Nasuto, “Affective brain-computer music interfacing,” J. Neural. Eng., vol. 13, no.

4, p. 046022, 2016.

4. Y.-H. Yang and H. H. Chen, “Machine recognition of music emotion: A review,”

ACM Transactions on Intelligent Systems and Technology (TIST), vol. 3, no.

3, p. 40, 2012.

5. [1] V. H. Anh, M. N. Van, B. B. Ha and T. H. Quyet (2012), “A Real-Time Model

Based Support Vector Machine for Emotion Recognition Through EEG”, in Proc.

ICCAIS, Ho Chi Minh City, Vietnam, 2012, pp. 191-196.

6. E.T. Berkman, D.K. Wong, M.P. Guimaraes, E.T. Uy, J.J. Gross, P.Suppes, “Brain

Wave Recognition of Emotions in EEG” Psychophysiology, vol. 41, 2004, pp.

S71-S7

50
BIODATA

1. Vivek Majumdar
572/IC/14
vivekmajumdar03@gmail.com

2. Kartik Nishad
471/IC/14
kartiknishad204@gmail.com

3. Vishal Chopra
568/IC/14
vishalchopra96500@gmail.com

4. Vivek Nigam
573/IC/14
viveknigam420@gmail.com

You might also like