/
You are viewing the documentation for metbit 9.0.0. Change release context

metbit.analysis.pca

Analysis and models module in metbit 9.0.0.

import metbit.analysis.pca

Classes

pca

PCA model

Parameters

Xarray-like, shape (n_samples, n_features)

Training data, where n_samples is the number of samples and n_features is the number of features.

labelarray-like, shape (n_samples,)

Target data, where n_samples is the number of samples.

features_namearray-like, shape (n_features,), default=None

Name of features.

n_componentsint, default=2

Number of components to keep.

scalestr, default='pareto'

Method of scaling. 'pareto' for pareto scaling, 'mean' for mean centering, 'uv' for unitvarian scaling.

random_stateint, default=42

Random state for permutation test.

test_sizefloat, default=0.3

Size of test set.

Examples:

import pandas as pd import numpy as np from metbit import pca

# Create a dataset data = pd.DataFrame(np.random.rand(500, 50000)) class_ = pd.Series(np.random.choice(['A', 'B', 'C'], 500), name='Group') time = pd.Series(np.random.choice(['1-wk', '2-wk', '3-wk', '4-wk'], 500), name='Time point')

# Assign X and target X = datasets.iloc[:, 2:] y = datasets['Group'] time = datasets['Time point'] features_name = list(X.columns.astype(float))

## Perform PCA model

pca_mod = pca(X = X, label = y, features_name=features_name, n_components=2, scaling_method='pareto', random_state=42, test_size=0.3) pca_mod.fit()

# Visualisation of PCA model pca_mod.plot_observe_variance()

pca_mod.plot_cumulative_observed()

shape_ = {'1-wk': 'circle', '2-wk': 'square', '3-wk': 'diamond', '4-wk': 'cross'}

pca_mod.plot_pca_scores(symbol=time, symbol_dict=shape_)

pca_mod.plot_loading_()

pca_mod.plot_pca_trajectory(time_=time, time_order={'1-wk': 0, '2-wk': 1, '3-wk': 2, '4-wk': 3}, color_dict={'A': '#636EFA', 'B': '#EF553B', 'C': '#00CC96'}, symbol_dict=shape_)

Methods

__init__(self, X: Union[pd.DataFrame, np.ndarray], label: Optional[Union[pd.Series, np.ndarray, List[Any]]]=None, features_name: Optional[Union[pd.Series, np.ndarray, List[Any]]]=None, n_components: int=2, scaling_method: str='pareto', random_state: int=42, test_size: float=0.3)
fit(self)
get_explained_variance(self)
get_scores(self)
get_loadings(self)
get_q2_test(self)
plot_observe_variance(self, fig_height: int=600, fig_width: int=800, font_size: int=15)

Visualise explained variance plot

Returns

figplotly.graph_objects.Figure

Explained variance plot.

plot_cumulative_observed(self, fig_height: int=600, fig_width: int=800, font_size: int=15, marker_size: int=10)

Visualise cumulative variance plot

Returns

figplotly.graph_objects.Figure

Cumulative variance plot.

plot_pca_scores(self, pc: List[str]=['PC1', 'PC2'], color_: Optional[pd.Series]=None, color_dict: Optional[dict]=None, symbol_: Optional[pd.Series]=None, symbol_dict: Optional[dict]=None, marker_label: Optional[pd.Series]=None, fig_height: int=900, fig_width: int=1300, marker_size: int=35, marker_opacity: float=0.7, font_size: int=20, title_font_size: int=21, individual_ellipse: bool=True, legend_name: List[str]=['Group', 'Time point'])

Visualise PCA scores plot

Parameters

pclist, default=['PC1', 'PC2']

List of principal components to plot.

colorarray-like, shape (n_samples,), default=None

Target data, where n_samples is the number of samples.

color_dictdict, default=None

Dictionary of color_ mapping.

symbol_array-like, shape (n_samples,), default=None

Target data, where n_samples is the number of samples.

symbol_dictdict, default=None

Dictionary of symbol_ mapping.

fig_heightint, default=900

Height of figure.

fig_widthint, default=1300

Width of figure.

marker_sizeint, default=35

Size of marker.

marker_opacityfloat, default=0.7

Opacity of marker.

text_array-like, shape (n_samples,), default=None

Text to display on each point.

Returns

figplotly.graph_objects.Figure

PCA scores plot.

plot_loading_(self, pc: List[str]=['PC1', 'PC2'], fig_height: int=600, fig_width: int=1800, font_size: int=20, title_font_size: int=20, marker_size: int=1, x_axis_title: str='𝛿<sub>H</sub> in ppm', xaxis_direction: str='reversed')

Visualise PCA loadings

Parameters

pclist, default=['PC1', 'PC2']

Principle component to plot.

fig_heightint, default=600

Height of figure.

fig_widthint, default=1800

Width of figure.

Returns

figplotly.graph_objects.Figure

Plotly figure.

----------

plot_pca_trajectory(self, time_: pd.Series, time_order: dict, stat_: List[str]=['mean', 'sem'], pc: List[str]=['PC1', 'PC2'], color_dict: Optional[dict]=None, symbol_dict: Optional[dict]=None, fig_height: int=900, fig_width: int=1300, marker_size: int=35, marker_opacity: float=0.7, title_font_size: int=20, font_size: int=20, legend_name: List[str]=['Group', 'Time point'])
plot_3d_pca(self, pc: List[str]=['PC1', 'PC2', 'PC3'], color_: Optional[pd.Series]=None, color_dict: Optional[dict]=None, symbol_: Optional[pd.Series]=None, symbol_dict: Optional[dict]=None, fig_height: int=900, fig_width: int=1300, marker_size: int=35, marker_opacity: float=0.7, marker_label: Optional[pd.Series]=None, font_size: int=20, title_font_size: int=20, legend_name: List[str]=['Group', 'Time point'])

Source

metbit/analysis/pca.py at v9.0.0
Project downloadsPyPI and GitHub measure different distribution channels. Statistics refresh daily.

Counts are distribution activity, not unique users. GitHub source archives and Git clones are not included. Sources: PyPI Stats, Pepy, ClickPy, and GitHub Releases.

metbit 9.0.0 documentation