Package org.uma.jmetal.util
Class KNearestDistanceCalculator
java.lang.Object
org.uma.jmetal.util.KNearestDistanceCalculator
Utility class for computing the distance of each row in a matrix to its K-th nearest neighbor
using Euclidean distance, with optional normalization.
Code created with an AI generative tool
-
Constructor Summary
ConstructorsConstructorDescriptionKNearestDistanceCalculator(int k, boolean normalize) Creates a new KNearestDistanceCalculator. -
Method Summary
Modifier and TypeMethodDescriptiondouble[]compute(double[][] matrix) Computes the distance of each row to its K-th nearest neighbor.
-
Constructor Details
-
KNearestDistanceCalculator
public KNearestDistanceCalculator(int k, boolean normalize) Creates a new KNearestDistanceCalculator.- Parameters:
k- The number of nearest neighbors to consider (must be positive and less than the number of rows - 1)normalize- Whether to normalize the input matrix before computing distances- Throws:
IllegalArgumentException- if k is not positive or if k is greater than or equal to the number of rows - 1
-
-
Method Details
-
compute
public double[] compute(double[][] matrix) Computes the distance of each row to its K-th nearest neighbor.- Parameters:
matrix- The input matrix (n x m) where n is the number of rows and m is the number of features- Returns:
- An array where the i-th element is the distance of the i-th row to its K-th nearest neighbor
- Throws:
IllegalArgumentException- if the input matrix is null, empty, or contains rows of different lengthsIllegalStateException- if k is greater than or equal to the number of rows - 1
-