[scikit-learn] Fwd: Custom transformer failing check_estimator test

Sam Barnett sambarnett95 at gmail.com
Mon Jul 24 08:57:25 EDT 2017


Dear scikit-learn developers,

I am developing a transformer, named Sqizer, that has the ultimate goal of
modifying a kernel for use with the sklearn.svm package. When given an
input data array X, Sqizer.transform(X) should have as its output the Gram
matrix for X using the modified version of the kernel. Here is the code for
the class so far:

class Sqizer(BaseEstimator, TransformerMixin):

    def __init__(self, C=1.0, kernel='rbf', degree=3, gamma=1,
                     coef0=0.0, cut_ord_pair=(2,1)):
            self.C = C
            self.kernel = kernel
            self.degree = degree
            self.gamma = gamma
            self.coef0 = coef0
            self.cut_ord_pair = cut_ord_pair

    def fit(self, X, y=None):
        # Check that X and y have correct shape
        X, y = check_X_y(X, y)
        # Store the classes seen during fit
        self.classes_ = unique_labels(y)

        self.X_ = X
        self.y_ = y
        return self

    def transform(self, X):

        X = check_array(X, warn_on_dtype=True)

        """Returns Gram matrix corresponding to X, once sqized."""
        def kPolynom(x,y):
            return (self.coef0+self.gamma*np.inner(x,y))**self.degree
        def kGauss(x,y):
            return np.exp(-self.gamma*np.sum(np.square(x-y)))
        def kLinear(x,y):
            return np.inner(x,y)
        def kSigmoid(x,y):
            return np.tanh(self.gamma*np.inner(x,y) +self.coef0)

        def kernselect(kername):
            switcher = {
                'linear': kPolynom,
                'rbf': kGauss,
                'sigmoid': kLinear,
                'poly': kSigmoid,
                    }
            return switcher.get(kername, "nothing")

        cut_off = self.cut_ord_pair[0]
        order = self.cut_ord_pair[1]

        from SeqKernel import hiSeqKernEval

        def getGram(Y):
            gram_matrix = np.zeros((Y.shape[0], Y.shape[0]))
            for row1ind in range(Y.shape[0]):
                for row2ind in range(X.shape[0]):
                    gram_matrix[row1ind,row2ind] = \

hiSeqKernEval(Y[row1ind],Y[row2ind],kernselect(self.kernel),\
                    cut_off,order)
            return gram_matrix

        return getGram(X)

However, when I run the check_estimator method on Sqizer, I get an
error with the following check:

# raises error on malformed input for transformif hasattr(X, 'T'):
    # If it's not an array, it does not have a 'T' property
    assert_raises(ValueError, transformer.transform, X.T)

How do I alter my code to pass this test? Could my estimator trip up
on any further tests?

I have attached the relevant .py files if you require a bigger
picture. This particular snippet comes from the OptimalKernel.py file.



Many thanks,

Sam Barnett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170724/ca0a186d/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OptimalKernel.py
Type: text/x-python-script
Size: 8516 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170724/ca0a186d/attachment-0003.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SeqKernel.py
Type: text/x-python-script
Size: 4199 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170724/ca0a186d/attachment-0004.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TensorTools.py
Type: text/x-python-script
Size: 983 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170724/ca0a186d/attachment-0005.bin>


More information about the scikit-learn mailing list