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:
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 fromvastorbitare 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], } )
123categoryInteger123valInteger1 0 12 2 0 12 3 0 12 4 0 12 5 0 9 6 1 14 7 0 10 8 0 10 9 0 15 10 0 14 11 1 16 Rows: 1-11 | Columns: 2Note
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
balancefunction to fix this:balanced_vdf = vdf.balance(column="category", x = 0.5)
123categoryInteger123valInteger1 1 14 2 1 16 3 0 12 4 0 12 5 0 15 6 0 9 7 0 10 Rows: 1-7 | Columns: 2Note
By giving
xvalue 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()