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.