Discussion:
[Scikit-learn-general] Complex Valued Data and PCA
David Brough
2016-03-30 17:15:44 UTC
Permalink
Hello all,

I have a complex valued array that I would like to do PCA on, but I am
running into an issue where
sklearn is casting the complex128 array as a float64.


X = np.random.random((30, 50)) + 1j * np.random.random((30, 50))
pca = RandomizedPCA()
pca.fit(X)

ComplexWarning: Casting complex values to real discards the imaginary
part

It looks like it is coming form the following lines of code.

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/pca.py#L639
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/utils/validation.py#L101

Is there a way to do PCA on complex valued array with sklearn?

Thanks,

David
Michael Eickenberg
2016-03-30 17:37:04 UTC
Permalink
easiest would be to associate each one of your complex features to an R^2
real feature. You can do this with np.hstack([np.real(X), np.imag(X)]).
This has more degrees of freedom in a sense, since you are not constrained
by complex multiplication anymore. If however this is essential to you I'd
suggest to do the PCA by hand as centering -> covariance -> eigh or
something to that effect.

hope that helps,
Michael
Post by David Brough
Hello all,
I have a complex valued array that I would like to do PCA on, but I am
running into an issue where
sklearn is casting the complex128 array as a float64.
X = np.random.random((30, 50)) + 1j * np.random.random((30, 50))
pca = RandomizedPCA()
pca.fit(X)
ComplexWarning: Casting complex values to real discards the imaginary
part
It looks like it is coming form the following lines of code.
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/pca.py#L639
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/utils/validation.py#L101
Is there a way to do PCA on complex valued array with sklearn?
Thanks,
David
Continue reading on narkive:
Loading...