PlnMixture
The PlnMixture model clusters count data. Clusters can be accessed through the .clusters attribute after fitting the model (.fit() method). The use of covariates is possible, but the regression coefficient is shared among all clusters. The performance may decrease significantly with the number of covariates. Note that the number of clusters is a hyperparameter that needs to be set by the user.
PlnMixture Documentation
- class pyPLNmodels.PlnMixture(endog, n_cluster, *, exog=None, add_const=False, offsets=None, compute_offsets_method='zero')[source]
Pln mixture models, that is a gaussian mixture model with Poisson layer on top of it. The effect of covariates is shared with clusters. Note that stability may significantly decrease with the number of covariates.
Examples
>>> import seaborn as sns >>> from pyPLNmodels import PlnMixture, load_scrna, plot_confusion_matrix >>> data = load_scrna() >>> mixture = PlnMixture(data["endog"],n_cluster = 3) >>> mixture.fit() >>> print(mixture) >>> plot_confusion_matrix(mixture.clusters, data["labels"])
See also
pyPLNmodels.PlnDiag()
pyPLNmodels.Pln()
pyPLNmodels.PlnMixture.__init__()
pyPLNmodels.PlnMixture.from_formula()
:func:`pyPLNmodels.PlnMixture.predict- Parameters:
endog (Tensor | ndarray | DataFrame)
n_cluster (int)
exog (Tensor | ndarray | DataFrame | None)
add_const (bool)
offsets (Tensor | ndarray | DataFrame | None)
compute_offsets_method ({'logsum', 'zero'})
- __init__(endog, n_cluster, *, exog=None, add_const=False, offsets=None, compute_offsets_method='zero')[source]
Initializes the model class.
- Parameters:
endog (Union[torch.Tensor, np.ndarray, pd.DataFrame]) – The count data.
exog (Union[torch.Tensor, np.ndarray, pd.DataFrame], optional(keyword-only)) – The covariate data. Defaults to None.
offsets (Union[torch.Tensor, np.ndarray, pd.DataFrame], optional(keyword-only)) – The offsets data. Defaults to None.
compute_offsets_method (str, optional(keyword-only)) –
- Method to compute offsets if not provided. Options are:
”zero” that will set the offsets to zero.
”logsum” that will take the logarithm of the sum (per line) of the counts.
Overridden (useless) if offsets is not None.
add_const (bool, optional(keyword-only)) – Whether to add a column of one in the exog. Defaults to True.
n_cluster (int) – The number of clusters in the model.
- Return type:
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture(data["endog"],n_cluster = 3) >>> mixture.fit() >>> print(mixture)
Notes
The add_const keyword is useless here and ignored. Adding an intercept in the covariates results in non-identifiable coefficients for the mixture model.
- classmethod from_formula(formula, data, n_cluster, *, compute_offsets_method='zero')[source]
Create an instance from a formula and data.
- Parameters:
formula (str) – The formula.
data (dict) – The data dictionary. Each value can be either a torch.Tensor, np.ndarray, pd.DataFrame or pd.Series. The categorical exogenous variables should be 1-dimensional.
compute_offsets_method (str, optional(keyword-only)) –
- Method to compute offsets if not provided. Options are:
”zero” that will set the offsets to zero.
”logsum” that will take the logarithm of the sum (per line) of the counts.
Overridden (useless) if data[“offsets”] is not None.
n_cluster (int) – The number of clusters in the model.
- Return type:
See also
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0",data, n_cluster = 3) >>> mixture.fit() >>> print(mixture)
- fit(*, maxiter=400, lr=0.01, tol=1e-06, verbose=False)[source]
Fit the model using variational inference. The lower the tol (tolerance), the more accurate the model.
- Parameters:
maxiter (int, optional) – The maximum number of iterations to be done. Defaults to 400.
lr (float, optional(keyword-only)) – The learning rate. Defaults to 0.01.
tol (float, optional(keyword-only)) – The tolerance for convergence. Defaults to 1e-6.
verbose (bool, optional(keyword-only)) – Whether to print training progress. Defaults to False.
- Raises:
ValueError – If maxiter is not an int.
- Return type:
PlnMixture object
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data, n_cluster = 3) >>> mixture.fit() >>> print(mixture)
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data, n_cluster = 3) >>> mixture.fit(maxiter=500, verbose=True) >>> print(mixture)
- property covariances
Tensor of covariances of shape (n_cluster, dim). Each vector corresponds to the (diagonal) covariance of each cluster.
- property weights
Probability of a sample to belong to each cluster. Tensor of size (n_cluster).
- viz(*, ax=None, colors=None, show_cov=False, remove_exog_effect=False)[source]
Visualize the latent variables. One can remove the effect of exogenous variables with the remove_exog_effect boolean variable.
- Parameters:
ax (matplotlib.axes.Axes, optional) – The axes on which to plot, by default None.
colors (list, optional) – The labels to color the samples, of size n_samples.
show_cov (bool, optional) – Whether to show covariances, by default False.
remove_exog_effect (bool, optional) – Whether to remove or not the effect of exogenous variables. Default to False.
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data=data, n_cluster = 3) >>> mixture.fit() >>> mixture.viz() >>> mixture.viz(colors=data["labels"]) >>> mixture.viz(show_cov=True) >>> mixture.viz(remove_exog_effect=True, colors=data["labels"])
- property covariance
The GMM does not have a single covariance. It has multiple covariances, one per cluster. You may call .covariances.
- biplot(column_names, *, column_index=None, colors=None, title='')[source]
Visualizes variables using the correlation circle along with the pca transformed samples. If the endog has been given as a pd.DataFrame, the column_names have been stored and may be indicated with the column_names argument. Else, one should provide the indices of variables.
- Parameters:
column_names (List[str]) – A list of variable names to visualize. If column_index is None, the variables plotted are the ones in column_names. If column_index is not None, this only serves as a legend. Check the attribute column_names_endog.
column_index (Optional[List[int]], optional keyword-only) – A list of indices corresponding to the variables that should be plotted. If None, the indices are determined based on column_names_endog given the column_names, by default None. If not None, should have the same length as column_names.
title (str optional, keyword-only) – An additional title for the plot.
colors (list, optional, keyword-only) – The labels to color the samples, of size n_samples.
- Raises:
ValueError – If column_index is None and column_names_endog is not set, that has been set if the model has been initialized with a pd.DataFrame as endog.
ValueError – If the length of column_index is different from the length of column_names.
Examples
Notes
- property clusters
The predicted clusters of each sample.
- property dict_latent_parameters
The latent parameters of the model.
- property dict_model_parameters
The parameters of the model.
- property latent_positions
The (conditional) mean of the latent variables with the effect of covariates removed.
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data, n_cluster = 3) >>> mixture.fit() >>> print("Shape latent positions: ", mixture.latent_positions.shape) >>> mixture.viz(remove_exog_effect=True) # Visualize the latent positions
- property latent_variables
The (conditional) mean of the latent variables. This is the best approximation of latent variables. This variable is supposed to be more meaningful than the counts (endog).
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data, n_cluster = 3) >>> mixture.fit() >>> print("Shape latent variables: ", mixture.latent_variables.shape) >>> mixture.viz() # Visualize the latent variables
- property latent_means
Tensor of latent_means of shape (n_cluster, n_samples, dim). Each vector corresponds to the latent_mean of each cluster.
- property latent_sqrt_variances
Tensor of latent_sqrt_variances of shape (n_cluster, n_samples, dim). Each vector corresponds to the latent_sqrt_variance of each cluster.
- property latent_prob
Latent probability that sample i corresponds to cluster k. Returns a torch.Tensor of size (n_samples, n_cluster).
- property list_of_parameters_needing_gradient
The list of all the parameters of the model that needs to be updated at each iteration.
- property number_of_parameters
Number of parameters.
- pca_pairplot(n_components=3, colors=None)[source]
Generates a scatter matrix plot based on Principal Component Analysis (PCA) on the latent variables.
- Parameters:
(int (n_components) – Defaults to 3. It Cannot be greater than 6.
optional) (The number of components to consider for plotting.) – Defaults to 3. It Cannot be greater than 6.
(np.ndarray) (colors) – sample in the endog property of the object. Defaults to the inferred clusters.
n_components (bool)
- Raises:
ValueError – If the number of components requested is greater: than the number of variables in the dataset.
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data=data, n_cluster = 3) >>> mixture.fit() >>> mixture.pca_pairplot(n_components=5) >>> mixture.pca_pairplot(n_components=5, colors=data["labels"])
- plot_correlation_circle(column_names, column_index=None, title='')[source]
Visualizes variables using PCA and plots a correlation circle. If the endog has been given as a pd.DataFrame, the column_names have been stored and may be indicated with the column_names argument. Else, one should provide the indices of variables.
- Parameters:
column_names (List[str]) – A list of variable names to visualize. If column_index is None, the variables plotted are the ones in column_names. If column_index is not None, this only serves as a legend. Check the attribute column_names_endog.
column_index (Optional[List[int]], optional) – A list of indices corresponding to the variables that should be plotted. If None, the indices are determined based on column_names_endog given the column_names, by default None. If not None, should have the same length as column_names.
title (str) – An additional title for the plot.
- Raises:
ValueError – If column_index is None and column_names_endog is not set, that has been set if the model has been initialized with a pd.DataFrame as endog.
ValueError – If the length of column_index is different from the length of column_names.
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture.from_formula("endog ~ 0", data=data, n_cluster = 3) >>> mixture.fit() >>> mixture.plot_correlation_circle(column_names=["MALAT1", "ACTB"]) >>> mixture.plot_correlation_circle(column_names=["A", "B"], column_index=[0, 4])
- property n_cluster
Number of clusters in the model.
- property cluster_bias
The mean that is associated to each cluster, of size (n_cluster, dim). This does not encompass the mean that does not depend on each cluster. Each vector cluster_bias[k] is the bias associated to cluster k.
- predict_clusters(endog, *, exog=None, offsets=None)[source]
Predict the clusters of the given endog and exog. The dimensions of endog, exog, and offsets should match the ones given in the model.
- Parameters:
endog (Union[torch.Tensor, np.ndarray, pd.DataFrame]) – The count data.
exog (Union[torch.Tensor, np.ndarray, pd.DataFrame], optional(keyword-only)) – The covariate data. Defaults to None.
offsets (Union[torch.Tensor, np.ndarray, pd.DataFrame], optional(keyword-only)) – The offsets data. Defaults to None.
- Raises:
ValueError – If the endog (or exog) has wrong shape compared to the previously fitted endog (or exog) variables.
- Returns:
list
- Return type:
The predicted clusters
Examples
>>> from pyPLNmodels import PlnMixture, load_scrna >>> data = load_scrna() >>> mixture = PlnMixture(data["endog"], n_cluster = 3).fit() >>> pred = mixture.predict_clusters(data["endog"]) >>> print('pred', pred)
- property entropy
Entropy of the latent variables.
- property WCSS
Compute the Within-Cluster Sum of Squares on the latent positions.
The higher the better, but increasing n_cluster can only increase the metric. A trade-off (with the elbow method for example) must be applied.
Returns positive float.
- property silhouette
Compute the silhouette score on the latent_positions. See scikit-learn.metrics.silhouette_score for more information.
The higher the better.
Returns float between -1 and 1.
- property AIC
Akaike Information Criterion (AIC).
- property BIC
Bayesian Information Criterion (BIC) of the model.
- property ICL
Integrated Completed Likelihood criterion.
- property coef
Property representing the regression coefficients of size (nb_cov, dim). If no exogenous (exog) is available, returns None.
- Returns:
The coefficients or None if no coefficients are given in the model.
- Return type:
torch.Tensor or None
- property dim
Number of dimensions (i.e. variables) of the dataset.
- property elbo
Returns the last elbo computed.
- property endog
Property representing the endogenous variables (counts).
- Returns:
The endogenous variables.
- Return type:
torch.Tensor
- property exog
Property representing the exogenous variables (covariates).
- Returns:
The exogenous variables or None if no covariates are given in the model.
- Return type:
torch.Tensor or None
- property latent_mean
Property representing the latent mean conditionally on the observed counts, i.e. the conditional mean of the latent variable of each sample.
- Returns:
The latent mean.
- Return type:
torch.Tensor
- property latent_parameters
Alias for dict_latent_parameters.
- property latent_sqrt_variance
Property representing the latent square root variance conditionally on the observed counts, i.e. the square root variance of the latent variable of each sample.
- Returns:
The square root of the latent variance.
- Return type:
torch.Tensor
- property latent_variance
Property representing the latent variance conditionally on the observed counts, i.e. the conditional variance of the latent variable of each sample.
- property loglike
Alias for elbo.
- property marginal_mean
The marginal mean of the model, i.e. the mean of the gaussian latent variable.
- property model_parameters
Alias for dict_model_parameters.
- property n_samples
Number of samples in the dataset.
- property nb_cov: int
The number of exogenous variables.
- property offsets
Property representing the offsets.
- Returns:
The offsets.
- Return type:
torch.Tensor
- property optim_details
Property representing the optimization details.
- Returns:
The dictionary of optimization details.
- Return type:
dict
- plot_expected_vs_true(ax=None, colors=None)
Plot the predicted value of the endog against the endog.
- Parameters:
ax (Optional[matplotlib.axes.Axes], optional) – The matplotlib axis to use. If None, the current axis is used, by default None.
colors (Optional[Any], optional) – The labels to color the samples, of size n_samples. By default None (no colors).
- Returns:
The matplotlib axis.
- Return type:
matplotlib.axes.Axes
- property precision
Property representing the precision of the model, that is the inverse covariance matrix.
- Returns:
The precision matrix of size (dim, dim).
- Return type:
torch.Tensor
- predict(array_like=None)
- projected_latent_variables(rank=2, remove_exog_effect=False)
Perform PCA on latent variables and return the projected variables.
- Parameters:
rank (int, optional) – The number of principal components to compute, by default 2.
remove_exog_effect (bool, optional) – Whether to remove or not the effect of exogenous variables. Default to False.
- Returns:
The projected variables.
- Return type:
numpy.ndarray
- remove_zero_columns = True
- show(savefig=False, name_file='', figsize=(10, 10))
Display the model parameters, norm evolution of the parameters and the criterion.
- Parameters:
savefig (bool, optional) – If True, the figure will be saved to a file. Default is False.
name_file (str, optional) – The name of the file to save the figure. Only used if savefig is True. Default is an empty string.
figsize (tuple of two positive floats.) – Size of the figure that will be created. By default (10,10)
- transform(remove_exog_effect=False)
Returns the latent variables. Can be seen as a normalization of the counts given.
- Parameters:
remove_exog_effect (bool (optional)) – Whether to remove or not the mean induced by the exogenous variables. Default is False.
- Returns:
The transformed endogenous variables (latent variables of the model).
- Return type:
torch.Tensor
- optim: torch.optim.Optimizer
List of methods and attributes
Public Data Attributes:
|
Tensor of covariances of shape (n_cluster, dim). |
|
Probability of a sample to belong to each cluster. |
|
The GMM does not have a single covariance. |
|
The predicted clusters of each sample. |
|
The latent parameters of the model. |
|
The parameters of the model. |
|
The (conditional) mean of the latent variables with the effect of covariates removed. |
|
The (conditional) mean of the latent variables. |
|
Tensor of latent_means of shape (n_cluster, n_samples, dim). |
|
Tensor of latent_sqrt_variances of shape (n_cluster, n_samples, dim). |
|
Latent probability that sample i corresponds to cluster k. |
|
The list of all the parameters of the model that needs to be updated at each iteration. |
|
Number of parameters. |
|
Number of clusters in the model. |
|
The mean that is associated to each cluster, of size (n_cluster, dim). |
|
Entropy of the latent variables. |
|
Compute the Within-Cluster Sum of Squares on the latent positions. |
|
Compute the silhouette score on the latent_positions. |
|
Inherited from BaseModel
|
|
|
The list of all the parameters of the model that needs to be updated at each iteration. |
|
The parameters of the model. |
|
Alias for dict_model_parameters. |
|
The latent parameters of the model. |
|
Alias for dict_latent_parameters. |
|
Number of samples in the dataset. |
|
Number of dimensions (i.e. variables) of the dataset. |
|
Property representing the endogenous variables (counts). |
|
Property representing the exogenous variables (covariates). |
|
The number of exogenous variables. |
|
Property representing the offsets. |
|
Property representing the latent mean conditionally on the observed counts, i.e. the conditional mean of the latent variable of each sample. |
|
Property representing the latent variance conditionally on the observed counts, i.e. the conditional variance of the latent variable of each sample. |
|
Property representing the latent square root variance conditionally on the observed counts, i.e. the square root variance of the latent variable of each sample. |
|
Property representing the regression coefficients of size (nb_cov, dim). |
|
Property representing the covariance of the model. |
|
Property representing the precision of the model, that is the inverse covariance matrix. |
|
The marginal mean of the model, i.e. the mean of the gaussian latent variable. |
|
The (conditional) mean of the latent variables. |
|
The (conditional) mean of the latent variables with the effect of covariates removed. |
|
Returns the last elbo computed. |
|
Alias for elbo. |
|
Bayesian Information Criterion (BIC) of the model. |
|
Integrated Completed Likelihood criterion. |
|
Akaike Information Criterion (AIC). |
|
Returns the number of parameters of the model. |
|
Entropy of the latent variables. |
|
Property representing the optimization details. |
|
Public Methods:
|
Initializes the model class. |
|
Create an instance from a formula and data. |
|
Fit the model using variational inference. |
|
Visualize the latent variables. |
|
Visualizes variables using the correlation circle along with the pca transformed samples. |
|
Compute the elbo of the current parameters. |
|
Generates a scatter matrix plot based on Principal Component Analysis (PCA) on the latent variables. |
|
Visualizes variables using PCA and plots a correlation circle. |
|
Predict the clusters of the given endog and exog. |
|
Covariance of the model. |
Inherited from BaseModel
|
Initializes the model class. |
|
Create an instance from a formula and data. |
|
Fit the model using variational inference. |
|
Display the model parameters, norm evolution of the parameters and the criterion. |
|
Visualizes variables using PCA and plots a correlation circle. |
|
Visualizes variables using the correlation circle along with the pca transformed samples. |
|
Compute the elbo of the current parameters. |
|
Perform PCA on latent variables and return the projected variables. |
|
Returns the latent variables. |
|
Visualize the latent variables. |
|
Generate the string representation of the model. |
|
|
|
Covariance of the model. |
|
Generates a scatter matrix plot based on Principal Component Analysis (PCA) on the latent variables. |
|
Plot the predicted value of the endog against the endog. |
Private Data Attributes:
|
|
|
|
|
Description of the model. |
|
Abstract method the predict the endog variables. |
|
|
|
|
|
The attributes that are specific to this model. |
|
The methods that are specific to this model. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Inherited from BaseModel
|
|
|
Description of the model. |
|
|
|
|
|
|
|
|
|
|
|
|
|
The attributes that are specific to this model. |
|
The methods that are specific to this model. |
|
Property representing the dictionary for printing. |
|
Abstract method the predict the endog variables. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Inherited from ABC
|
Private Methods:
|
|
|
Everything is done in the _init_parameters method. |
|
Everything is done in the _init_parameters method. |
|
Update some parameters. |
|
Computes the covariance when the latent variables are embedded in a lower dimensional space (often 2) with sklearn_components. |
Inherited from BaseModel
|
|
|
Compute the elbo and do a gradient step. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Initialization of model parameters. |
|
Initialization of latent parameters. |
|
Move parameters to the GPU device if present. |
|
|
|
|
|
Project some parameters such as probabilities. |
|
Update some parameters. |
|
|
|
Print the training statistics. |
|
Perform PCA on latent variables and return the projected variables along with their covariances in the two dimensional space. |
|
Computes the covariance when the latent variables are embedded in a lower dimensional space (often 2) with sklearn_components. |