Skip to content

Commit 85f1730

Browse files
committed
Add test for principal_component_analysis
1 parent 7ed93e2 commit 85f1730

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Diff for: machine_learning/dimensionality_reduction.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def column_reshape(input_array: np.ndarray) -> np.ndarray:
2929

3030

3131
def covariance_within_classes(
32-
features: np.ndarray, labels: np.ndarray, classes: int
32+
features: np.ndarray, labels: np.ndarray, classes: int
3333
) -> np.ndarray:
3434
"""Function to compute the covariance matrix inside each class.
3535
>>> features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
@@ -57,7 +57,7 @@ def covariance_within_classes(
5757

5858

5959
def covariance_between_classes(
60-
features: np.ndarray, labels: np.ndarray, classes: int
60+
features: np.ndarray, labels: np.ndarray, classes: int
6161
) -> np.ndarray:
6262
"""Function to compute the covariance matrix between multiple classes
6363
>>> features = np.array([[9, 2, 3], [4, 3, 6], [1, 8, 9]])
@@ -98,11 +98,6 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
9898
Parameters:
9999
* features: the features extracted from the dataset
100100
* dimensions: to filter the projected data for the desired dimension
101-
>>> features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
102-
>>> dimensions = 2
103-
>>> principal_component_analysis(features, dimensions)
104-
array([[ 6.92820323, 8.66025404, 10.39230485],
105-
[ 3. , 3. , 3. ]])
106101
"""
107102

108103
# Check if the features have been loaded
@@ -113,7 +108,6 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
113108
covariance_matrix = np.dot(centered_data, centered_data.T) / features.shape[1]
114109
_, eigenvectors = np.linalg.eigh(covariance_matrix)
115110
# Take all the columns in the reverse order (-1), and then takes only the first
116-
# columns
117111
filtered_eigenvectors = eigenvectors[:, ::-1][:, 0:dimensions]
118112
# Project the database on the new space
119113
projected_data = np.dot(filtered_eigenvectors.T, features)
@@ -127,7 +121,7 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
127121

128122

129123
def linear_discriminant_analysis(
130-
features: np.ndarray, labels: np.ndarray, classes: int, dimensions: int
124+
features: np.ndarray, labels: np.ndarray, classes: int, dimensions: int
131125
) -> np.ndarray:
132126
"""
133127
Linear Discriminant Analysis.
@@ -189,6 +183,14 @@ def test_linear_discriminant_analysis() -> None:
189183
raise AssertionError("Did not raise AssertionError for dimensions > classes")
190184

191185

186+
def test_principal_component_analysis() -> None:
187+
features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
188+
dimensions = 2
189+
expected_output = np.array([[6.92820323, 8.66025404, 10.39230485], [3., 3., 3.]])
190+
output = principal_component_analysis(features, dimensions)
191+
assert np.allclose(expected_output, output), f"Expected {expected_output}, but got {output}"
192+
193+
192194
if __name__ == "__main__":
193195
import doctest
194196

0 commit comments

Comments
 (0)