title: Assigning weights to a multilabel SVM to balance classes
body: <p>How is this done? I am using Sklearn to train an SVM. My classes are unbalanced. Note that my problem is multiclass, multilabel so I am using OneVsRestClassifier:</p>
  
  <pre><code>mlb = MultiLabelBinarizer()
  y = mlb.fit_transform(y_train)
  
  clf = OneVsRestClassifier(svm.SVC(kernel='rbf'))
  clf = clf.fit(x, y) 
  pred = clf.predict(x_test)
  </code></pre>
  
  <p>Can I add a 'sample_weight' parameter somewhere to account for the unbalanced classes? </p>
  
  <hr>
  
  <p>When I add a class_weight dict to the svm I get the error: </p>
  
  <pre><code>ValueError: Class label 2 not present
  </code></pre>
  
  <p>This is because I have converted my labels to binary using the mlb. However, if I do not convert the labels, I get: </p>
  
  <pre><code>ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead. 
  </code></pre>
  
  <p>class_weight is a dict, mapping the class labels to the weight: {1: 1, 2: 1, 3: 3...}</p>
  
  <p>Here are the details of x and y: </p>
  
  <pre><code>print(X[0])  
  [ 0.76625633  0.63062721  0.01954162 ...,  1.1767817   0.249034    0.23544988]
  print(type(X))
  &lt;type 'numpy.ndarray'&gt;
  
  print(y[0])
  print(type(y))
  [1, 2, 3, 4, 5, 6, 7]
  &lt;type 'numpy.ndarray'&gt;
  </code></pre>
  
  <p>Note that mlb = MultiLabelBinarizer(); y = mlb.fit_transform(y_train) converts y to a binary array. </p>
  
  <hr>
  
  <p>The suggested answer produces the error: </p>
  
  <pre><code>ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead.
  </code></pre>
  
  <p><strong>So, the problem reduces to converting the labels (a np.array) to a sparse matrix.</strong> </p>
  
  <pre><code>from scipy import sparse
  y_sp = sparse.csr_matrix(y) 
  </code></pre>
  
  <p>This produces the error: </p>
  
  <pre><code>TypeError: no supported conversion for types: (dtype('O'),)
  </code></pre>
  
  <p>I will open a new query for this. </p>