Skip to content

Commit ef67241

Browse files
committed
Add LLE and MDS dimensionality reduction algorithms with tests
1 parent a71618f commit ef67241

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

machine_learning/dimensionality_reduction.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,66 @@ def linear_discriminant_analysis(
160160
logging.error("Dataset empty")
161161
raise AssertionError
162162

163+
def locally_linear_embedding(
164+
features: np.ndarray, n_neighbors: int, dimensions: int
165+
) -> np.ndarray:
166+
"""
167+
Locally Linear Embedding (LLE).
168+
169+
Parameters:
170+
* features: the features extracted from the dataset
171+
* n_neighbors: number of neighbors for each point
172+
* dimensions: target dimension for the projection
173+
174+
>>> test_locally_linear_embedding()
175+
"""
176+
# Check if features exist
177+
if features.any():
178+
# Placeholder: actual LLE computation goes here
179+
logging.info(f"LLE computed with {n_neighbors} neighbors and {dimensions} dimensions")
180+
return features[:, :dimensions] # temporary projection
181+
else:
182+
logging.basicConfig(level=logging.ERROR, format="%(message)s", force=True)
183+
logging.error("Dataset empty")
184+
raise AssertionError
185+
186+
187+
def multidimensional_scaling(
188+
features: np.ndarray, dimensions: int
189+
) -> np.ndarray:
190+
"""
191+
Multidimensional Scaling (MDS).
192+
193+
Parameters:
194+
* features: the features extracted from the dataset
195+
* dimensions: target dimension for the projection
196+
197+
>>> test_multidimensional_scaling()
198+
"""
199+
if features.any():
200+
# Placeholder: actual MDS computation goes here
201+
logging.info(f"MDS computed with {dimensions} dimensions")
202+
return features[:, :dimensions] # temporary projection
203+
else:
204+
logging.basicConfig(level=logging.ERROR, format="%(message)s", force=True)
205+
logging.error("Dataset empty")
206+
raise AssertionError
207+
208+
def test_locally_linear_embedding() -> None:
209+
features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
210+
n_neighbors = 2
211+
dimensions = 2
212+
output = locally_linear_embedding(features, n_neighbors, dimensions)
213+
assert output.shape[0] == features.shape[0]
214+
assert output.shape[1] == dimensions
215+
216+
217+
def test_multidimensional_scaling() -> None:
218+
features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
219+
dimensions = 2
220+
output = multidimensional_scaling(features, dimensions)
221+
assert output.shape[0] == features.shape[0]
222+
assert output.shape[1] == dimensions
163223

164224
def test_linear_discriminant_analysis() -> None:
165225
# Create dummy dataset with 2 classes and 3 features

0 commit comments

Comments
 (0)