Fwd: Custom transformer failing check_estimator test
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
what is the failing test? please provide the full traceback. On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com> wrote:
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
_______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
...
This is the Traceback I get: AssertionErrorTraceback (most recent call last) <ipython-input-5-166b8f0141db> in <module>() ----> 1 check_estimator(OK.Sqizer) /Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_ checks.pyc in check_estimator(Estimator) 253 check_parameters_default_constructible(name, Estimator) 254 for check in _yield_all_checks(name, Estimator): --> 255 check(name, Estimator) 256 257 /Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs) 353 with warnings.catch_warnings(): 354 warnings.simplefilter("ignore", self.category) --> 355 return fn(*args, **kwargs) 356 357 return wrapper /Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_transformer_general(name, Transformer) 578 X = StandardScaler().fit_transform(X) 579 X -= X.min() --> 580 _check_transformer(name, Transformer, X, y) 581 _check_transformer(name, Transformer, X.tolist(), y.tolist()) 582 /Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in _check_transformer(name, Transformer, X, y) 671 if hasattr(X, 'T'): 672 # If it's not an array, it does not have a 'T' property --> 673 assert_raises(ValueError, transformer.transform, X.T) 674 675 /Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs) 471 return context 472 with context: --> 473 callableObj(*args, **kwargs) 474 475 def _getAssertEqualityFunc(self, first, second): /Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb) 114 exc_name = str(self.expected) 115 raise self.failureException( --> 116 "{0} not raised".format(exc_name)) 117 if not issubclass(exc_type, self.expected): 118 # let unexpected exceptions pass through AssertionError: ValueError not raised On Tue, Jul 25, 2017 at 12:54 AM, Joel Nothman <joel.nothman@gmail.com> wrote:
what is the failing test? please provide the full traceback.
On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com> wrote:
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.
...
[Message clipped] _______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
Apologies: I've since worked out what the problem was and have resolved this issue. This was what I was missing in my code: # Check that the input is of the same shape as the one passed # during fit. if X.shape != self.input_shape_: raise ValueError('Shape of input is different from what was seen' 'in `fit`') On Tue, Jul 25, 2017 at 9:41 AM, Sam Barnett <sambarnett95@gmail.com> wrote:
This is the Traceback I get:
AssertionErrorTraceback (most recent call last) <ipython-input-5-166b8f0141db> in <module>() ----> 1 check_estimator(OK.Sqizer)
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/ utils/estimator_checks.pyc in check_estimator(Estimator) 253 check_parameters_default_constructible(name, Estimator) 254 for check in _yield_all_checks(name, Estimator): --> 255 check(name, Estimator) 256 257
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs) 353 with warnings.catch_warnings(): 354 warnings.simplefilter("ignore", self.category) --> 355 return fn(*args, **kwargs) 356 357 return wrapper
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_transformer_general(name, Transformer) 578 X = StandardScaler().fit_transform(X) 579 X -= X.min() --> 580 _check_transformer(name, Transformer, X, y) 581 _check_transformer(name, Transformer, X.tolist(), y.tolist()) 582
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in _check_transformer(name, Transformer, X, y) 671 if hasattr(X, 'T'): 672 # If it's not an array, it does not have a 'T' property --> 673 assert_raises(ValueError, transformer.transform, X.T) 674 675
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs) 471 return context 472 with context: --> 473 callableObj(*args, **kwargs) 474 475 def _getAssertEqualityFunc(self, first, second):
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb) 114 exc_name = str(self.expected) 115 raise self.failureException( --> 116 "{0} not raised".format(exc_name)) 117 if not issubclass(exc_type, self.expected): 118 # let unexpected exceptions pass through
AssertionError: ValueError not raised
On Tue, Jul 25, 2017 at 12:54 AM, Joel Nothman <joel.nothman@gmail.com> wrote:
what is the failing test? please provide the full traceback.
On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com> wrote:
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.
...
[Message clipped] _______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
Indeed, it makes sure that the transform is applied to data with the same number of samples as the input. PR welcome to provide a better error message on this! On 07/25/2017 08:15 AM, Sam Barnett wrote:
Apologies: I've since worked out what the problem was and have resolved this issue. This was what I was missing in my code:
# Check that the input is of the same shape as the one passed # during fit. if X.shape != self.input_shape_: raise ValueError('Shape of input is different from what was seen' 'in `fit`')
On Tue, Jul 25, 2017 at 9:41 AM, Sam Barnett <sambarnett95@gmail.com <mailto:sambarnett95@gmail.com>> wrote:
This is the Traceback I get:
AssertionErrorTraceback (most recent call last) <ipython-input-5-166b8f0141db> in <module>() ----> 1 check_estimator(OK.Sqizer)
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_estimator(Estimator) 253 check_parameters_default_constructible(name, Estimator) 254 for check in _yield_all_checks(name, Estimator): --> 255 check(name, Estimator) 256 257
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs) 353 with warnings.catch_warnings(): 354 warnings.simplefilter("ignore", self.category) --> 355 return fn(*args, **kwargs) 356 357 return wrapper
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_transformer_general(name, Transformer) 578 X = StandardScaler().fit_transform(X) 579 X -= X.min() --> 580 _check_transformer(name, Transformer, X, y) 581 _check_transformer(name, Transformer, X.tolist(), y.tolist()) 582
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in _check_transformer(name, Transformer, X, y) 671 if hasattr(X, 'T'): 672 # If it's not an array, it does not have a 'T' property --> 673 assert_raises(ValueError, transformer.transform, X.T) 674 675
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs) 471 return context 472 with context: --> 473 callableObj(*args, **kwargs) 474 475 def _getAssertEqualityFunc(self, first, second):
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb) 114 exc_name = str(self.expected) 115 raise self.failureException( --> 116 "{0} not raised".format(exc_name)) 117 if not issubclass(exc_type, self.expected): 118 # let unexpected exceptions pass through
AssertionError: ValueError not raised
On Tue, Jul 25, 2017 at 12:54 AM, Joel Nothman <joel.nothman@gmail.com <mailto:joel.nothman@gmail.com>> wrote:
what is the failing test? please provide the full traceback.
On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com <mailto:sambarnett95@gmail.com>> wrote:
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:
|classSqizer(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 deffit(self,X,y=None):# Check that X and y have correct shapeX,y =check_X_y(X,y)# Store the classes seen during fitself.classes_ =unique_labels(y)self.X_ =X self.y_ =y returnself deftransform(self,X):X =check_array(X,warn_on_dtype=True)"""Returns Gram matrix corresponding to X, once sqized."""defkPolynom(x,y):return(self.coef0+self.gamma*np.inner(x,y))**self.degree defkGauss(x,y):returnnp.exp(-self.gamma*np.sum(np.square(x-y)))defkLinear(x,y):returnnp.inner(x,y)defkSigmoid(x,y):returnnp.tanh(self.gamma*np.inner(x,y)+self.coef0)defkernselect(kername):switcher ={'linear':kPolynom,'rbf':kGauss,'sigmoid':kLinear,'poly':kSigmoid,}returnswitcher.get(kername,"nothing")cut_off =self.cut_ord_pair[0]order =self.cut_ord_pair[1]fromSeqKernelimporthiSeqKernEval defgetGram(Y):gram_matrix =np.zeros((Y.|
...
[Message clipped] _______________________________________________ scikit-learn mailing list scikit-learn@python.org <mailto:scikit-learn@python.org> https://mail.python.org/mailman/listinfo/scikit-learn <https://mail.python.org/mailman/listinfo/scikit-learn>
_______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
One advantage of moving to pytest is that we can put messages into pytest.raises, and we should emphasise this in moving the check_estimator assertions to pytest. But I'm also not sure how we do the deprecation of nosetests for check_estimator in a way that is friendly to our contribbers... On 26 July 2017 at 06:31, Andreas Mueller <t3kcit@gmail.com> wrote:
Indeed, it makes sure that the transform is applied to data with the same number of samples as the input. PR welcome to provide a better error message on this!
On 07/25/2017 08:15 AM, Sam Barnett wrote:
Apologies: I've since worked out what the problem was and have resolved this issue. This was what I was missing in my code:
# Check that the input is of the same shape as the one passed # during fit. if X.shape != self.input_shape_: raise ValueError('Shape of input is different from what was seen' 'in `fit`')
On Tue, Jul 25, 2017 at 9:41 AM, Sam Barnett <sambarnett95@gmail.com> wrote:
This is the Traceback I get:
AssertionErrorTraceback (most recent call last) <ipython-input-5-166b8f0141db> in <module>() ----> 1 check_estimator(OK.Sqizer)
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/util s/estimator_checks.pyc in check_estimator(Estimator) 253 check_parameters_default_constructible(name, Estimator) 254 for check in _yield_all_checks(name, Estimator): --> 255 check(name, Estimator) 256 257
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs) 353 with warnings.catch_warnings(): 354 warnings.simplefilter("ignore", self.category) --> 355 return fn(*args, **kwargs) 356 357 return wrapper
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_transformer_general(name, Transformer) 578 X = StandardScaler().fit_transform(X) 579 X -= X.min() --> 580 _check_transformer(name, Transformer, X, y) 581 _check_transformer(name, Transformer, X.tolist(), y.tolist()) 582
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in _check_transformer(name, Transformer, X, y) 671 if hasattr(X, 'T'): 672 # If it's not an array, it does not have a 'T' property --> 673 assert_raises(ValueError, transformer.transform, X.T) 674 675
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs) 471 return context 472 with context: --> 473 callableObj(*args, **kwargs) 474 475 def _getAssertEqualityFunc(self, first, second):
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb) 114 exc_name = str(self.expected) 115 raise self.failureException( --> 116 "{0} not raised".format(exc_name)) 117 if not issubclass(exc_type, self.expected): 118 # let unexpected exceptions pass through
AssertionError: ValueError not raised
On Tue, Jul 25, 2017 at 12:54 AM, Joel Nothman <joel.nothman@gmail.com> wrote:
what is the failing test? please provide the full traceback.
On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com> wrote:
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.
...
[Message clipped] _______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
_______________________________________________ scikit-learn mailing listscikit-learn@python.orghttps://mail.python.org/mailman/listinfo/scikit-learn
_______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
Hm, it would be nice to do this in a way that relies less on pytest, but I guess that would be tricky. One way would be to use assert_raise_message to make clear what the expected error is. But that would make the current test more strict - not necessarily that bad, I guess? It looks like all asserts in unittest have a "msg" argument... apart from assertRaises: https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRais... That has been fixed in Python 3.3, though: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRais... So maybe we should just do a backport for assert_raises and assert_raises_regex? On 07/25/2017 07:58 PM, Joel Nothman wrote:
One advantage of moving to pytest is that we can put messages into pytest.raises, and we should emphasise this in moving the check_estimator assertions to pytest. But I'm also not sure how we do the deprecation of nosetests for check_estimator in a way that is friendly to our contribbers...
On 26 July 2017 at 06:31, Andreas Mueller <t3kcit@gmail.com <mailto:t3kcit@gmail.com>> wrote:
Indeed, it makes sure that the transform is applied to data with the same number of samples as the input. PR welcome to provide a better error message on this!
On 07/25/2017 08:15 AM, Sam Barnett wrote:
Apologies: I've since worked out what the problem was and have resolved this issue. This was what I was missing in my code:
# Check that the input is of the same shape as the one passed # during fit. if X.shape != self.input_shape_: raise ValueError('Shape of input is different from what was seen' 'in `fit`')
On Tue, Jul 25, 2017 at 9:41 AM, Sam Barnett <sambarnett95@gmail.com <mailto:sambarnett95@gmail.com>> wrote:
This is the Traceback I get:
AssertionErrorTraceback (most recent call last) <ipython-input-5-166b8f0141db> in <module>() ----> 1 check_estimator(OK.Sqizer)
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_estimator(Estimator) 253 check_parameters_default_constructible(name, Estimator) 254 for check in _yield_all_checks(name, Estimator): --> 255 check(name, Estimator) 256 257
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs) 353 with warnings.catch_warnings(): 354 warnings.simplefilter("ignore", self.category) --> 355 return fn(*args, **kwargs) 356 357 return wrapper
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_transformer_general(name, Transformer) 578 X = StandardScaler().fit_transform(X) 579 X -= X.min() --> 580 _check_transformer(name, Transformer, X, y) 581 _check_transformer(name, Transformer, X.tolist(), y.tolist()) 582
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in _check_transformer(name, Transformer, X, y) 671 if hasattr(X, 'T'): 672 # If it's not an array, it does not have a 'T' property --> 673 assert_raises(ValueError, transformer.transform, X.T) 674 675
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs) 471 return context 472 with context: --> 473 callableObj(*args, **kwargs) 474 475 def _getAssertEqualityFunc(self, first, second):
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb) 114 exc_name = str(self.expected) 115 raise self.failureException( --> 116 "{0} not raised".format(exc_name)) 117 if not issubclass(exc_type, self.expected): 118 # let unexpected exceptions pass through
AssertionError: ValueError not raised
On Tue, Jul 25, 2017 at 12:54 AM, Joel Nothman <joel.nothman@gmail.com <mailto:joel.nothman@gmail.com>> wrote:
what is the failing test? please provide the full traceback.
On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com <mailto:sambarnett95@gmail.com>> wrote:
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:
|classSqizer(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 deffit(self,X,y=None):# Check that X and y have correct shapeX,y =check_X_y(X,y)# Store the classes seen during fitself.classes_ =unique_labels(y)self.X_ =X self.y_ =y returnself deftransform(self,X):X =check_array(X,warn_on_dtype=True)"""Returns Gram matrix corresponding to X, once sqized."""defkPolynom(x,y):return(self.coef0+self.gamma*np.inner(x,y))**self.degree defkGauss(x,y):returnnp.exp(-self.gamma*np.sum(np.square(x-y)))defkLinear(x,y):returnnp.inner(x,y)defkSigmoid(x,y):returnnp.tanh(self.gamma*np.inner(x,y)+self.coef0)defkernselect(kername):switcher ={'linear':kPolynom,'rbf':kGauss,'sigmoid':kLinear,'poly':kSigmoid,}returnswitcher.get(kername,"nothing")cut_off =self.cut_ord_pair[0]order =self.cut_ord_pair[1]fromSeqKernelimporthiSeqKernEval defgetGram(Y):gram_matrix =np.zeros((Y.|
...
[Message clipped] _______________________________________________ scikit-learn mailing list scikit-learn@python.org <mailto:scikit-learn@python.org> https://mail.python.org/mailman/listinfo/scikit-learn <https://mail.python.org/mailman/listinfo/scikit-learn>
_______________________________________________ scikit-learn mailing list scikit-learn@python.org <mailto:scikit-learn@python.org> https://mail.python.org/mailman/listinfo/scikit-learn <https://mail.python.org/mailman/listinfo/scikit-learn>
_______________________________________________ scikit-learn mailing list scikit-learn@python.org <mailto:scikit-learn@python.org> https://mail.python.org/mailman/listinfo/scikit-learn <https://mail.python.org/mailman/listinfo/scikit-learn>
_______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
The difference is the functional form versus the context manager. you can't add extra parameters to the function, only to the context manager. On 27 Jul 2017 12:56 am, "Andreas Mueller" <t3kcit@gmail.com> wrote:
Hm, it would be nice to do this in a way that relies less on pytest, but I guess that would be tricky. One way would be to use assert_raise_message to make clear what the expected error is. But that would make the current test more strict - not necessarily that bad, I guess?
It looks like all asserts in unittest have a "msg" argument... apart from assertRaises: https://docs.python.org/2/library/unittest.html# unittest.TestCase.assertRaises
That has been fixed in Python 3.3, though: https://docs.python.org/3/library/unittest.html# unittest.TestCase.assertRaises
So maybe we should just do a backport for assert_raises and assert_raises_regex?
On 07/25/2017 07:58 PM, Joel Nothman wrote:
One advantage of moving to pytest is that we can put messages into pytest.raises, and we should emphasise this in moving the check_estimator assertions to pytest. But I'm also not sure how we do the deprecation of nosetests for check_estimator in a way that is friendly to our contribbers...
On 26 July 2017 at 06:31, Andreas Mueller <t3kcit@gmail.com> wrote:
Indeed, it makes sure that the transform is applied to data with the same number of samples as the input. PR welcome to provide a better error message on this!
On 07/25/2017 08:15 AM, Sam Barnett wrote:
Apologies: I've since worked out what the problem was and have resolved this issue. This was what I was missing in my code:
# Check that the input is of the same shape as the one passed # during fit. if X.shape != self.input_shape_: raise ValueError('Shape of input is different from what was seen' 'in `fit`')
On Tue, Jul 25, 2017 at 9:41 AM, Sam Barnett <sambarnett95@gmail.com> wrote:
This is the Traceback I get:
AssertionErrorTraceback (most recent call last) <ipython-input-5-166b8f0141db> in <module>() ----> 1 check_estimator(OK.Sqizer)
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/util s/estimator_checks.pyc in check_estimator(Estimator) 253 check_parameters_default_constructible(name, Estimator) 254 for check in _yield_all_checks(name, Estimator): --> 255 check(name, Estimator) 256 257
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs) 353 with warnings.catch_warnings(): 354 warnings.simplefilter("ignore", self.category) --> 355 return fn(*args, **kwargs) 356 357 return wrapper
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_transformer_general(name, Transformer) 578 X = StandardScaler().fit_transform(X) 579 X -= X.min() --> 580 _check_transformer(name, Transformer, X, y) 581 _check_transformer(name, Transformer, X.tolist(), y.tolist()) 582
/Users/Sam/anaconda/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in _check_transformer(name, Transformer, X, y) 671 if hasattr(X, 'T'): 672 # If it's not an array, it does not have a 'T' property --> 673 assert_raises(ValueError, transformer.transform, X.T ) 674 675
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs) 471 return context 472 with context: --> 473 callableObj(*args, **kwargs) 474 475 def _getAssertEqualityFunc(self, first, second):
/Users/Sam/anaconda/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb) 114 exc_name = str(self.expected) 115 raise self.failureException( --> 116 "{0} not raised".format(exc_name)) 117 if not issubclass(exc_type, self.expected): 118 # let unexpected exceptions pass through
AssertionError: ValueError not raised
On Tue, Jul 25, 2017 at 12:54 AM, Joel Nothman <joel.nothman@gmail.com> wrote:
what is the failing test? please provide the full traceback.
On 24 Jul 2017 10:58 pm, "Sam Barnett" <sambarnett95@gmail.com> wrote:
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(
scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn
...
participants (3)
-
Andreas Mueller -
Joel Nothman -
Sam Barnett