|
3 | 3 | from bluecast.preprocessing.remove_collinearity import remove_correlated_columns |
4 | 4 |
|
5 | 5 |
|
6 | | -def test_remove_correlated_columns_high_correlation(): |
7 | | - # Create a DataFrame with high correlation between columns |
| 6 | +def test_remove_correlated_columns_positive_correlation(): |
8 | 7 | data = { |
9 | 8 | "A": [1, 2, 3, 4, 5], |
10 | | - "B": [2, 4, 6, 8, 10], # B is perfectly correlated with A |
11 | | - "C": [5, 4, 3, 2, 1], # C is not correlated with A or B |
| 9 | + "B": [2, 4, 6, 8, 10], # B is perfectly positively correlated with A |
| 10 | + "C": [1, 3, 2, 5, 4], # C has low correlation with A |
12 | 11 | } |
13 | 12 | df = pd.DataFrame(data) |
14 | 13 |
|
15 | 14 | result_df = remove_correlated_columns(df, threshold=0.9) |
16 | 15 |
|
17 | | - # B should be removed because it's highly correlated with A |
18 | | - expected_df = pd.DataFrame({"A": [1, 2, 3, 4, 5], "C": [5, 4, 3, 2, 1]}) |
| 16 | + assert "A" in result_df.columns |
| 17 | + assert "B" not in result_df.columns |
| 18 | + assert "C" in result_df.columns |
19 | 19 |
|
20 | | - pd.testing.assert_frame_equal(result_df, expected_df) |
21 | 20 |
|
22 | | - |
23 | | -def test_remove_correlated_columns_no_removal(): |
24 | | - # Create a DataFrame with no high correlations |
| 21 | +def test_remove_correlated_columns_negative_correlation(): |
25 | 22 | data = { |
26 | 23 | "A": [1, 2, 3, 4, 5], |
27 | | - "B": [2, 3, 4, 5, 6], # B is not perfectly correlated with A |
28 | | - "C": [5, 4, 3, 2, 1], # C is not correlated with A or B |
| 24 | + "B": [5, 4, 3, 2, 1], # B is perfectly negatively correlated with A |
| 25 | + "C": [1, 3, 2, 5, 4], # C has low correlation |
29 | 26 | } |
30 | 27 | df = pd.DataFrame(data) |
31 | 28 |
|
32 | 29 | result_df = remove_correlated_columns(df, threshold=0.9) |
33 | 30 |
|
34 | | - # No columns should be removed |
35 | | - pd.testing.assert_frame_equal(result_df, df) |
| 31 | + assert "A" in result_df.columns |
| 32 | + assert "B" not in result_df.columns, "Negative correlation should also be caught" |
| 33 | + assert "C" in result_df.columns |
36 | 34 |
|
37 | 35 |
|
38 | | -def test_remove_correlated_columns_no_correlation(): |
39 | | - # Create a DataFrame where no columns are correlated above the threshold |
| 36 | +def test_remove_correlated_columns_no_removal(): |
40 | 37 | data = { |
41 | 38 | "A": [1, 2, 3, 4, 5], |
42 | | - "B": [2, 3, 4, 5, 6], |
43 | | - "C": [5, 4, 3, 2, 1], |
44 | | - "D": [1, 2, 1, 2, 1], |
| 39 | + "B": [1, 3, 2, 5, 4], # Low correlation with A |
| 40 | + "C": [3, 1, 4, 2, 5], # Low correlation with A and B |
45 | 41 | } |
46 | 42 | df = pd.DataFrame(data) |
47 | 43 |
|
48 | 44 | result_df = remove_correlated_columns(df, threshold=0.9) |
49 | 45 |
|
50 | | - # Since no columns are correlated above the threshold, the original DataFrame should be returned |
51 | | - pd.testing.assert_frame_equal(result_df, df) |
| 46 | + assert list(result_df.columns) == ["A", "B", "C"] |
52 | 47 |
|
53 | 48 |
|
54 | | -def test_remove_correlated_columns_different_threshold(): |
55 | | - # Create a DataFrame with some correlation |
| 49 | +def test_remove_correlated_columns_does_not_mutate_input(): |
56 | 50 | data = { |
57 | 51 | "A": [1, 2, 3, 4, 5], |
58 | | - "B": [2, 4, 6, 8, 10], # B is perfectly correlated with A |
59 | | - "C": [5, 5, 5, 5, 5], # C is constant, should have no correlation |
| 52 | + "B": [2, 4, 6, 8, 10], |
| 53 | + "C": [1, 3, 2, 5, 4], |
60 | 54 | } |
61 | 55 | df = pd.DataFrame(data) |
| 56 | + original_cols = list(df.columns) |
| 57 | + |
| 58 | + remove_correlated_columns(df, threshold=0.9) |
62 | 59 |
|
63 | | - # Use a higher threshold, so no columns should be removed |
64 | | - result_df = remove_correlated_columns(df, threshold=0.95) |
| 60 | + assert list(df.columns) == original_cols, "Original DataFrame should not be mutated" |
65 | 61 |
|
66 | | - pd.testing.assert_frame_equal(result_df, df) |
67 | 62 |
|
68 | | - # Use a lower threshold, so column B should be removed |
69 | | - result_df = remove_correlated_columns(df, threshold=0.8) |
| 63 | +def test_remove_correlated_columns_different_threshold(): |
| 64 | + data = { |
| 65 | + "A": [1, 2, 3, 4, 5], |
| 66 | + "B": [2, 4, 6, 8, 10], # Perfectly correlated with A |
| 67 | + "C": [1, 3, 2, 5, 4], # Low correlation |
| 68 | + } |
| 69 | + df = pd.DataFrame(data) |
70 | 70 |
|
71 | | - expected_df = pd.DataFrame({"A": [1, 2, 3, 4, 5], "C": [5, 5, 5, 5, 5]}) |
| 71 | + result_high = remove_correlated_columns(df, threshold=1.01) |
| 72 | + assert len(result_high.columns) == 3, "No columns removed at threshold > 1.0" |
72 | 73 |
|
73 | | - pd.testing.assert_frame_equal(result_df, expected_df) |
| 74 | + result_low = remove_correlated_columns(df, threshold=0.8) |
| 75 | + assert "B" not in result_low.columns |
0 commit comments