0
$\begingroup$

I'm trying to run a grid CV parameter search using sklearn.model_selection.GridSearchCV. I keep getting a ValueError that is really confusing me. Below I've included the code for the pipeline I created, which includes a TfidfVectorizer and a RandomForestClassifier. I used train_test_split to separate the features and target, and tried to fit the grid search with the pipeline. Here are my results.

from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import TfidfVectorizer

features = ['id', 'description']
target = 'ratingCategory'
x_train, x_val, y_train, y_val = train_test_split(
    train[features],
    train[target],
    test_size=0.2,
    stratify=train[target],
    random_state=95
)

vect = TfidfVectorizer()
clf = RandomForestClassifier()

pipe = Pipeline([
    ('vect', vect), 
    ('clf', clf)]
)

parameters = {
    'vect__min_df': (0.01, 0.05),
    'clf__ccp_alpha': (0.1, 0.5)
}

grid_search = GridSearchCV(pipe, parameters, cv=5, n_jobs=4, verbose=1)
grid_search.fit(X=x_train, y=y_train)

Checking the shape of the matrices confirms that x_train and y_train have the same length (the number of rows in both = 3269). So I'm confused as to why fitting the grid search gives me the following error:

Fitting 5 folds for each of 4 candidates, totalling 20 fits
[Parallel(n_jobs=4)]: Using backend LokyBackend with 4 concurrent workers.
[Parallel(n_jobs=4)]: Done  20 out of  20 | elapsed:    2.0s finished
--------------------
ValueErrorTraceback (most recent call last)
<ipython-input-18-4e85850d6599> in <module>
      29 grid_search = GridSearchCV(pipe, parameters, cv=5, n_jobs=4, verbose=1)
----> 30 grid_search.fit(X=x_train, y=y_train)

........................................

ValueError: Number of labels=3269 does not match number of samples=2

What does it mean by number of labels and samples? There should be 3269 samples, since the shape of both X and y matrices is (3269, 2) and (3269, ), respectively.

Any help is super appreciated! Let me know if the full traceback would help, but it was extremely long so I didn't include it.

$\endgroup$
1
  • $\begingroup$ First thing to note, you are passing 2 columns: id and description to TfidfVectorizer, when you should pass only a text column (description) $\endgroup$
    – Multivac
    Commented Jul 15, 2021 at 2:42

1 Answer 1

0
$\begingroup$

Try:

from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import TfidfVectorizer

# features = ['description']
# target = 'ratingCategory'
x_train, x_val, y_train, y_val = train_test_split(
    # train[features],
    train.description,
    # train[target],
    train.ratingCategory,
    test_size=0.2,
    # stratify=train[target],
    stratify=train.ratingCategory,
    random_state=95
)

vect = TfidfVectorizer()
clf = RandomForestClassifier()

pipe = Pipeline([
    ('vect', vect), 
    ('clf', clf)]
)

parameters = {
    'vect__min_df': (0.01, 0.05),
    'clf__ccp_alpha': (0.1, 0.5)
}

grid_search = GridSearchCV(pipe, parameters, cv=5, n_jobs=4, verbose=1)
grid_search.fit(X=x_train, y=y_train)
$\endgroup$
4
  • $\begingroup$ I thought it might be that too, but when I make the change the error is the same, only this time it says: ValueError: Number of labels=3269 does not match number of samples=1 $\endgroup$ Commented Jul 15, 2021 at 3:31
  • $\begingroup$ Please try with the edit I made $\endgroup$
    – Multivac
    Commented Jul 15, 2021 at 12:30
  • $\begingroup$ Great! I've been going crazy, thank you! Could you explain to me why this method worked? $\endgroup$ Commented Jul 15, 2021 at 21:33
  • 1
    $\begingroup$ Sure, I had the same issue long ago and I realize that TFIDFVectorizer expects a pd.Series not a DataFrame, when you select the "description" features as train[["description"]] this is a DataFrame whereas with train.description is a series (what the vectorizer expects) $\endgroup$
    – Multivac
    Commented Jul 15, 2021 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.