how to make feature vector from the lists
Question: I'm new to python. Actually I have a train data which is in bag of words.Each
line of the train data is an article. The labels of the train data is in
another file and each i label is equal to i article in the train data. I did
stemming on the train data and also removed the stop words. The output is the
lists of words of each article(line). Now I want to extract a feature vector
of it and then use it in my KNN classifier in python.. I don't know how to do
it! I appreciate any quick answer. Here's my code up to the things I did:

    
    
      import nltk
      from nltk.corpus import stopwords
      from nltk import stem
      stemmer=stem.PorterStemmer()
    
    
      with open('data.txt')as file:
      while 1:
          line=file.readline().split()
          filtered_words = [w for w in line if not w in stopwords.words('english')]
          documents = [stemmer.stem(line) for line in filtered_words]
    
          print(documents)
    
    
          if not line:
             break
          pass
    


Answer: Take a look at Scikit-learn's [CountVectorizer](http://scikit-
learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html#sklearn.feature_extraction.text.CountVectorizer)
or [TfIdfVectorizer](http://scikit-
learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html#sklearn.feature_extraction.text.TfidfVectorizer).
These can take a list of documents (these are lists of tokens, as in your
example) as their input, and return a feature matrix:

    
    
    from sklearn.feature_extraction.text import CountVectorizer
    count_vect = CountVectorizer()
    X_train_counts = count_vect.fit_transform(your_list_of_documents)
    

You can find more information in the [Working with Text Data
Tutorial](http://scikit-
learn.org/stable/tutorial/text_analytics/working_with_text_data.html).

