#NEWLINE# def pairwise_distances( X , Y = None , metric = 'euclidean' , n_jobs = 1 , **kwds ) : #NEWLINE# #INDENT# ' Compute the distance matrix from a vector array X and optional Y.\n\n    This method takes either a vector array or a distance matrix, and returns\n    a distance matrix. If the input is a vector array, the distances are\n    computed. If the input is a distances matrix, it is returned instead.\n\n    This method provides a safe way to take a distance matrix as input, while\n    preserving compatibility with many other algorithms that take a vector\n    array.\n\n    If Y is given (default is None), then the returned matrix is the pairwise\n    distance between the arrays from both X and Y.\n\n    Valid values for metric are:\n\n    - From scikit-learn: [\'cityblock\', \'cosine\', \'euclidean\', \'l1\', \'l2\',\n      \'manhattan\']. These metrics support sparse matrix inputs.\n\n    - From scipy.spatial.distance: [\'braycurtis\', \'canberra\', \'chebyshev\',\n      \'correlation\', \'dice\', \'hamming\', \'jaccard\', \'kulsinski\', \'mahalanobis\',\n      \'matching\', \'minkowski\', \'rogerstanimoto\', \'russellrao\', \'seuclidean\',\n      \'sokalmichener\', \'sokalsneath\', \'sqeuclidean\', \'yule\']\n      See the documentation for scipy.spatial.distance for details on these\n      metrics. These metrics do not support sparse matrix inputs.\n\n    Note that in the case of \'cityblock\', \'cosine\' and \'euclidean\' (which are\n    valid scipy.spatial.distance metrics), the scikit-learn implementation\n    will be used, which is faster and has support for sparse matrices (except\n    for \'cityblock\'). For a verbose description of the metrics from\n    scikit-learn, see the __doc__ of the sklearn.pairwise.distance_metrics\n    function.\n\n    Read more in the :ref:`User Guide <metrics>`.\n\n    Parameters\n    ----------\n    X : array [n_samples_a, n_samples_a] if metric == "precomputed", or,              [n_samples_a, n_features] otherwise\n        Array of pairwise distances between samples, or a feature array.\n\n    Y : array [n_samples_b, n_features], optional\n        An optional second feature array. Only allowed if metric != "precomputed".\n\n    metric : string, or callable\n        The metric to use when calculating distance between instances in a\n        feature array. If metric is a string, it must be one of the options\n        allowed by scipy.spatial.distance.pdist for its metric parameter, or\n        a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS.\n        If metric is "precomputed", X is assumed to be a distance matrix.\n        Alternatively, if metric is a callable function, it is called on each\n        pair of instances (rows) and the resulting value recorded. The callable\n        should take two arrays from X as input and return a value indicating\n        the distance between them.\n\n    n_jobs : int\n        The number of jobs to use for the computation. This works by breaking\n        down the pairwise matrix into n_jobs even slices and computing them in\n        parallel.\n\n        If -1 all CPUs are used. If 1 is given, no parallel computing code is\n        used at all, which is useful for debugging. For n_jobs below -1,\n        (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one\n        are used.\n\n    `**kwds` : optional keyword parameters\n        Any further parameters are passed directly to the distance function.\n        If using a scipy.spatial.distance metric, the parameters are still\n        metric dependent. See the scipy docs for usage examples.\n\n    Returns\n    -------\n    D : array [n_samples_a, n_samples_a] or [n_samples_a, n_samples_b]\n        A distance matrix D such that D_{i, j} is the distance between the\n        ith and jth vectors of the given matrix X, if Y is None.\n        If Y is not None, then D_{i, j} is the distance between the ith array\n        from X and the jth array from Y.\n\n    ' #NEWLINE# if ( ( metric not in _VALID_METRICS ) and ( not callable ( metric ) ) and ( metric != 'precomputed' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( "Unknown metric %s. Valid metrics are %s, or 'precomputed', or a callable" % ( metric , _VALID_METRICS ) ) ) #NEWLINE# #UNINDENT# if ( metric == 'precomputed' ) : #NEWLINE# #INDENT# ( X , _ ) = check_pairwise_arrays ( X , Y , precomputed = True ) #NEWLINE# return X #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( metric in PAIRWISE_DISTANCE_FUNCTIONS ) : #NEWLINE# #INDENT# func = PAIRWISE_DISTANCE_FUNCTIONS [ metric ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if callable ( metric ) : #NEWLINE# #INDENT# func = partial ( _pairwise_callable , metric = metric , ** kwds ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( issparse ( X ) or issparse ( Y ) ) : #NEWLINE# #INDENT# raise TypeError ( 'scipy distance metrics do not support sparse matrices.' ) #NEWLINE# #UNINDENT# ( X , Y ) = check_pairwise_arrays ( X , Y ) #NEWLINE# if ( ( n_jobs == 1 ) and ( X is Y ) ) : #NEWLINE# #INDENT# return distance . squareform ( distance . pdist ( X , metric = metric , ** kwds ) ) #NEWLINE# #UNINDENT# func = partial ( distance . cdist , metric = metric , ** kwds ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return _parallel_pairwise ( X , Y , func , n_jobs , ** kwds )