Loading...

vastorbit.VastFrame.balance

VastFrame.balance(column: str, method: Literal['over', 'under'] = 'under', x: float = 0.5, order_by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None) VastFrame

Balances the dataset using the input method.

Warning

If the data is not sorted, the generated SQL code may differ between attempts.

Parameters:
  • column (str) – Column used to compute the different categories.

  • method (str, optional) –

    The method with which to sample the data.

    • over:

      Oversampling.

    • under:

      Undersampling.

  • x (float, optional) – The desired ratio between the majority class and minority classes.

  • order_by (SQLColumns, optional) – VastColumns used to sort the data.

Returns:

balanced VastFrame

Return type:

VastFrame

Examples

We import 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 from vastorbit are used as intended without interfering with functions from other libraries.

For this example, we will create a toy imbalanced dataset:

vdf = vo.VastFrame(
    {
        "category" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
        "val": [12, 12, 14, 15, 10, 9, 10, 12, 12, 14, 16],
    }
)
123
category
Integer
123
val
Integer
1012
2012
3012
4012
509
6114
7010
8010
9015
10014
11116
Rows: 1-11 | Columns: 2

Note

vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.

In the above data, we can see that there are many more 0s than 1s in the category column. We can conveniently plot the historgram to visualize the skewness:

vdf["category"].hist()

Now we can use the balance function to fix this:

balanced_vdf = vdf.balance(column="category", x = 0.5)
123
category
Integer
123
val
Integer
1114
2116
3012
4012
5015
609
7010
Rows: 1-7 | Columns: 2

Note

By giving x value of 0.5, we have ensured that the ratio between the two classes is not more skewed than this.

Let’s visualize the distribution after the balancing.

balanced_vdf["category"].hist()

See also

VastFrame.balance() : Balances the VastFrame.
VastFrame.between() : Filters between two conditions.