FeatureDistance[example1,example2,extractor]
gives the distance between example1 and example2 in the feature space defined by extractor.
FeatureDistance[extractor]
represents an operator form of FeatureDistance that can be applied to a pair of examples.
FeatureDistance
FeatureDistance[example1,example2,extractor]
gives the distance between example1 and example2 in the feature space defined by extractor.
FeatureDistance[extractor]
represents an operator form of FeatureDistance that can be applied to a pair of examples.
Details and Options
- FeatureDistance can be used on many types of data, including numerical, textual, sounds, and images, as well as combinations of these.
- The extractor is typically a FeatureExtractorFunction object as generated by FeatureExtraction.
- FeatureDistance[extractor][ex1,ex2] is equivalent to FeatureDistance[ex1,ex2,extractor].
- The following options can be given:
-
DistanceFunction Automatic the distance to use in the feature space - The option DistanceFunction can be any distance or dissimilarity function or a function f defining a distance between two values.
- For numeric feature vectors, the default setting is DistanceFunctionEuclideanDistance.
Examples
open all close allBasic Examples (1)
Train a FeatureExtractorFunction on a simple dataset of pairs to use with FeatureDistance:
fe = FeatureExtraction[{{1.4, "A"}, {1.5, "A"}, {2.3, "B"}, {5.4, "B"}}]Compute the distance between two pairs using the trained feature extractor:
FeatureDistance[{1.8, "A"}, {4.5, "B"}, fe]Scope (1)
Train a FeatureDistance function on a list of colors:
list = ColorData["AlpineColors"] /@ Range[0, 1, 0.1]fe = FeatureExtraction[list]fd = FeatureDistance[fe]Compute the distance between new colors using the trained distance function:
fd[Yellow, Orange]Find the color closest to Red under this distance metric:
Nearest[list, Red, 1, DistanceFunction -> fd]Options (1)
DistanceFunction (1)
Train a feature extractor to compute term frequency-inverse document frequency vectors from texts:
fe = FeatureExtraction[{"the cat is grey", "my cat is fast", "this dog is scary", "the big dog"}, "TFIDF"]Construct a feature distance for this feature extractor:
fd1 = FeatureDistance[fe]fd1["the cat is grey", "the big dog"]The distance function EuclideanDistance has been chosen. Use the option DistanceFunction to chose CosineDistance instead:
fd2 = FeatureDistance[fe, DistanceFunction -> CosineDistance]fd2["the cat is grey", "the big dog"]Applications (1)
Construct a dataset of dog images:
dataset = {[image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image], [image]};Train an extractor function from this dataset:
fe = FeatureExtraction[dataset]Compute the distance in the feature space between two images using FeatureDistance:
FeatureDistance[[image], [image], fe]Compare to EuclideanDistance between the extracted features:
EuclideanDistance@@fe[{[image], [image]}]Generate an operator form of FeatureDistance:
fd = FeatureDistance[fe]This operator form can be used to compute distances:
fd[[image], [image]]Compute the distance matrix of the dataset using this feature distance:
dm = DistanceMatrix[dataset, DistanceFunction -> fd];dm // MatrixPlotFind the dog that maximizes its distance from the others in the feature space:
dataset[[Ordering[Mean[dm], -1]]]Properties & Relations (1)
FeatureNearest can be used to find close examples in feature space:
lines = ExampleData[{"Text", "OriginOfSpecies"}, "Lines"];
fe = FeatureExtraction[ExampleData[{"Text", "OriginOfSpecies"}, "Lines"], {"LowerCasedText", "TFIDF", "DimensionReducedVector"}];
FeatureNearest[ExampleData[{"Text", "OriginOfSpecies"}, "Lines"],
"domesticated pigeons", FeatureExtractor -> fe]//Short//AbsoluteTimingThis is usually faster than using Nearest with custom DistanceFunction:
fd = FeatureDistance[fe];
Nearest[lines, "domesticated pigeons", 1, DistanceFunction -> fd]//Short//AbsoluteTimingNeat Examples (1)
Generate a NearestNeighborGraph of handwritten digits:
mnist = ExampleData[{"MachineLearning", "MNIST"}, "Data"];
fe = FeatureExtraction[Keys[mnist]];
fd = FeatureDistance[fe]Sample a number of different digits:
digits = Catenate@GroupBy[mnist, Values -> Keys, RandomSample[#, 2]&]NearestNeighborGraph[digits, DistanceFunction -> fd, VertexLabels -> Automatic]Related Guides
History
Text
Wolfram Research (2016), FeatureDistance, Wolfram Language function, https://reference.wolfram.com/language/ref/FeatureDistance.html.
CMS
Wolfram Language. 2016. "FeatureDistance." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FeatureDistance.html.
APA
Wolfram Language. (2016). FeatureDistance. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FeatureDistance.html
BibTeX
@misc{reference.wolfram_2026_featuredistance, author="Wolfram Research", title="{FeatureDistance}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/FeatureDistance.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_featuredistance, organization={Wolfram Research}, title={FeatureDistance}, year={2016}, url={https://reference.wolfram.com/language/ref/FeatureDistance.html}, note=[Accessed: 13-June-2026]}