This is perhaps more of a coding question than data science so apologies if this is not the right platform to ask this.
My question is related to the sklearn
's ColumnTransformer
class.
Considering 'description'
corresponds to a text/string data column with 9508 rows. The following works as expected:
In [61]: transformer = ColumnTransformer(transformers=[('text-features', CountVectorizer(), 'description')])
In [62]: X=transformer.fit_transform(df)
In [63]: X.shape
Out[63]: (9508, 5913)
However, the following (note the []
around 'description'
):
In [64]: transformer = ColumnTransformer(transformers=[('text-features', CountVectorizer(), ['description'])])
In [65]: X=transformer.fit_transform(df)
does not work as expected:
In [66]: X.shape
Out[66]: (1, 3)
Note that there is no issue parsing the list of column names for other transformers such as OneHotEncoder()
. This is making it difficult to programmatically configure transformers for dataframes containing heterogenous data type columns.
One possibility is to configure transformer for each column individually even if multiple columns require the same transformer but I was wondering if there is better way to deal with this?
CountVectorizer
accepts "An iterable which yields either str, unicode or file objects", but when you pass a list of columns toColumnTransformer
, an actual pandas.DataFrame is fed toCountVectorizer.fit
. So I guess you will have to copy the transformation for each column. $\endgroup$