Skip to content

Unable to perform complex PCA due to data type cast #10422

Closed
@chris-hld

Description

@chris-hld

Description

pca._fit() casts input arrays to float types.

ComplexWarning: Casting complex values to real discards the imaginary part
  array = np.array(array, dtype=dtype, order=order, copy=copy)

This forbids PCAs on complex input data, although possible with scipy.linalg.svd

Steps/Code to Reproduce

import numpy as np
from sklearn.decomposition import PCA

X = np.array([[-1+1j, -1], [-2j, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])

pca = PCA(svd_solver='full')

Y = pca.fit_transform(X)
B = pca.components_
print(pca.singular_values_)
print(B)

X_reconstructed = pca.inverse_transform(Y)
print(np.allclose(X, X_reconstructed))

Expected Results

True

Actual Results

False

Versions

Linux-4.10.0-42-generic-x86_64-with-debian-stretch-sid
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0]
NumPy 1.13.3
SciPy 0.19.1
Scikit-Learn 0.19.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions