title: Using DictVectorizer with sklearn DecisionTreeClassifier
body: <p>I try to start a decision tree with python and sklearn.
  Working approach was like this:</p>
  
  <pre><code>import pandas as pd
  from sklearn import tree
  
  for col in set(train.columns):
      if train[col].dtype == np.dtype('object'):
          s = np.unique(train[col].values)
          mapping = pd.Series([x[0] for x in enumerate(s)], index = s)
          train_fea = train_fea.join(train[col].map(mapping))
      else:
          train_fea = train_fea.join(train[col])
  
  dt = tree.DecisionTreeClassifier(min_samples_split=3,
                               compute_importances=True,max_depth=5)
  dt.fit(train_fea, labels)
  </code></pre>
  
  <p>Now I try to make the same thing with DictVectorizer, but my code doesn't work:</p>
  
  <pre><code>from sklearn.feature_extraction import DictVectorizer
  
  vec = DictVectorizer(sparse=False)
  train_fea = vec.fit_transform([dict(enumerate(sample)) for sample in train])
  
  dt = tree.DecisionTreeClassifier(min_samples_split=3,
                               compute_importances=True,max_depth=5)
  dt.fit(train_fea, labels)
  </code></pre>
  
  <p>I've got a error in last line: "ValueError: Number of labels=332448 does not match number of samples=55". As I learnt from documentation DictVectorize was designed to transform nominal features into numerical ones. What do I do wrong?</p>
  
  <p>corrected (thanks ogrisel for pushing me to make a full example):</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  from sklearn import tree
  
  ##################################
  #  working example
  train = pd.DataFrame({'a' : ['a', 'b', 'a'], 'd' : ['e', 'e', 'f'],
                    'b' : [0, 1, 1], 'c' : ['b', 'c', 'b']})
  columns = set(train.columns)
  columns.remove('b')
  train_fea = train[['b']]
  
  for col in columns:
      if train[col].dtype == np.dtype('object'):
          s = np.unique(train[col].values)
          mapping = pd.Series([x[0] for x in enumerate(s)], index = s)
          train_fea = train_fea.join(train[col].map(mapping))
      else:
          train_fea = train_fea.join(train[col])
  
  dt = tree.DecisionTreeClassifier(min_samples_split=3,
                           compute_importances=True,max_depth=5)
  dt.fit(train_fea, train['c'])
  
  ##########################################
  # example with DictVectorizer and error
  
  from sklearn.feature_extraction import DictVectorizer
  
  vec = DictVectorizer(sparse=False)
  train_fea = vec.fit_transform([dict(enumerate(sample)) for sample in train])
  
  dt = tree.DecisionTreeClassifier(min_samples_split=3,
                           compute_importances=True,max_depth=5)
  dt.fit(train_fea, train['c'])
  </code></pre>
  
  <p>Last code was fixed with a help of ogrisel:</p>
  
  <pre><code>import pandas as pd
  from sklearn import tree
  from sklearn.feature_extraction import DictVectorizer
  from sklearn import preprocessing
  
  train = pd.DataFrame({'a' : ['a', 'b', 'a'], 'd' : ['e', 'x', 'f'],
                    'b' : [0, 1, 1], 'c' : ['b', 'c', 'b']})
  
  # encode labels
  labels = train[['c']]
  le = preprocessing.LabelEncoder()
  labels_fea = le.fit_transform(labels) 
  # vectorize training data
  del train['c']
  train_as_dicts = [dict(r.iteritems()) for _, r in train.iterrows()]
  train_fea = DictVectorizer(sparse=False).fit_transform(train_as_dicts)
  # use decision tree
  dt = tree.DecisionTreeClassifier()
  dt.fit(train_fea, labels_fea)
  # transform result
  predictions = le.inverse_transform(dt.predict(train_fea).astype('I'))
  predictions_as_dataframe = train.join(pd.DataFrame({"Prediction": predictions}))
  print predictions_as_dataframe
  </code></pre>
  
  <p>everything works</p>