pydist2 package

Submodules

pydist2.custom_typing module

This is a customized script that defines a customized type hinting.
This program and the accompanying materials are made available under the terms of the MIT License.
SPDX short identifier: MIT
Contributors: Mahmoud Harmouch, mail.
class pydist2.custom_typing.Bool(name=None)[source]

Bases: pydist2.custom_typing.CustomType

A customized Boolean data type.

class pydist2.custom_typing.CustomType(name=None)[source]

Bases: pydist2.custom_typing.TypeDescriptor

A custom tyoe class that implements Descriptor.

class pydist2.custom_typing.Float(name=None)[source]

Bases: pydist2.custom_typing.CustomType

A customized Float data type.

class pydist2.custom_typing.Integer(name=None)[source]

Bases: pydist2.custom_typing.CustomType

A customized Integer data type.

class pydist2.custom_typing.NumpyArray(name=None)[source]

Bases: pydist2.custom_typing.CustomType

A customized NumpyArray data type.

class pydist2.custom_typing.Positive(name=None)[source]

Bases: pydist2.custom_typing.TypeDescriptor

A customized Positive data type.

class pydist2.custom_typing.PositiveInteger(name=None)[source]

Bases: pydist2.custom_typing.Integer, pydist2.custom_typing.Positive

A customized Positive Integer data type.

class pydist2.custom_typing.String(name=None)[source]

Bases: pydist2.custom_typing.CustomType

A customized String data type.

class pydist2.custom_typing.TypeDescriptor(name=None)[source]

Bases: object

A basic Type Descriptor class that allows customize handling for different attributes.

It intercepts get, set and repr methods.

class pydist2.custom_typing.Void(name=None)[source]

Bases: pydist2.custom_typing.CustomType

A customized Void data type.

pydist2.distance module

