title: Getting error on averaging Word2Vec crerated vectors
body: <p>i want to use gensim to create Word2Vec vectors on my tweet dataset. the code is for multi-label emotion classification based on tweets. i have aggregated tweets file which contains 107k tweets. i used this for creating Word2Vec vectors based on. my code:</p>
  <pre><code>np.set_printoptions(threshold=sys.maxsize)
  
  #Pre-Processor Function
  pre_processor = TextPreProcessor(
      omit=['url', 'email', 'percent', 'money', 'phone', 'user',
          'time', 'url', 'date', 'number'],
      
      normalize=['url', 'email', 'percent', 'money', 'phone', 'user',
          'time', 'url', 'date', 'number'],
       
      segmenter=&quot;twitter&quot;, 
      
      corrector=&quot;twitter&quot;, 
      
      unpack_hashtags=True,
      unpack_contractions=True,
      
      tokenizer=SocialTokenizer(lowercase=True).tokenize,
      
      dicts=[emoticons]
  )
  
  #Averaging Words Vectors to Create Sentence Embedding
  def word_averaging(wv, words):
      all_words, mean = set(), []
      
      for word in words:
          if isinstance(word, np.ndarray):
              mean.append(word)
          elif word in wv.vocab:
              mean.append(wv.syn0norm[wv.vocab[word].index])
              all_words.add(wv.vocab[word].index)
  
      if not mean:
          logging.warning(&quot;cannot compute similarity with no input %s&quot;, words)
          # FIXME: remove these examples in pre-processing
          return np.zeros(wv.vector_size,)
  
      mean = gensim.matutils.unitvec(np.array(mean).mean(axis=0)).astype(np.float32)
      return mean
  
  def  word_averaging_list(wv, text_list):
      return np.vstack([word_averaging(wv, post) for post in text_list ])
  
  #Loading data
  raw_aggregate_tweets = pandas.read_excel('E:\\aggregate.xlsx').iloc[:,0] #Loading all tweets to have a bigger word2vec corpus
  
  raw_train_tweets = pandas.read_excel('E:\\train.xlsx').iloc[:,1] #Loading all train tweets
  train_labels = np.array(pandas.read_excel('E:\\train.xlsx').iloc[:,2:13]) #Loading corresponding train labels (11 emotions)
  
  raw_test_tweets = pandas.read_excel('E:\\test.xlsx').iloc[:,1] #Loading all test tweets
  test_gold_labels = np.array(pandas.read_excel('E:\\test.xlsx').iloc[:,2:13]) #Loading corresponding test labels (11 emotions)
  print(&quot;please wait&quot;)
  
  #Pre-Processing
  aggregate_tweets=[]
  train_tweets=[]
  test_tweets=[]
  for tweets in raw_aggregate_tweets:
      aggregate_tweets.append(pre_processor.pre_process_doc(tweets))
  
  for tweets in raw_train_tweets:
      train_tweets.append(pre_processor.pre_process_doc(tweets))
  
  for tweets in raw_test_tweets:
      test_tweets.append(pre_processor.pre_process_doc(tweets))
  
  
  #Vectorizing 
  w2v_model = gensim.models.Word2Vec(aggregate_tweets, min_count = 10, size = 300, window = 8)
  
  
  train_array = word_averaging_list(w2v_model.wv,train_tweets)
  test_array = word_averaging_list(w2v_model.wv,test_tweets)
  </code></pre>
  <p>but i get this error:</p>
  <pre><code>TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-1-8a5fe4dbf144&gt; in &lt;module&gt;
      110 print(w2v_model.wv.vectors.shape)
      111 
  --&gt; 112 train_array = word_averaging_list(w2v_model.wv,train_tweets)
      113 test_array = word_averaging_list(w2v_model.wv,test_tweets)
      114 
  
  &lt;ipython-input-1-8a5fe4dbf144&gt; in word_averaging_list(wv, text_list)
       70 
       71 def  word_averaging_list(wv, text_list):
  ---&gt; 72     return np.vstack([word_averaging(wv, post) for post in text_list ])
       73 
       74 #Averaging Words Vectors to Create Sentence Embedding
  
  &lt;ipython-input-1-8a5fe4dbf144&gt; in &lt;listcomp&gt;(.0)
       70 
       71 def  word_averaging_list(wv, text_list):
  ---&gt; 72     return np.vstack([word_averaging(wv, post) for post in text_list ])
       73 
       74 #Averaging Words Vectors to Create Sentence Embedding
  
  &lt;ipython-input-1-8a5fe4dbf144&gt; in word_averaging(wv, words)
       58             mean.append(word)
       59         elif word in wv.vocab:
  ---&gt; 60             mean.append(wv.syn0norm[wv.vocab[word].index])
       61             all_words.add(wv.vocab[word].index)
       62 
  
  TypeError: 'NoneType' object is not subscriptable
  </code></pre>
  <p>It looks like your post is mostly code; please add some more details. what is this error of site? my god. i don't have any more details. sorry i have to do this to bypass the error.</p>
  <h1>Second averaging method</h1>
  <pre><code>#Averaging Words Vectors to Create Sentence Embedding
  def get_mean_vector(word2vec_model, words):
      # remove out-of-vocabulary words
      words = [word for word in words if word in word2vec_model.vocab]
      if len(words) &gt;= 1:
          return np.mean(word2vec_model[words], axis=0)
      else:
          return np.zeros(word2vec_model.vector_size)
  
  #Vectorizing 
  w2v_model = gensim.models.Word2Vec(aggregate_tweets, min_count = 11, size = 400, window = 18, sg=1)
  
  train_array=[]
  test_array=[]
  for tweet in train_tweets:
      vec = get_mean_vector(w2v_model.wv, tweet)
      if len(vec) &gt; 0:
          train_array.append(vec)
          
  for tweet in test_tweets:
      vec = get_mean_vector(w2v_model.wv, tweet)
      if len(vec) &gt; 0:
          test_array.append(vec)
  </code></pre>