#NEWLINE# def pairwise_distances_argmin_min( X , Y , axis = 1 , metric = 'euclidean' , batch_size = 500 , metric_kwargs = None ) : #NEWLINE# #INDENT# "Compute minimum distances between one point and a set of points.\n\n    This function computes for each row in X, the index of the row of Y which\n    is closest (according to the specified distance). The minimal distances are\n    also returned.\n\n    This is mostly equivalent to calling:\n\n        (pairwise_distances(X, Y=Y, metric=metric).argmin(axis=axis),\n         pairwise_distances(X, Y=Y, metric=metric).min(axis=axis))\n\n    but uses much less memory, and is faster for large arrays.\n\n    Parameters\n    ----------\n    X, Y : {array-like, sparse matrix}\n        Arrays containing points. Respective shapes (n_samples1, n_features)\n        and (n_samples2, n_features)\n\n    batch_size : integer\n        To reduce memory consumption over the naive solution, data are\n        processed in batches, comprising batch_size rows of X and\n        batch_size rows of Y. The default value is quite conservative, but\n        can be changed for fine-tuning. The larger the number, the larger the\n        memory usage.\n\n    metric : string or callable, default 'euclidean'\n        metric to use for distance computation. Any metric from scikit-learn\n        or scipy.spatial.distance can be used.\n\n        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 as input and return one value indicating the\n        distance between them. This works for Scipy's metrics, but is less\n        efficient than passing the metric name as a string.\n\n        Distance matrices are not supported.\n\n        Valid values for metric are:\n\n        - from scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2',\n          'manhattan']\n\n        - from scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev',\n          'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski',\n          'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto',\n          'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath',\n          'sqeuclidean', 'yule']\n\n        See the documentation for scipy.spatial.distance for details on these\n        metrics.\n\n    metric_kwargs : dict, optional\n        Keyword arguments to pass to specified metric function.\n\n    axis : int, optional, default 1\n        Axis along which the argmin and distances are to be computed.\n\n    Returns\n    -------\n    argmin : numpy.ndarray\n        Y[argmin[i], :] is the row in Y that is closest to X[i, :].\n\n    distances : numpy.ndarray\n        distances[i] is the distance between the i-th row in X and the\n        argmin[i]-th row in Y.\n\n    See also\n    --------\n    sklearn.metrics.pairwise_distances\n    sklearn.metrics.pairwise_distances_argmin\n    " #NEWLINE# dist_func = None #NEWLINE# if ( metric in PAIRWISE_DISTANCE_FUNCTIONS ) : #NEWLINE# #INDENT# dist_func = PAIRWISE_DISTANCE_FUNCTIONS [ metric ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( not callable ( metric ) ) and ( not isinstance ( metric , str ) ) ) : #NEWLINE# #INDENT# raise ValueError ( "'metric' must be a string or a callable" ) #NEWLINE# #UNINDENT# #UNINDENT# ( X , Y ) = check_pairwise_arrays ( X , Y ) #NEWLINE# if ( metric_kwargs is None ) : #NEWLINE# #INDENT# metric_kwargs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( axis == 0 ) : #NEWLINE# #INDENT# ( X , Y ) = ( Y , X ) #NEWLINE# #UNINDENT# indices = np . empty ( X . shape [ 0 ] , dtype = np . intp ) #NEWLINE# values = np . empty ( X . shape [ 0 ] ) #NEWLINE# values . fill ( np . infty ) #NEWLINE# for chunk_x in gen_batches ( X . shape [ 0 ] , batch_size ) : #NEWLINE# #INDENT# X_chunk = X [ chunk_x , : ] #NEWLINE# for chunk_y in gen_batches ( Y . shape [ 0 ] , batch_size ) : #NEWLINE# #INDENT# Y_chunk = Y [ chunk_y , : ] #NEWLINE# if ( dist_func is not None ) : #NEWLINE# #INDENT# if ( metric == 'euclidean' ) : #NEWLINE# #INDENT# d_chunk = safe_sparse_dot ( X_chunk , Y_chunk . T , dense_output = True ) #NEWLINE# d_chunk *= ( - 2 ) #NEWLINE# d_chunk += row_norms ( X_chunk , squared = True ) [ : , np . newaxis ] #NEWLINE# d_chunk += row_norms ( Y_chunk , squared = True ) [ np . newaxis , : ] #NEWLINE# np . maximum ( d_chunk , 0 , d_chunk ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# d_chunk = dist_func ( X_chunk , Y_chunk , ** metric_kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# d_chunk = pairwise_distances ( X_chunk , Y_chunk , metric = indices , ** metric_kwargs ) #NEWLINE# #UNINDENT# min_indices = d_chunk . argmin ( axis = 1 ) #NEWLINE# min_values = d_chunk [ ( np . arange ( ( chunk_x . stop - chunk_x . start ) ) , min_indices ) ] #NEWLINE# flags = ( values [ chunk_x ] > min_values ) #NEWLINE# indices [ chunk_x ] [ flags ] = ( min_indices [ flags ] + chunk_y . start ) #NEWLINE# values [ chunk_x ] [ flags ] = min_values [ flags ] #NEWLINE# #UNINDENT# #UNINDENT# if ( ( metric == 'euclidean' ) and ( not metric_kwargs . get ( 'squared' , False ) ) ) : #NEWLINE# #INDENT# np . sqrt ( values , values ) #NEWLINE# #UNINDENT# return ( indices , values )