<p>A few comments before answering the actual question:</p>

<ol>
<li>When using an RBF kernel, you really have to tune <code>gamma</code> to get good results. Only tuning misclassification penalties (<code>C</code> and <code>weights</code>) is not sufficient.</li>
<li>The <a href="http://optunity.readthedocs.org/en/latest/api/optunity.html" rel="nofollow">main API functions</a> are <code>optunity.maximize</code>, <code>optunity.minimize</code> and <code>optunity.optimize</code>, not the solver-specific methods you are using. Though both offer similar functionality, the API functions are probably easier to use.</li>
<li>For real tuning tasks, I strongly recommend to use the default particle swarm optimizer over grid search. You will get better results in far fewer function evaluations (= time).</li>
<li>It might be easier to use Optunity's cross-validation facilities instead of scikit-learn's. This is entirely optional, though. You can find more information about this <a href="http://optunity.readthedocs.org/en/latest/user/cross_validation.html" rel="nofollow">here</a>.</li>
<li>The hyperparameters <code>m</code> and <code>w</code> are somewhat redundant. You don't have to balance classes if you're going to optimize class weights. I would stop optimizing class balance (for which you're necessarily under -or oversampling = changing your data).</li>
</ol>

<h1>The solution</h1>

<p>The function you specify for <code>optimize</code> has to be the objective function, that means the only arguments to this function must be the hyperparameters you want to optimize. For more information on this, please refer to <a href="http://arxiv.org/abs/1412.1114" rel="nofollow">Optunity's paper</a>. In your specific example, this means that the arguments should be <code>c</code>, <code>m</code> and <code>w</code>.</p>

<p>To fix <code>X</code> and <code>y</code>, you can use any of the standard Python approaches, such as <code>functools.partial</code> or closures. In my opinion, closures are the cleanest method:</p>

<pre><code>def fix_data(X_fixed, y_fixed):
    def svm_acc(m, w, c):
        X, y = balanceClasses(X_fixed, y_fixed, m)
        clf = svm.SVC(kernel='rbf', C=c, class_weight = {1: w})
        scores = cross_validation.cross_val_score(clf, X, y, 5)
        return( scores.mean() )

    return svm_acc
</code></pre>

<p>The function, <code>fix_data</code> fixes a certain data set <code>X_fixed</code> and <code>y_fixed</code> and produces a function which only has the hyperparameters as arguments, as required. Then you can do something like this (assuming you've constructed the solver etc.):</p>

<pre><code>svm_acc_with_fixed_data = fix_data(X, y)
best_pars, _ = s.optimize(svm_acc_with_fixed_data)
</code></pre>
Python Optunity: how to pass additional arguments to optimization routines
<h2>Background</h2>

<p>I am using a support vector machine for binary classification on unbalanced classes (i.e. the ratio of positive to negative labels in my training set is ~100). I would like to optimize the following parameters: m (the ratio of positive to negative labels I sample from my training data), w (the class weight), and the SVM parameter C.</p>

<h2>Problem</h2>

<p>I would like to optimize these parameters by doing a gridsearch, and have defined the score function as follows:</p>

<pre><code>def svm_acc(X, y, m, w, c):
  X, y = balanceClasses(X, y, m)
  clf = svm.SVC(kernel='rbf', C=c, class_weight = {1: w})
  scores = cross_validation.cross_val_score(clf, X, y, 5)
  return( scores.mean() )
</code></pre>

<p>where X is the feature matrix, y are binary classification labels, and <code>svm_acc</code> returns the mean accuracy from 5-fold cross-validation. I have tried the following in optunity:</p>

<pre><code>import optunity as opt
s = opt.solvers.GridSearch(mult=[1,10], w=[1,10], c=[1,10])
best_pars, _ = s.optimize(svm_acc, X=X, y=y)
</code></pre>

<p>but I get this error:</p>

<pre><code>Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: optimize() got an unexpected keyword argument 'X'
</code></pre>

<p>I gathered from the documentation that <code>optimize</code> does not take any additional keyword arguments (<code>X</code> and <code>y</code>). I have tried different variations of the above but have not been able to figure out how to pass additional parameters that should not be optimized to the routine.</p>

<p>As far as I can see, I cannot use scikit-learn's gridsearch because I want to optimize the <code>m</code> parameter, which is not 'intrinsic' to the estimator. Could anybody point me to a solution or to other python packages for doing gridsearch?</p>