Revision bc4ae43da0ba264e180a4f05f33f25a7f53724e2 authored by Susanne Zabel on 30 March 2021, 09:06:11 UTC, committed by Susanne Zabel on 30 March 2021, 09:06:11 UTC
1 parent b252134
PCA.cpython-37.pyc
B
7iG`1 � @ s| d dl mZ d dlZd dlmZ e�dd� d dlm Z d dlmZ d dl m
Z d dlmZ d a
dd � ZG d
d� de�ZdS )� )�arrayN)�configZjax_enable_x64T)�jacrev)�DimensionReductionMethodc C s� | t j| dd� } d| jd t �t �| �| � }t j�|�\}}t j| dd�}t j| dd� }t �|d d �|f �dt �d d �f }|dt � |fS )Nr )�axis� )
�np�mean�shape�dot� transpose�linalg�eigh�argsort�sort�N_COMPONENTS)�X�
covariance�eigenvalues�eigenvectors�sorting� r �6/share/home/zabel/projects_romanov/jax/src2/jax/PCA.py�pca_forward s &r c @ sf e Zd ZdZdd� Zdd� Zddd�Zd d
� Zdd� Zd
d� Z ddd�Z
ddd�Zdd� Zdd� Z
dS )�PCAan PCA: Class computing the PCA and corresponding Jacobian of a matrix.
Attributes:
matrix (DataMatrix): Instance of the DataMatrix class
n_compontents (int): Number of output dimensions
axis (int): 0 or 1 indicating if features are in columns (0) or rows (1)
compute_jacobian (Bool): indicates if jacobian should be computed or notc O s6 t j| f|� d | _d | _d | _d | _d | _d | _d S )N)r �__init__r r r �cov_eigenvectors�jacobian_eigenvalues�cov_eigenvalues)�self�args�kwargsr r r r $ s zPCA.__init__c C s� | j tj| j dd� | _ d| j jd t�t�| j �| j � | _tj�| j�\| _ | _
tj| j dd�}tj| j dd�d| j
� | _ t�| j
d d �|f �d| j
�d d �f | _
d S )Nr )r r )�matrixr r r
r r r r
r r r r r �n_components)r r r r r � pca_value- s ( z
PCA.pca_valueTc C s� | j r,| jatt�| j�\| _| _| �� n| �� t �
| j�| _| j r�t �t j| j| j| j
d | j
d | j
d fdd�| j
d | j | j
d | j
d f�| _t �| j| j| j
d | j
d f�| _d S )Nr r �F)�order)Zcompute_jacobianr# r r r r"