title: Feature importance using SVM's coef_ function
body: <p>I am working on a text classification project and trying to use SVC(kernel= 'linear') to get the feature importance. Here is my code:<br>
  (I changed the code from <a href="https://stackoverflow.com/questions/41592661/determining-the-most-contributing-features-for-svm-classifier-in-sklearn">this post</a>)</p>
  
  <pre><code>X = df1[features]
  y = df1['label']
  
  
  # Create selector class for text and numbers
  class TextSelector(BaseEstimator, TransformerMixin):
      """Transformer to select a single column from the data frame to perform additional transformations on"""
      def __init__(self, key):
          self.key = key
  
      def fit(self, X, y=None):
          return self
  
      def transform(self, X):
          return X[self.key]
  
  class NumberSelector(BaseEstimator, TransformerMixin):
      """For data grouped by feature, select subset of data at a provided key."""
      def __init__(self, key):
          self.key = key
  
      def fit(self, X, y=None):
          return self
  
      def transform(self, X):
          return X[[self.key]]
  
  scaler = StandardScaler()    
  text = Pipeline([
                  ('selector', TextSelector(key='title_mainText')),
                  ('vect', TfidfVectorizer(ngram_range=(1, 2))),                
              ])
  
  upper_title =  Pipeline([
                  ('selector', NumberSelector(key='upper_title')),
                  ('standard', scaler),
              ])
  
  upper_mainText =  Pipeline([
                  ('selector', NumberSelector(key='upper_mainText')),
                  ('standard', scaler),
              ])
  
  punct_title =  Pipeline([
                  ('selector', NumberSelector(key='punct_title')),
                  ('standard', scaler),
              ])
  
  punct_mainText =  Pipeline([
                  ('selector', NumberSelector(key='punct_mainText')),
                  ('standard', scaler),
              ])
  
  
  exclamations_title =  Pipeline([
                  ('selector', NumberSelector(key='exclamations_title')),
                  ('standard', scaler),
              ])
  
  
  exclamations_text =  Pipeline([
                  ('selector', NumberSelector(key='exclamations_text')),
                  ('standard', scaler),
              ])
  
  
  feats = FeatureUnion([('title_mainText', text), 
                        ('upper_title', upper_title),
                        ('upper_mainText', upper_mainText),
                        ('punct_title', punct_title),
                        ('punct_mainText', punct_mainText),                    
                        ('exclamations_text', exclamations_text),
                        ('exclamations_title', exclamations_title),                        
  
  feature_processing = Pipeline([('feats', feats)])
  
  pipeline = Pipeline([
          ('features', feats),
          ('classifier', SVC(C=1, kernel= 'linear', max_iter= 1000, tol=0.0001, probability=True))
      ])
  
  
      def f_importances(coef, names):
          imp = coef
          imp,names = zip(*sorted(zip(imp,names)))
          plt.barh(range(len(names)), imp, align='center')
          plt.yticks(range(len(names)), names)
          plt.show()
  
      features_names = ['title_mainText', 'upper_title', 'upper_mainText', 'punct_title', 'punct_mainText',
                        'exclamations_title', 'exclamations_text']
      pipeline.fit(X, y)
      clf = pipeline.named_steps['classifier']
      f_importances(clf.coef_, features_names)
  </code></pre>
  
  <p>However, it shows an error message, and I don't know where I have done wrong.
  Did someone have the experience with this before?</p>
  
  <blockquote>
    <p>ValueError                                Traceback (most recent call
    last)  in ()
         13 pipeline.fit(X, y)
         14 clf = pipeline.named_steps['classifier']
    ---> 15 f_importances((clf.coef_[0]), features_names)
         16 </p>
    
    <p> in f_importances(coef, names)
          5     imp = coef
          6     imp,names = zip(*sorted(zip(imp,names)))
    ----> 7     plt.barh(range(len(names)), imp, align='center')
          8     plt.yticks(range(len(names)), names)
          9     plt.show()</p>
    
    <p>/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in
    barh(*args, **kwargs)    2667                       mplDeprecation)<br>
    2668     try:
    -> 2669         ret = ax.barh(*args, **kwargs)    2670     finally:    2671         ax._hold = washold</p>
    
    <p>/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in
    barh(self, *args, **kwargs)    2281<br>
    kwargs.setdefault('orientation', 'horizontal')    2282         patches
    = self.bar(x=left, height=height, width=width,
    -> 2283                            bottom=y, **kwargs)    2284         return patches    2285 </p>
    
    <p>/anaconda3/lib/python3.6/site-packages/matplotlib/<strong>init</strong>.py in
    inner(ax, *args, **kwargs)    1715<br>
    warnings.warn(msg % (label_namer, func.<strong>name</strong>),    1716<br>
    RuntimeWarning, stacklevel=2)
    -> 1717             return func(ax, *args, **kwargs)    1718         pre_doc = inner.<strong>doc</strong>    1719         if pre_doc is None:</p>
    
    <p>/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in
    bar(self, *args, **kwargs)    2091             elif orientation ==
    'horizontal':    2092                 r.sticky_edges.x.append(l)
    -> 2093             self.add_patch(r)    2094             patches.append(r)    2095 </p>
    
    <p>/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in
    add_patch(self, p)    1852         if p.get_clip_path() is None:<br>
    1853             p.set_clip_path(self.patch)
    -> 1854         self._update_patch_limits(p)    1855         self.patches.append(p)    1856         p._remove_method = lambda h:
    self.patches.remove(h)</p>
    
    <p>/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in
    _update_patch_limits(self, patch)    1868         # or height.    1869         if (isinstance(patch, mpatches.Rectangle) and
    -> 1870                 ((not patch.get_width()) and (not patch.get_height()))):    1871             return    1872<br>
    vertices = patch.get_path().vertices</p>
    
    <p>/anaconda3/lib/python3.6/site-packages/scipy/sparse/base.py in
    <strong>bool</strong>(self)
        286             return self.nnz != 0
        287         else:
    --> 288             raise ValueError("The truth value of an array with more than one "
        289                              "element is ambiguous. Use a.any() or a.all().")
        290     <strong>nonzero</strong> = <strong>bool</strong></p>
    
    <p>ValueError: The truth value of an array with more than one element is
    ambiguous. Use a.any() or a.all().</p>
  </blockquote>
  
  <p>Thank you!</p>