December 2018
Beginner to intermediate
684 pages
21h 9m
English
The CountVectorizer result lets us find the most similar documents using the pdist() function for pairwise distances provided by the scipy.spatial.distance module. It returns a condensed distance matrix with entries corresponding to the upper triangle of a square matrix. We use np.triu_indices() to translate the index that minimizes the distance to the row and column indices that in turn correspond to the closest token vectors:
m = binary_dtm.todense() # pdist does not accept sparse formatpairwise_distances = pdist(m, metric='cosine')closest = np.argmin(pairwise_distances) # index that minimizes distancerows, cols = np.triu_indices(n_docs) # get row-col indicesrows[closest], cols[closest](11, 75)
Articles ...