Principal component analysis (PCA) and independent component analysis (ICA) in python

Introduction

When analyzing electroencephalogram (EEG) and magnetoencephalography (MEG) data in research, we decided to use PCA and ICA to decompose the components of brain activity and analyze the results of dimensionality reduction as features. To be honest, I don't really understand the mathematical background, but for the time being, I have summarized the PCA and ICA of time series data as a memorandum on an implementation basis.

Principal component analysis (PCA) and independent component analysis (ICA)

What is PCA

Wikipedia Than,

In other words ** When it can be assumed that the data follows a normal distribution, the method of taking the axis in the orthogonal direction in order from the component with the largest variance. Since the bases are orthogonal, the outputs are uncorrelated data with each other (there is no linear relationship, but there may be a non-linear relationship) **

What is ICA

[Wikipedia](https://ja.wikipedia.org/wiki/%E7%8B%AC%E7%AB%8B%E6%88%90%E5%88%86%E5%88%86%E6%9E From% 90, "Wikipedia")

In other words ** A technique that axes in the direction of maximum independence when it can be assumed that the data are statistically independent and do not follow a normal distribution. The output will be independent data that does not follow a normal distribution (no linear or non-linear relationships) **

Difference between PCA and ICA (image)

PCAとICA (Reference http://meg.aalip.jp/ICA/)

PCA implementation

PCA.py


import numpy as np
import matplotlib.pyplot as plt

from sklearn.decomposition import PCA
X = input_data #400 (number of data) x 300 (time series) data

#PCA execution
pca = PCA(n_components=20, random_state=0)#Make 20 bases (components)
feature = pca.fit_transform(X)
PCA_comp =  pca.components_ #Basis matrix

#Restore 0th data with 20 components
plt.figure(figsize = (12, 2))
plt.plot(X[0], label="data")#0th data
plt.plot(np.dot(feature[0], PCA_comp).T, label="reconstruct")#Restored 0th data
plt.legend()

PCA_fig.png

Implementation of ICA

ICA.py


import numpy as np
import matplotlib.pyplot as plt

from sklearn.decomposition import FastICA
X = input_data #400 (number of data) x 300 (time series) data

#Implementation of ICA
ICA = FastICA(n_components=20, random_state=0)#Make 20 bases (components)
X_transformed = ICA.fit_transform(X)
A_ =  ICA.mixing_.T #Mixed matrix

#Restore 0th data with 20 components
plt.figure(figsize = (12, 2))
plt.plot(X[0], label="data")#0th data
plt.plot(np.dot(X_transformed[0], A_), label="reconstruct")#Restored 0th data
plt.legend()

ICA_fig.png

in conclusion

This time, I have summarized PCA and ICA as a memorandum. I thought it would be okay because I was able to implement it for the time being, but I think I need to acquire more theoretical knowledge for the future. I would like to add about NMF (Non-negative matrix factorization).

reference

Dimension reduction method (summary and implementation) PCA, LSI (SVD), LDA, ICA

Recommended Posts

Principal component analysis (PCA) and independent component analysis (ICA) in python
Principal component analysis (Principal component analysis: PCA)
Robot grip position (Python PCA principal component analysis)
2. Multivariate analysis spelled out in Python 3-2. Principal component analysis (algorithm)
2. Multivariate analysis spelled out in Python 3-1. Principal component analysis (scikit-learn)
[Python] Comparison of Principal Component Analysis Theory and Implementation by Python (PCA, Kernel PCA, 2DPCA)
Python: Unsupervised Learning: Principal Component Analysis
Coursera Machine Learning Challenges in Python: ex7-2 (Principal Component Analysis)
Visualize the correlation matrix by principal component analysis in Python
Principal component analysis
Principal component analysis with Power BI + Python
Dimensional compression with self-encoder and principal component analysis
Recognize the contour and direction of a shaped object with OpenCV3 and Python3 (Principal component analysis: PCA, eigenvectors)
Association analysis in Python
Regression analysis in Python
Collaborative filtering with principal component analysis and K-means clustering
Merge sort implementation / complexity analysis and experiment in Python
Clustering and principal component analysis by K-means method (beginner)
Principal component analysis Analyze handwritten numbers using PCA. Part 2
Principal component analysis Analyze handwritten numbers using PCA. Part 1
Axisymmetric stress analysis in Python
Unsupervised learning 3 Principal component analysis
Implementation of independent component analysis
Stack and Queue in Python
Simple regression analysis in Python
Unittest and CI in Python
EEG analysis in Python: Python MNE tutorial
[Python] How to do PCA in Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
First simple regression analysis in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Introduction to Python Basics of Machine Learning (Unsupervised Learning / Principal Component Analysis)
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Principal component analysis with Spark ML
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Planar skeleton analysis in Python (2) Hotfix
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Principal Component Analysis with Livedoor News Corpus-Practice-
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Implementation module "deque" in queue and Python