Skip to content

Commit c95f526

Browse files
0.27.6
1 parent 1369bec commit c95f526

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.27.5"
10+
version = "0.27.6"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/utils/stats.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,26 @@ def preprocess_df_for_ols(df, independent_var_columns, target_col) -> tuple:
784784
raise ValueError(f"Mismatch in rows: predictors (X_encoded) have {X_encoded.shape[0]} rows, " f"but target (y) has {y.shape[0]} rows.")
785785

786786
return X_encoded, y
787+
788+
789+
def get_combinations(z_ind: list) -> list:
790+
"""
791+
Generates all possible combinations of two targets from a list of target indices. Order is not important.
792+
793+
Args:
794+
z_ind (list): A list of target indices.
795+
796+
Returns:
797+
list: A list of tuples, where each tuple contains a combination of two target indices.
798+
The order of the targets within a tuple is not important, and each combination
799+
appears only once.
800+
801+
Examples:
802+
>>> from spotpython.utils.stats import get_combinations
803+
>>> z_ind = [0, 1, 2, 30]
804+
>>> combinations = get_combinations(z_ind)
805+
>>> print(combinations)
806+
[(0, 1), (0, 2), (0, 30), (1, 2), (1, 30), (2, 30)]
807+
"""
808+
combinations = [(z_ind[i], z_ind[j]) for i in range(len(z_ind)) for j in range(i + 1, len(z_ind))]
809+
return combinations

test/test_get_combinations.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
from spotpython.utils.stats import get_combinations
3+
4+
def test_get_combinations_empty_list():
5+
"""Test get_combinations with an empty list."""
6+
assert get_combinations([]) == []
7+
8+
def test_get_combinations_single_element():
9+
"""Test get_combinations with a single element."""
10+
assert get_combinations([0]) == []
11+
12+
def test_get_combinations_two_elements():
13+
"""Test get_combinations with two elements."""
14+
assert get_combinations([0, 1]) == [(0, 1)]
15+
16+
def test_get_combinations_multiple_elements():
17+
"""Test get_combinations with multiple elements."""
18+
z_ind = [0, 1, 2, 3]
19+
expected = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
20+
assert get_combinations(z_ind) == expected
21+
22+
def test_get_combinations_non_sequential_indices():
23+
"""Test get_combinations with non-sequential indices."""
24+
z_ind = [10, 20, 30]
25+
expected = [(10, 20), (10, 30), (20, 30)] # Indices are based on values, not indices
26+
assert get_combinations(z_ind) == expected

0 commit comments

Comments
 (0)