I have started to use scikit learn for text extraction. When I use standard function CountVectorizer and TfidfTransformer in a pipeline and when I try to combine with new features ( a concatention of matrix) I have got a row dimension problem.
This is my pipeline:
pipeline = Pipeline([('feats', FeatureUnion([
('ngram_tfidf', Pipeline([('vect', CountVectorizer()),'tfidf', TfidfTransformer())])),
('addned', AddNed()),])), ('clf', SGDClassifier()),])
This is my class AddNEd which add 30 news features on each documents (sample).
class AddNed(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def transform (self, X, **transform_params):
do_something
x_new_feat = np.array(list_feat)
print(type(X))
X_np = np.array(X)
print(X_np.shape, x_new_feat.shape)
return np.concatenate((X_np, x_new_feat), axis = 1)
def fit(self, X, y=None):
return self
And the first part of my main programm
data = load_files('HO_without_tag')
grid_search = GridSearchCV(pipeline, parameters, n_jobs = 1, verbose = 20)
print(len(data.data), len(data.target))
grid_search.fit(X, Y).transform(X)
But I get this result:
486 486
Fitting 3 folds for each of 3456 candidates, totalling 10368 fits
[CV]feats__ngram_tfidf__vect__max_features=3000....
323
<class 'list'>
(323,) (486, 30)
And of course a Indexerror Exception
return np.concatenate((X_np, x_new_feat), axis = 1)
IndexError: axis 1 out of bounds [0, 1
When I have the params X in transform function (class AddNed) why I don't have a numpy array (486, 3000) shape for X. I have only (323,) shape. I don't understand because if I delete Feature Union and AddNed() pipeline, CountVectorizer and tf_idf work properly with the right features and the right shape. If anyone have an idea? Thanks a lot.
OK, I will try to give more explication. When I say do_something, I say do_nothing with X. In the class AddNed if I rewrite :
In this transform case above, I do not concatenate X matrix and new matrix. I presume features union do that... And my result:
To go further, just to see, if if I put a cross validation on gridsearchCV, just to modify sample size:
I have this result:
Of course if it is necessary, I can give all the code of do_nothing_withX. But what I don't understand, it is why sample size with the pipeline countvectorizer+tdf_idf it is not equal to the number of files load with sklearn.datasets.load_files() function.
You've probably solved it by now, but someone else may have the same problem:
AddNed
tries to concatenate a matrix with a sparse matrix, the sparse matrix should be transformed to dense matrix first. I've found the same error trying to use the result ofCountVectorizer