unsurprisinglywe have five posts with total of different words the following words that have been tokenized will be countedprint(vectorizer get_feature_names()[ 'about' 'actually' 'capabilities' 'contains' 'data' 'databases' 'images' 'imaging' 'interesting' 'is' 'it' 'learning' 'machine' 'most' 'much' 'not' 'permanently' 'post' 'provide' 'safe' 'storage' 'store' 'stuff' 'this' 'toy'now we can vectorize our new post as followsnew_post "imaging databasesnew_post_vec vectorizer transform([new_post]note that the count vectors returned by the transform method are sparse that iseach vector does not store one count value for each wordas most of those counts would be zero (post does not contain the wordinsteadit uses the more memory efficient implementation coo_matrix (for "coordinate"our new postfor instanceactually contains only two elementsprint(new_post_vec( ) ( ) via its member toarray()we can again access full ndarray as followsprint(new_post_vec toarray()[[ ]we need to use the full array if we want to use it as vector for similarity calculations for the similarity measurement (the naive one)we calculate the euclidean distance between the count vectors of the new post and all the old posts as followsimport scipy as sp def dist_raw( )delta - return sp linalg norm(delta toarray()the norm(function calculates the euclidean norm (shortest distancewith dist_ rawwe just need to iterate over all the posts and remember the nearest oneimport sys best_doc none best_dist sys maxint best_i none for in range( num_samples)