[scikit-learn] Malformed input for SVC(kernel='precomputed').predict()

Andreas Mueller t3kcit at gmail.com
Thu Aug 17 11:03:16 EDT 2017

Hi Sam.

Can you say which test fails exactly and where (i.e. give traceback)?
The estimator checks are currently quite strict with respect to raising 
helpful error messages.
That doesn't mean your estimator is broken (necessarily).
With a precomputed gram matrix, I expect the shape of X in predict to be 
(n_samples_test, n_samples_train), right?
Does you estimator have a _pairwise attribute? (It should to work with 
cross-validation, I'm not sure if it's
used in the estimator checks right now, but it should).

Your feedback will help making check_estimator be more robust. I don't 
think it's tested with anything that requires
"precomputed" kernels.



On 08/17/2017 05:22 AM, Sam Barnett wrote:
> I am rolling classifier based on SVC which computes a custom Gram 
> matrix and runs this through the SVC classifier with kernel = 
> 'precomputed'. While this works fine with the fit method, I face a 
> dilemma with the predict method, shown here:
> def predict(self, X):
> """Run the predict method of the previously-instantiated SVM
>       classifier, returning the predicted classes for test set X."""
> # Check is fit had been called
> check_is_fitted(self, ['X_', 'y_'])
>         # Input validation
>         X = check_array(X)
>         cut_off = self.cut_ord_pair[0]
>         order = self.cut_ord_pair[1]
>         X_gram = seq_kernel_free(X, self.X_, \
> pri_kernel=kernselect(self.kernel, self.coef0, self.gamma, 
> self.degree, self.scale), \
> cut_off=cut_off, order=order)
>         X_gram = np.nan_to_num(X_gram)
>         return self.ord_svc_.predict(X_gram)
> This will run on any dataset just fine. However, it fails the 
> check_estimator test. Specifically, when trying to raise an error for 
> malformed input on predict (in check_classifiers_train), it says that 
> a ValueError is not raised. Yet if I change the order of X and self.X_ 
> in seq_kernel_free (which computes the [n_samples_train, 
> n_samples_test] Gram matrix), it passes the check_estimator test yet 
> fails to run the predict method.
> How do I resolve both issues simultaneously?
> _______________________________________________
> scikit-learn mailing list
> scikit-learn at python.org
> https://mail.python.org/mailman/listinfo/scikit-learn

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170817/c0f8fee3/attachment.html>

More information about the scikit-learn mailing list