You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using (for example) the following transformators
CountVectorizer
TruncatedSVD
SelectKBest
with GridSearchCV it happens that it chooses a number for n_features for CountVectorizer that is less than n_components for TruncatedSVD or k for SelectKBest.
This leads to an error: ValueError: n_components must be < n_features
For SelectKBest I found a temporary solution:
class SelectAtMostKBest(SelectKBest):
def _check_params(self, X, y):
if not (self.k == "all" or 0 <= self.k <= X.shape[1]):
self.k = "all"
But there is no equivalent for TruncatedSVD.
Is this behaviour intended? If yes, what can I do about this?
Yes, this is a limitation of our API currently. One option is to use a parameter grid that only allows valid combinations, by using a list of parameter dicts wherein each setting is valid. A more robust forward-thinking solution might consider n_components and k optionally being functions of X and y.
@jnothman Thank you, can you please elaborate a little bit more on the function-based approach? I am also considering to use the RandomizedSearchCV, but will probably face the same problem there, right?
With randomized search you can't encode constraints between multiple
parameters, but you can use error_score=0.
The function-based approach would require someone changing the n_components
interface to allow it to be a function. It's not something we've generally
done, so it's probably on the scale of requiring an Enhancement Proposal to
suggest that this approach be used across the board.
You could potentially do something hacky like inherit from TruncatedSVD and
overwrite fit(X, y), such that it changes n_components depending on X.shape.
How about using SelectPercentile instead? Not sure if we allow covered variance in TruncatedSVD but it would be nice to have an option that is relative to the input feature size, not absolute.
Activity
jnothman commentedon Jan 30, 2017
Yes, this is a limitation of our API currently. One option is to use a parameter grid that only allows valid combinations, by using a list of parameter dicts wherein each setting is valid. A more robust forward-thinking solution might consider
n_componentsandkoptionally being functions ofXandy.retkowski commentedon Jan 30, 2017
@jnothman Thank you, can you please elaborate a little bit more on the function-based approach? I am also considering to use the RandomizedSearchCV, but will probably face the same problem there, right?
jnothman commentedon Jan 30, 2017
amueller commentedon Mar 4, 2017
How about using
SelectPercentileinstead? Not sure if we allow covered variance inTruncatedSVDbut it would be nice to have an option that is relative to the input feature size, not absolute.