Package smile.neighbor
Class LinearSearch<K,V>
java.lang.Object
smile.neighbor.LinearSearch<K,V>
- Type Parameters:
K
- the type of keys.V
- the type of associated objects.
- All Implemented Interfaces:
Serializable
,KNNSearch<K,
,V> RNNSearch<K,
V>
public class LinearSearch<K,V>
extends Object
implements KNNSearch<K,V>, RNNSearch<K,V>, Serializable
Brute force linear nearest neighbor search. This simplest solution computes
the distance from the query point to every other point in the database,
keeping track of the "best so far". There are no search data structures to
maintain, so linear search has no space complexity beyond the storage of
the database. Although it is very simple, naive search outperforms space
partitioning approaches (e.g. K-D trees) on higher dimensional spaces.
By default, the query object (reference equality) is excluded from the neighborhood.
Note that you may observe weird behavior with String objects. JVM will pool the string
literal objects. So the below variables
String a = "ABC";
String b = "ABC";
String c = "AB" + "C";
are actually equal in reference test a == b == c
. With toy data that you
type explicitly in the code, this will cause problems. Fortunately, the data would be
read from secondary storage in production.
- See Also:
-
Constructor Summary
ConstructorDescriptionConstructor.Constructor.LinearSearch
(K[] keys, V[] data, Distance<K> distance) Constructor.Constructor. -
Method Summary
Modifier and TypeMethodDescriptionReturns the nearest neighbor.static <T> LinearSearch
<T, T> Return linear nearest neighbor search.static <T> LinearSearch
<T, T> Return linear nearest neighbor search.void
Retrieves the neighbors in a fixed radius of query object, i.e.Retrieves the k nearest neighbors to the query key.toString()
-
Constructor Details
-
LinearSearch
Constructor.- Parameters:
keys
- the data keys.data
- the data objects.distance
- the distance function.
-
LinearSearch
Constructor.- Parameters:
keys
- the data keys.data
- the data objects.distance
- the distance function.
-
LinearSearch
Constructor.- Parameters:
data
- the data objects.distance
- the distance function.key
- the lambda to extra the key from data object.
-
LinearSearch
Constructor.- Parameters:
data
- the data objects.distance
- the distance function.key
- the lambda to extra the key from data object.
-
-
Method Details
-
of
Return linear nearest neighbor search.- Type Parameters:
T
- the type of keys and values.- Parameters:
data
- the data objects, which are also used as key.distance
- the distance function.- Returns:
- Linear nearest neighbor search.
-
of
Return linear nearest neighbor search.- Type Parameters:
T
- the type of keys and values.- Parameters:
data
- the data objects, which are also used as key.distance
- the distance function.- Returns:
- Linear nearest neighbor search.
-
toString
-
nearest
Description copied from interface:KNNSearch
Returns the nearest neighbor. In machine learning, we often build a nearest neighbor search data structure, and then search with object in the same dataset. The object itself is of course the nearest one with distance 0. Since this is generally useless, we check the reference during the search and excludes the query object from the results. -
search
Description copied from interface:KNNSearch
Retrieves the k nearest neighbors to the query key. -
search
Description copied from interface:RNNSearch
Retrieves the neighbors in a fixed radius of query object, i.e.d(q, v) <= radius
.
-