@@ -29,7 +29,7 @@ def column_reshape(input_array: np.ndarray) -> np.ndarray:
29
29
30
30
31
31
def covariance_within_classes (
32
- features : np .ndarray , labels : np .ndarray , classes : int
32
+ features : np .ndarray , labels : np .ndarray , classes : int
33
33
) -> np .ndarray :
34
34
"""Function to compute the covariance matrix inside each class.
35
35
>>> features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
@@ -57,7 +57,7 @@ def covariance_within_classes(
57
57
58
58
59
59
def covariance_between_classes (
60
- features : np .ndarray , labels : np .ndarray , classes : int
60
+ features : np .ndarray , labels : np .ndarray , classes : int
61
61
) -> np .ndarray :
62
62
"""Function to compute the covariance matrix between multiple classes
63
63
>>> 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
98
98
Parameters:
99
99
* features: the features extracted from the dataset
100
100
* 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. ]])
106
101
"""
107
102
108
103
# Check if the features have been loaded
@@ -113,7 +108,6 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
113
108
covariance_matrix = np .dot (centered_data , centered_data .T ) / features .shape [1 ]
114
109
_ , eigenvectors = np .linalg .eigh (covariance_matrix )
115
110
# Take all the columns in the reverse order (-1), and then takes only the first
116
- # columns
117
111
filtered_eigenvectors = eigenvectors [:, ::- 1 ][:, 0 :dimensions ]
118
112
# Project the database on the new space
119
113
projected_data = np .dot (filtered_eigenvectors .T , features )
@@ -127,7 +121,7 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
127
121
128
122
129
123
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
131
125
) -> np .ndarray :
132
126
"""
133
127
Linear Discriminant Analysis.
@@ -189,6 +183,14 @@ def test_linear_discriminant_analysis() -> None:
189
183
raise AssertionError ("Did not raise AssertionError for dimensions > classes" )
190
184
191
185
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
+
192
194
if __name__ == "__main__" :
193
195
import doctest
194
196
0 commit comments