# Manhattan Distance¶

The Manhattan distance( $$L_1$$ distance ) for real valued features is the absolute difference between the components of two data points.

$\sum_{i=0}^{d}|{\bf x_i}-{\bf x'_i}|$

where $$\bf x$$ and $$\bf x'$$ are $$d$$ dimensional feature vectors.

## Example¶

Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as

features_a = RealFeatures(f_feats_a)
features_b = RealFeatures(f_feats_b)

features_a = RealFeatures(f_feats_a);
features_b = RealFeatures(f_feats_b);

RealFeatures features_a = new RealFeatures(f_feats_a);
RealFeatures features_b = new RealFeatures(f_feats_b);

features_a = Shogun::RealFeatures.new f_feats_a
features_b = Shogun::RealFeatures.new f_feats_b

features_a <- RealFeatures(f_feats_a)
features_b <- RealFeatures(f_feats_b)

features_a = shogun.RealFeatures(f_feats_a)
features_b = shogun.RealFeatures(f_feats_b)

RealFeatures features_a = new RealFeatures(f_feats_a);
RealFeatures features_b = new RealFeatures(f_feats_b);

auto features_a = some<CDenseFeatures<float64_t>>(f_feats_a);
auto features_b = some<CDenseFeatures<float64_t>>(f_feats_b);


We create an instance of CManhattanDistance by passing it CDenseFeatures.

distance = ManhattanMetric(features_a, features_a)

distance = ManhattanMetric(features_a, features_a);

ManhattanMetric distance = new ManhattanMetric(features_a, features_a);

distance = Shogun::ManhattanMetric.new features_a, features_a

distance <- ManhattanMetric(features_a, features_a)

distance = shogun.ManhattanMetric(features_a, features_a)

ManhattanMetric distance = new ManhattanMetric(features_a, features_a);

auto distance = some<CManhattanMetric>(features_a, features_a);


The distance matrix can be extracted as follows:

distance_matrix_aa = distance.get_distance_matrix()

distance_matrix_aa = distance.get_distance_matrix();

DoubleMatrix distance_matrix_aa = distance.get_distance_matrix();

distance_matrix_aa = distance.get_distance_matrix

distance_matrix_aa <- distance$get_distance_matrix()  distance_matrix_aa = distance:get_distance_matrix()  double[,] distance_matrix_aa = distance.get_distance_matrix();  auto distance_matrix_aa = distance->get_distance_matrix();  We can use the same instance with new CDenseFeatures to compute asymmetrical distance as follows: distance.init(features_a, features_b) distance_matrix_ab = distance.get_distance_matrix()  distance.init(features_a, features_b); distance_matrix_ab = distance.get_distance_matrix();  distance.init(features_a, features_b); DoubleMatrix distance_matrix_ab = distance.get_distance_matrix();  distance.init features_a, features_b distance_matrix_ab = distance.get_distance_matrix  distance$init(features_a, features_b)
distance_matrix_ab <- distance\$get_distance_matrix()

distance:init(features_a, features_b)
distance_matrix_ab = distance:get_distance_matrix()

distance.init(features_a, features_b);
double[,] distance_matrix_ab = distance.get_distance_matrix();

distance->init(features_a, features_b);
auto distance_matrix_ab = distance->get_distance_matrix();


## References¶

Wikipedia: Manhattan_distance

Wikipedia: L1_distance