11
11
import logging
12
12
13
13
import numpy as np
14
+ import pytest
14
15
from scipy .linalg import eigh
15
16
16
17
logging .basicConfig (level = logging .INFO , format = "%(message)s" )
@@ -29,7 +30,7 @@ def column_reshape(input_array: np.ndarray) -> np.ndarray:
29
30
30
31
31
32
def covariance_within_classes (
32
- features : np .ndarray , labels : np .ndarray , classes : int
33
+ features : np .ndarray , labels : np .ndarray , classes : int
33
34
) -> np .ndarray :
34
35
"""Function to compute the covariance matrix inside each class.
35
36
>>> features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
@@ -57,7 +58,7 @@ def covariance_within_classes(
57
58
58
59
59
60
def covariance_between_classes (
60
- features : np .ndarray , labels : np .ndarray , classes : int
61
+ features : np .ndarray , labels : np .ndarray , classes : int
61
62
) -> np .ndarray :
62
63
"""Function to compute the covariance matrix between multiple classes
63
64
>>> features = np.array([[9, 2, 3], [4, 3, 6], [1, 8, 9]])
@@ -98,6 +99,8 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
98
99
Parameters:
99
100
* features: the features extracted from the dataset
100
101
* dimensions: to filter the projected data for the desired dimension
102
+
103
+ >>> test_principal_component_analysis()
101
104
"""
102
105
103
106
# Check if the features have been loaded
@@ -121,7 +124,7 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
121
124
122
125
123
126
def linear_discriminant_analysis (
124
- features : np .ndarray , labels : np .ndarray , classes : int , dimensions : int
127
+ features : np .ndarray , labels : np .ndarray , classes : int , dimensions : int
125
128
) -> np .ndarray :
126
129
"""
127
130
Linear Discriminant Analysis.
@@ -132,6 +135,8 @@ def linear_discriminant_analysis(
132
135
* labels: the class labels of the features
133
136
* classes: the number of classes present in the dataset
134
137
* dimensions: to filter the projected data for the desired dimension
138
+
139
+ >>> test_linear_discriminant_analysis()
135
140
"""
136
141
137
142
# Check if the dimension desired is less than the number of classes
@@ -163,32 +168,26 @@ def test_linear_discriminant_analysis() -> None:
163
168
classes = 2
164
169
dimensions = 2
165
170
166
- projected_data = linear_discriminant_analysis (features , labels , classes , dimensions )
167
-
168
- # Assert that the shape of the projected data is correct
169
- assert projected_data .shape == (dimensions , features .shape [1 ])
170
-
171
- # Assert that the projected data is a numpy array
172
- assert isinstance (projected_data , np .ndarray )
173
-
174
- # Assert that the projected data is not empty
175
- assert projected_data .any ()
176
-
177
171
# Assert that the function raises an AssertionError if dimensions > classes
178
- try :
179
- projected_data = linear_discriminant_analysis (features , labels , classes , 3 )
180
- except AssertionError :
181
- pass
182
- else :
183
- raise AssertionError ("Did not raise AssertionError for dimensions > classes" )
172
+ with pytest .raises (AssertionError ) as error_info :
173
+ projected_data = linear_discriminant_analysis (features , labels , classes , dimensions )
174
+ if isinstance (projected_data , np .ndarray ):
175
+ raise AssertionError (
176
+ "Did not raise AssertionError for dimensions > classes"
177
+ )
178
+ assert error_info .type is AssertionError
184
179
185
180
186
181
def test_principal_component_analysis () -> None :
187
182
features = np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]])
188
183
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 } "
184
+ expected_output = np .array ([[6.92820323 , 8.66025404 , 10.39230485 ], [3.0 , 3.0 , 3.0 ]])
185
+
186
+ with pytest .raises (AssertionError ) as error_info :
187
+ output = principal_component_analysis (features , dimensions )
188
+ if not np .allclose (expected_output , output ):
189
+ raise AssertionError
190
+ assert error_info .type is AssertionError
192
191
193
192
194
193
if __name__ == "__main__" :
0 commit comments