The following script implements some useful vectors distances calculation.
The code has been translated from matlab and follows the same logic.
The original matlab code belongs to Piotr Dollar’s Toolbox.
Please refer to the above web pages for more explanations.
Matlab code can also be found @ MathWorks.
This program and the accompanying materials are made available under the terms of the MIT License.
SPDX short identifier: MIT
Contributors: Mahmoud Harmouch, mail.
class pydist2.distance.Chebychev(metric: pydist2.custom_typing.String = 'Chebychev Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Chebychev Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Chebychev distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.ChiSquaredDistance(metric: pydist2.custom_typing.String = 'Chi-Squared Distance')[source]

Bases: pydist2.distance.VectorsDistanceDescriptor

Compute the Chi-Squared Distance using the formula below.

d(P,Q) = sum((Pi - Qi)^2 / (Pi + Qi)) / 2

Literature:
https://www.hindawi.com/journals/mpe/2015/352849/
classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Chi-Squared Distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.CityBlock(metric: pydist2.custom_typing.String = 'City Block Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise City Block Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the City Block distance: sum|P_iq-P_hq|.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.Correlation(metric: pydist2.custom_typing.String = 'Correlation Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Correlation Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Correlation distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.Cosine(metric: pydist2.custom_typing.String = 'Cosine Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Cosine Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Cosine distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.CosineDistance(metric: pydist2.custom_typing.String = 'Cosine Distance')[source]

Bases: pydist2.distance.VectorsDistanceDescriptor

Compute the Cosine Distance equals tp 1 - Cosine_Similarity.

Literature:
https://en.wikipedia.org/wiki/Cosine_similarity
classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Distance is defined as 1 - cosine(angle between two vectors).

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.EarthMoversDistance(metric: pydist2.custom_typing.String = "Earth Mover's Distance")[source]

Bases: pydist2.distance.VectorsDistanceDescriptor

Compute the Earth Mover’s Distance between two vectors.

Literature:
https://en.wikipedia.org/wiki/Earth_mover%27s_distance
classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Distance is defined as cosine of the angle between two vectors.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.Euclidean(metric: pydist2.custom_typing.String = 'Pairwise Euclidean Distance')[source]

Bases: pydist2.distance.SQEuclidean

Pairwise Euclidean distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the distance using sqrt(Squared Euclidean Distance).

class pydist2.distance.EuclideanDistance(metric: pydist2.custom_typing.String = 'Euclidean Distance')[source]

Bases: pydist2.distance.SquaredEuclideanDistance

The L2 norm distance, aka the euclidean Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the distance using sqrt(Squared Euclidean Distance).

class pydist2.distance.Hamming(metric: pydist2.custom_typing.String = 'Hamming Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Hamming Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Hamming distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.Jaccard(metric: pydist2.custom_typing.String = 'Jaccard Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Jaccard Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Jaccard distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.L1Distance(metric: pydist2.custom_typing.String = 'L1 Distance')[source]

Bases: pydist2.distance.VectorsDistanceDescriptor

The L1 norm distance between two vectors. Also known as Manhattan Distance.

Literature:
https://en.wikipedia.org/wiki/Taxicab_geometry
classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the distance using sum(abs(P-Qi)).

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.Mahalanobis(metric: pydist2.custom_typing.String = 'Mahalanobis Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Mahalanobis Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Mahalanobis distance.

static cov(P0)[source]

A helper method that computes the covariance for a given matrix.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.Minkowski(metric: pydist2.custom_typing.String = 'Minkowski Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Minkowski Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray, exp: pydist2.custom_typing.PositiveInteger = 3) → pydist2.custom_typing.NumpyArray[source]

Compute the City Block distance: (sum(P_iq-P_hq)^(exp))^(1/exp).

exp = 2 —-> Euclidean distance exp = 1 —-> city-block distance

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.PairwiseDistanceDescriptor(metric: pydist2.custom_typing.String)[source]

Bases: abc.ABC

This descriptor construct a blueprint for different pairwise distances.

It defines the methods specified in the subclasses. All of the subclasses attributes(fields) are prefixed with an underscore, since they are not intended to be accessed directly, but rather through the getter and setter methods. They are unlikely to change, but must be defiened in a distance subclass in order to be compatible with the distance module. Any custom methods in a subclass should not be prefixed with an underscore. All of these methods must be implemented in any subclass in order to work with. Any implementation specific logic should be handled in a subclass.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

A method that computes the pairwise distance of vector P.

Parameters:P – NumpyArray that represents a certain vector.
Returns:NumpyArray that contains the ‘metric’ distance between each pair of data for the vector P.
metric

A getter method that returns the metric attribute.

Param:Instance of the class.
Returns:metric.
class pydist2.distance.SQEuclidean(metric: pydist2.custom_typing.String = 'Pairwise Squared Euclidean Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Squared Euclidean distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Squared Euclidean distances between each elements of P.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.SpearmanCorrelation(metric: pydist2.custom_typing.String = 'Spearman Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Spearman rank correlation Distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Spearman Correlation distance.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
static tiedrank(P, dim=None)[source]

A helper method that computes the tiedrank for a given matrix.

class pydist2.distance.SquaredEuclideanDistance(metric: pydist2.custom_typing.String = 'Squared Euclidean Distance')[source]

Bases: pydist2.distance.VectorsDistanceDescriptor

The L2 norm distance squared.

Literature:
https://en.wikipedia.org/wiki/Euclidean_distance#Squared_Euclidean_distance
classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Squared Euclidean distance using (P - Q)^2.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.StandardizedEuclidean(metric: pydist2.custom_typing.String = 'Pairwise Standardized Euclidean Distance')[source]

Bases: pydist2.distance.PairwiseDistanceDescriptor

Pairwise Standardized Euclidean Distance,Weighted Euclidean distance.

classmethod compute(P: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

Compute the Weighted Euclidean distance as: sqrt(wgts*(P_iq-P_hq)^2).

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.VectorsDistanceDescriptor(metric: pydist2.custom_typing.String)[source]

Bases: abc.ABC

This descriptor constuct a blueprint for different vectors distance.

It defines the methods specified in the subclasses. All of the subclasses attributes(fields) are prefixed with an underscore, since they are not intended to be accessed directly, but rather through the getter and setter methods. They are unlikely to change, but must be defiened in a distance subclass in order to be compatible with the distance module. Any custom methods in a subclass should not be prefixed with an underscore. All of these methods must be implemented in any subclass in order to work with. Any implementation specific logic should be handled in a subclass.

classmethod compute(P: pydist2.custom_typing.NumpyArray, Q: pydist2.custom_typing.NumpyArray) → pydist2.custom_typing.NumpyArray[source]

A method that computes the distance between two vectors P and Q.

Parameters:
  • P – NumpyArray that represents the first vector/matrix.
  • Q – NumpyArray that represents the second vector/matrix.
Returns:

The distance between the two vectors.

metric

A getter method that returns the metric attribute.

Param:Instance of the class.
Returns:String that represents the metric attribute.
class pydist2.distance.pdist1[source]

Bases: object

An interface for distance calculation between each pair of points.

The method of computation will be chosen based on the metric.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.
class pydist2.distance.pdist2[source]

Bases: object

An interface for distance calculation between each pair of two vectors.

The method of computation will be chosen based on the metric.

metric

A getter method that returns the metric attribute.

Param:instance of the class.
Returns:metric.

Module contents

Top-level package for pydist2.