vastorbit.VastFrame.cdt¶
- VastFrame.cdt(columns: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, max_cardinality: int = 20, nbins: int = 10, tcdt: bool = True, drop_transf_cols: bool = True) VastFrame¶
Returns the complete disjunctive table of the VastFrame. Numerical features are transformed to categorical using the
VastFrame.discretize()method. Applying PCA on TCDT leads to MCA (Multiple correspondence analysis).Warning
This method can become computationally expensive when used with categorical variables with many categories.
- Parameters:
columns (SQLColumns, optional) – List of the VastColumns names.
max_cardinality (int, optional) – For any categorical variable, keeps the most frequent categories and merges the less frequent categories into a new unique category.
nbins (int, optional) – Number of bins used for the discretization (must be > 1).
tcdt (bool, optional) – If set to True, returns the transformed complete disjunctive table (TCDT).
drop_transf_cols (bool, optional) – If set to True, drops the columns used during the transformation.
- Returns:
the CDT relation.
- Return type:
Examples
Let’s begin by importing vastorbit.
import vastorbit as vo
Hint
By assigning an alias to
vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromvastorbitare used as intended without interfering with functions from other libraries.Let us create a
VastFramewith multiple columns:vdf = vo.VastFrame( { "id": [0, 1, 2, 3, 4, 5], "cats": ["A", "B", "C", "A", "B", "C"], "vals": [2, 4, 8, 1, 4, 2], }, )
123idIntegerAbccatsVarchar(1)123valsInteger1 1 B 4 2 3 A 1 3 5 C 2 4 4 B 4 5 2 C 8 6 0 A 2 Rows: 1-6 | Columns: 3We can create the complete disjunctive table of the
VastFrame:vdf.cdt(columns=["cats", "vals"], tcdt = False)
123idInteger123cats_AInteger123cats_BInteger123cats_CInteger123vals_1Integer123vals_2Integer123vals_4Integer123vals_8Integer1 1 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 0 3 3 1 0 0 1 0 0 0 4 5 0 0 1 0 1 0 0 5 2 0 0 1 0 0 0 1 6 4 0 1 0 0 0 1 0 Rows: 6 | Columns: 8Same can be done to create the transformed complete disjunctive table of the
VastFrame:vdf.cdt(columns=["cats", "vals"], tcdt = True)
123idInteger123cats_ADecimal(18, 6)123cats_BDecimal(18, 6)123cats_CDecimal(18, 6)123vals_1Decimal(18, 6)123vals_2Decimal(18, 6)123vals_4Decimal(18, 6)123vals_8Decimal(18, 6)1 2 -1.0 -1.0 -0.5 -1.0 -1.0 -1.0 0.0 2 4 -1.0 -0.5 -1.0 -1.0 -1.0 -0.5 -1.0 3 0 -0.5 -1.0 -1.0 -1.0 -0.5 -1.0 -1.0 4 3 -0.5 -1.0 -1.0 0.0 -1.0 -1.0 -1.0 5 5 -1.0 -1.0 -0.5 -1.0 -0.5 -1.0 -1.0 6 1 -1.0 -0.5 -1.0 -1.0 -1.0 -0.5 -1.0 Rows: 1-6 | Columns: 8Note
This method can be useful to build an MCA (Multiple Correspondence Analysis) model based on a PCA (Principal Component Analysis) one. The transformed complete disjunctive table refers to a table used in MCA, where the original categorical data is transformed into binary indicators to represent the absence or presence of categories.
See also
PCA: Principal Component Analysis.