title: Use Logistic Regression for Tweets Topic Classification
body: <p>I have a problem regarding use of Logistic Regression. I'm currently making a Tweets Topic Classification in Python. 
  so far I'm able to read the train data from MySQL table using pandas, clean the train tweets using NLTK and create feature vectors using CountVectorizer. 
  here's the code below..</p>
  
  <pre><code>import pandas as pd
  from sqlalchemy import *
  from nltk.tokenize import RegexpTokenizer
  from nltk.corpus import stopwords
  import re
  from nltk.stem import SnowballStemmer
  from sklearn.feature_extraction.text import CountVectorizer
  from sklearn.linear_model import LogisticRegression
  
  #connect to database and get the training data
  engine = create_engine('mysql+mysqlconnector://root:root@localhost:3306/machinelearning')
  tweet = pd.read_sql_query('SELECT label, tweets FROM tweetstable', engine, index_col='label')
  
  #TEXT PREPROCESSING (REMOVE HTML MARKUP, REMOVE PUNCTUATION, TOKENIZING, REMOVE STOP WORDS, STEMMING)
  
  def preprocessing(pptweets):
      pptweets = pptweets.lower()
      urlrtweets = re.sub(r'https:.*$', ":", pptweets)
      rpptweets = urlrtweets.replace("_", " ")
      tokenizer = RegexpTokenizer(r'\w+')
      tokens = tokenizer.tokenize(rpptweets)
      filteredwords = [w for w in tokens if not w in stopwords.words('english')]
      stemmer = SnowballStemmer("english")
      stweets = [stemmer.stem(tokens) for tokens in filteredwords]
      return " ".join(stweets)
  
  #initialize an empty list to hold the clean reviews
  cleantweets = []
  
  #loop over each review, create an index i that goes from 0 to the length of tweets list
  for i in range(0, len(tweet["tweets"])):
      cleantweets.append(preprocessing(tweet["tweets"][i]))
  
  #initialize the "CountVectorizer" object, which is scikit-learn's BoW tools
  vectorizer = CountVectorizer(analyzer="word",
                               tokenizer=None,
                               preprocessor=None,
                               stop_words=None,
                               max_features=5000)
  
  #fit_transform() does two functions: First, it fits the model
  #and learns the vocabulary; second, it transforms our training data
  #into feature vectors. the input to fit_transform should be a list of strings
  traindatafeatures = vectorizer.fit_transform(cleantweets)
  
  #Numpy arrays are easy to work with, so convert the result to an array
  traindatafeatures = traindatafeatures.toarray()
  </code></pre>
  
  <p>the problem I'm facing right now is.. I don't know how to use Logistic Regression to learn from the train data. here's the code I use to fit the train data into Logistic Regression classifier.</p>
  
  <pre><code>#train the model
  logmodel = LogisticRegression()
  logmodel.fit(traindatafeatures, tweet["label"])
  
  #check trained model intercept
  
  print(logmodel.intercept_)
  #check trained model coefficients
  print(logmodel.coef_)
  </code></pre>
  
  <p>I pass the traindatafeatures as the input X and tweet["label"] as the label/class Y for each tweet to Logistic Regression classifier so it can learn from it but when I run the full code I get error like this:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Indra\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 1945, in get_loc
      return self._engine.get_loc(key)
    File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)
    File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)
    File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)
    File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)
  KeyError: 'label'
  </code></pre>
  
  <p>During handling of the above exception, another exception occurred:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/Indra/PycharmProjects/TextClassifier/textclassifier.py", line 52, in &lt;module&gt;
      logmodel.fit(traindatafeatures, tweet["label"])
    File "C:\Users\Indra\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
      return self._getitem_column(key)
    File "C:\Users\Indra\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
      return self._get_item_cache(key)
    File "C:\Users\Indra\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
      values = self._data.get(item)
    File "C:\Users\Indra\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3290, in get
      loc = self.items.get_loc(item)
    File "C:\Users\Indra\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
      return self._engine.get_loc(self._maybe_cast_indexer(key))
    File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)
    File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)
    File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)
    File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)
  KeyError: 'label'
  </code></pre>
  
  <p>Can anyone help me solve this problem? :( I've been searching for tutorials but I haven't found anything so far.</p>