metbit.metbit
Analysis and models module in metbit 1.7.2.
import metbit.metbitClasses
opls_da
Methods
__init__(self, X, y, features_name=None, n_components=2, scale='pareto', kfold=3, estimator='opls', random_state=42, auto_ncomp=True)
fit(self)
permutation_test(self, n_permutations=500, cv=3, n_jobs=-1, verbose=10)
vip_scores(self, model=None, features_name=None)
Get VIP score
Parameters
modelobject, default=NoneOPLS-DA model.
features_namearray-like, shape (n_features,), default=NoneName of features.
get_vip_scores(self, filter_=False, threshold=1)
Get VIP score
Parameters
filter_bool, default=FalseIf True, filter VIP score based on threshold.
thresholdint, default=1Threshold of VIP score.
vip_plot(self, x_range=9, threshold=2, size=12, width=1000, height=500, filter_=False, vip_trans_form=False)
Plot VIP score
Parameters
x_rangeint, default=9Range of x-axis.
thresholdint, default=2Threshold of VIP score.
sizeint, default=12Size of the marker.
widthint, default=1000Width of the figure.
heightint, default=500Height of the figure.
filter_bool, default=FalseIf True, filter VIP score based on threshold.
plot_oplsda_scores(self, color_=None, color_dict=None, symbol_=None, symbol_dict=None, fig_height=900, fig_width=1300, marker_size=35, marker_opacity=0.7)
Plot OPLS-DA scores plot
Parameters
color_array-like, shape (n_samples,), default=Nonecolor_ of the group. If None, color_ will be based on the group in y.
color_dictdict, default=NoneDictionary of color_ for the group. If None, color_ will be based on the group in y.
symbol_array-like, shape (n_samples,), default=Nonesymbol_ of the group. If None, symbol_ will be based on the group in y.
symbol_dictdict, default=NoneDictionary of symbol_ for the group. If None, symbol_ will be based on the group in y.
fig_heightint, default=900Height of the figure.
fig_widthint, default=1300Width of the figure.
marker_sizeint, default=35Size of the marker.
marker_opacityfloat, default=0.7Opacity of the marker.
plot_hist(self, nbins_=50, height_=500, width_=1000)
Plot histogram of permutation scores
Parameters
nbins_int, default=50Number of bins for histogram.
height_int, default=500Height of the figure.
width_int, default=1000Width of the figure.
plot_s_scores(self, height_=900, width_=2000, range_color_=[-0.05, 0.05], color_continuous_scale_='jet')
Plot S-plot
Parameters
height_int, default=900Height of the figure.
width_int, default=2000Width of the figure.
range_colorlist, default=[-0.05,0.05]Range of color_ for the plot.
color_continuous_scale_str, default='jet'color_ scale for the plot.
plot_loading(self, height_=900, width_=2000, range_color_=[-0.05, 0.05], color_continuous_scale_='jet')
Plot loading plot
Parameters
height_int, default=900Height of the figure.
width_int, default=2000Width of the figure.
range_colorlist, default=[-0.05,0.05]Range of color_ for the plot.
color_continuous_scale_str, default='jet'color_ scale for the plot.
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=NoneName of features.
n_componentsint, default=2Number 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=42Random state for permutation test.
test_sizefloat, default=0.3Size 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, scale='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, label, features_name=None, n_components=2, scale='pareto', random_state=42, test_size=0.3)
fit(self)
get_explained_variance(self)
get_scores(self)
get_loadings(self)
get_q2_test(self)
plot_observe_variance(self)
Visualise explained variance plot
Returns
figplotly.graph_objects.FigureExplained variance plot.
plot_cumulative_observed(self)
plot_pca_scores(self, pc=['PC1', 'PC2'], color_=None, color_dict=None, symbol_=None, symbol_dict=None, fig_height=900, fig_width=1300, marker_size=35, marker_opacity=0.7, text_=None)
Visualise PCA scores plot
Parameters
pclist, default=['PC1', 'PC2']List of principal components to plot.
colorarray-like, shape (n_samples,), default=NoneTarget data, where n_samples is the number of samples.
color_dictdict, default=NoneDictionary of color_ mapping.
symbol_array-like, shape (n_samples,), default=NoneTarget data, where n_samples is the number of samples.
symbol_dictdict, default=NoneDictionary of symbol_ mapping.
fig_heightint, default=900Height of figure.
fig_widthint, default=1300Width of figure.
marker_sizeint, default=35Size of marker.
marker_opacityfloat, default=0.7Opacity of marker.
text_array-like, shape (n_samples,), default=NoneText to display on each point.
Returns
figplotly.graph_objects.FigurePCA scores plot.
plot_loading_(self, pc=['PC1', 'PC2'], height_=600, width_=1800)
Visualise PCA loadings
Parameters
pclist, default=['PC1', 'PC2']Principle component to plot.
height_int, default=600Height of figure.
width_int, default=1800Width of figure.
Returns
figplotly.graph_objects.FigurePlotly figure.
----------
plot_pca_trajectory(self, time_, time_order, stat_=['mean', 'sem'], pc=['PC1', 'PC2'], color_dict=None, symbol_dict=None, height_=900, width_=1300, marker_size=35, marker_opacity=0.7)
Visualise PCA trajectory
Parameters
time_array-like, shape (n_samples,)Time point of samples.
time_orderdictionaryOrder of time point.
stat_list, default=['mean', 'sem']Statistic to calculate. First element is mean or median, second element is sem or std.
pclist, default=['PC1', 'PC2']Principle component to plot.
color_dictdictionary, default=NoneDictionary of color_ for each group.
symbol_dictdictionary, default=NoneDictionary of symbol_ for each time point.
height_int, default=900Height of figure.
width_int, default=1300Width of figure.
marker_sizeint, default=35Size of marker.
marker_opacityfloat, default=0.7Opacity of marker.
Returns
figplotly.graph_objects.FigurePlotly figure.