From ValueError: Unknown label type: 'unknown' on y_train
I guess you have some unsupported/invalid data type in your y_train
or the type is indeed valid but does not match with the RandomForestClassifier
expectation.
Please do the followings:
- print/plot the
y_train
and make sure all values make sense. There is a fair chance thatHashingVectorizer
could not hash some special cases (eg foreign language characters or missing values). - use
type
ordtype
to find out thev_train
data type. Make sure it is consistent withRandomForestClassifier
expectation.
p.s. as @outlier mentioned, we usually do not transform our labels (y_train
in your case). Many classifiers can handle strings/letters as class labels. In case they cannot, we can use a simple mapping such as P -> 0, Q -> 1, etc. The following does the job for you:
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y)