I am trying to create a simple question classifier using Scikit-learn. Currently I am able to classify question into corresponding classes using bag of word approach using countvectorizer function of Scikit. Now I want to create and add custom features with existing features generated with countvectorizer.
Suppose i want to create feature which checks if phone number is present in question or not and another feature which will extract length of question.
So what is the way to generate and merge all features together.
From this link i tried this template for custom feature extraction
`from sklearn.base import BaseEstimator, TransformerMixin
class SampleExtractor(BaseEstimator, TransformerMixin):
def __init__(self, vars):
self.vars = vars # e.g. pass in a column name to extract
def transform(self, X, y=None):
return do_something_to(X, self.vars) # where the actual feature extraction happens
def fit(self, X, y=None):
return self # generally does nothing`
but when when i put its output in pipeline with countvectorizer like
ppl = Pipeline([
('feats', FeatureUnion([
('ngram', CountVectorizer()), # can pass in either a pipeline
('ave', SampleExtractor()) # or a transformer
])),
('clf', LinearSVC()) # classifier
])
i get error
ValueError: blocks[0,:] has incompatible row dimensions
I think this error may be due to matrix of both feature are of not same dimension but I don't understand how to resolve it.
SampleExtractor()transform method reduce the number of rows? If so, that is not an acceptable transformation to put into a pipeline since only X is transformed, not y (and both X and y must have the same number of rows). - David