title: Reverse support vector machine: calculating the predictions
body: <p>I was wondering, given the regression coefficients of a svm regression model, if one could calculate the predictions 'by hand', made by that model. 
  More precisely, suppose:</p>
  
  <pre><code>svc = SVR(kernel='rbf', epsilon=0.3, gamma=0.7, C=64)
  svc.fit(X_train, y_train)
  </code></pre>
  
  <p>then you can obtain the predictions very easily by using</p>
  
  <pre><code>y_pred = svc.predict(X_test)
  </code></pre>
  
  <p>I was wondering how one obtains this result by calculating it directly. Starting with the decision function,
  <a href="https://i.stack.imgur.com/MoSx3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MoSx3.png" alt="Decision function of the RBF kernel"></a>
  where <code>K</code> is the RBF kernel function, <code>b</code> is the intercept and the alpha's are the dual coefficients.</p>
  
  <p>Because I work with the RBF-kernel, I started like this:</p>
  
  <pre><code>def RBF(x,z,gamma,axis=None):
      return np.exp((-gamma*np.linalg.norm(x-z, axis=axis)**2))
  
  
  for i in len(svc.support_):
      A[i] = RBF(X_train[i], X_test[0], 0.7)
  </code></pre>
  
  <p>Then I calculated</p>
  
  <pre><code>np.sum(svc._dual_coef_*A)+svc.intercept_
  </code></pre>
  
  <p>However, the result of this calculation isn't the same as the first term of <code>y_pred</code>. I suspect my reasoning isn't entirely correct and/or my code isn't what it should be, so apologies if this isn't the right board to ask. I've been staring blind at this problem for the past 2 hours, so any help would be greatly appreciated!</p>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>After some more research, I've found the following posts: <a href="https://stackoverflow.com/questions/28593684/replication-of-scikit-svm-srv-predictx?rq=1">Replication of scikit.svm.SRV.predict(X)</a> and <a href="https://stackoverflow.com/questions/28503932/calculating-decision-function-of-svm-manually?noredirect=1&amp;lq=1">Calculating decision function of SVM manually</a>.
  In the first post, they talk about regression, in the second about classification, but the idea stays the same. In both cases, the OP is basically asking the same thing, but when I try to implement their code, I always run into an error in the step</p>
  
  <pre><code>diff = sup_vecs - X_test
  </code></pre>
  
  <p>of the form</p>
  
  <pre><code>ValueError: operands could not be broadcast together with shapes 
  
  (number equal to amount of support vectors,7) (number equal to len(Xtest),7)
  </code></pre>
  
  <p>I don't understand why the amount of the support vectors should be equal to the amount of test data. From what I understand, that's almost never the case. So can anyone shine some light on how one should approach this problem more generally, i.e. how one can improve the code so that it works for multi-dimensional arrays?</p>
  
  <p>P.S. Not relevant to the problem, but just to be precise: 7 is the number of features.</p>