title: combine python dictionary and sparse matrix
body: <p>I have created a python dictionary for a set of text as features. I am using libsvm for classification in python which takes sparse matrix as input.The input should be in:
  <code>&lt;label&gt; &lt;index1&gt;:&lt;value1&gt; &lt;index2&gt;:&lt;value2&gt; ...</code>form Is there any way to convert my dictionary to the libsvm format. I tried to save it in csv file and converting to the sparse matrix format. But I get some error while converting to a csv file. My dictionary looks like this:</p>
  
  <pre><code>{0: {0: 1, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0,
   15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0}, 1: {0: 1, 1: 1, 2: 0, 3:
   1, 4: 0, 5: 0, 6: 1, 7: 0, 8: 0, 9: 1, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 
  19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0}, 2: {0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0, 7: 0,
   8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0,
   23: 0, 24: 0, 25: 0}....}
  </code></pre>
  
  <p>The code I used to convert save it in csv file is:</p>
  
  <pre><code>writer = csv.writer(open('/home/gew/python_docs/feature_tags.csv', 'wb'))
  for key, value in features_tags.items():
      writer.writerows([key,value])
  </code></pre>
  
  <p>But I get an error <code>Error: sequence expected</code></p>
  
  <p>This is only a subset of my features to be used in libsvm, after converting this to a sparse matrix is there any way I can combine the two sparse matrix to form the feature vector for libsvm. The other features is already in sparse matrix form. I used dump_svmlight_file to save the tf-idf in libsvm form:</p>
  
  <pre><code>-1 491:0.0776333443740911 1161:0.4481444868908682 1220:0.09787659944322356 1297:0.09091558887557132
   1518:0.2558810794182657 1663:0.1000883672992121 1806:0.09191664928182955 2296:0.1493814956302894
  +1 5749:0.1493814956302894 5819:0.1200273208236481 5843:0.1493814956302894 5859:0.1087982845232741
   5966:0.1076151102064468 6182:0.1730733336818764 6238:0.07999390379552077 6389:0.07944410971663282
  +1 ...........
  </code></pre>
  
  <p>-1 and +1 being the class label. It contains around 6500 feature in total. How can I combine this two features as it is for the same text to form a feature vector for libsvm. So the features from dictionary may start from 6501 key.Is there any lib that I can use to convert the dictionary to libsvm form and combine the two sparse matrix?</p>