# Cosine Distance¶

The Cosine distance for real valued features x and x’ is the similarity as measured by their angle.

\[1-\frac{{\bf x^\top x'}}{\Vert \bf{x}\Vert_2 \Vert \bf{x'}\Vert_2 }\]

where where \(\Vert \cdot\Vert_2\) is the Euclidean norm.

## 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 CCosineDistance by passing it CDenseFeatures.

```
distance = CosineDistance(features_a, features_a)
```

```
distance = CosineDistance(features_a, features_a);
```

```
CosineDistance distance = new CosineDistance(features_a, features_a);
```

```
distance = Shogun::CosineDistance.new features_a, features_a
```

```
distance <- CosineDistance(features_a, features_a)
```

```
distance = shogun.CosineDistance(features_a, features_a)
```

```
CosineDistance distance = new CosineDistance(features_a, features_a);
```

```
auto distance = some<CCosineDistance>(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();
```