I'm trying to understand what does this ML program - which based on doc2vec - predict:

import logging, gensim 
from gensim.models.doc2vec import TaggedDocument
from gensim.models import Doc2Vec 
import re
import os 
import random
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
import numpy as np

model = Doc2Vec.load('reviews_model.d2v') # Already trained 

sent = []
answer = []
docvec = []

for fname in ['yelp', 'amazon_cells','imdb' ]:
    with open ('sentiment labelled sentences/%s_labelled.txt'% fname, encoding = ('UTF-8')) as f:
        for i , line in enumerate(f):
            line_split = line.strip().split('\t')
            words = extract_word(line_split[0])
            docvec.append(model.infer_vector(words, steps=10))
            print (str(docvec) +  'time')

combined = list(zip(sent, docvec, answer))
sent , docvec, answer= zip(*combined)

 clf = KNeighborsClassifier(n_neighbors=9)
 score = cross_val_score(clf, docvec, answer, cv =5)

 print (str(np.mean(score)) + str(np.std(score)) )

The output be something like:


So what does it actually mean it's 79% correct? correct of predicting what exactly?

P.S: the documents learned are positive and negative reviews.


1 Answer 1


The code is trying to predict sentiment of documents (Amazon reviews or yelp reviews).

Kneighbors classifier works with vectors for input. A pre-trained doc2vec model is used to convert each review to a vector. The classifier is trained to predict sentiment label from vector representing each review.

Accuracy is mean accuracy on the dataset: percentage of reviews which were correctly classified (as positive or negative).

  • $\begingroup$ do you mean it only tries to predict to which website these reviews belong ( either amazon , yelp or imdb) ? $\endgroup$ Commented Aug 8, 2018 at 6:26
  • $\begingroup$ No. Each review has either positive or negative (obtained through labelling via humans). The code builds a classifier to learn predicting positive or negative sentiment given text of a review. $\endgroup$
    – hssay
    Commented Aug 8, 2018 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.