【发布时间】:2017-06-20 17:18:12
【问题描述】:
我正在对一些已提取特征并在文本文件中给出的文档进行二进制分类。我的问题是存在文本特征和数字特征,例如年份等。 以这种格式给出了一个示例:
label |title text |otherText text |numFeature1 number |numFeature2 number
我正在关注有关 feature unions 的文档,但它们的用例有点不同。我没有从另一个特征中提取特征,因为这些数字特征已经给出。
目前我正在通过以下方式使用设置:
pipeline = Pipeline([
('features', Features()),
('union', FeatureUnion(
transformer_list=[
('title', Pipeline([
('selector', ItemSelector(key='title')),
('tfidf', TfidfVectorizer()),
])),
('otherText', Pipeline([
('selector', ItemSelector(key='otherText')),
('tfidf', TfidfVectorizer()),
])),
('numFeature1', Pipeline([
('selector', ItemSelector(key='numFeature1')),
])),
('numFeature2', Pipeline([
('selector', ItemSelector(key='numFeature2')),
])),
],
)),
('classifier', MultinomialNB()),
])
Feature 类也是从文档中采用的:
class Features(BaseEstimator, TransformerMixin):
def fit(self, x, y=None):
return self
def transform(self, posts):
features = np.recarray(shape=(len(posts),),
dtype=[('title', object),('otherText', object),
('numFeature1', object),('numFeature2', object)])
for i, text in enumerate(posts):
l = re.split("\|\w+", text)
features['title'][i] = l[1]
features['otherText'][i] = l[2]
features['numFeature1'][i] = l[3]
features['numFeature2'][i] = l[4]
return features
我现在的问题是:如何将数字特征添加到 FeatureUnion?使用 CountVectorizer 时,我得到“ValueError:空词汇;也许文档只包含停用词”,并且使用只有一个条目的 DictVectorizer 并没有让我觉得要走的路。
【问题讨论】:
-
只需使用 ItemSelector() 类和 key='numFeature1' 和 'numFeature2'
-
返回
ValueError: blocks[0,:] has incompatible row dimensions -
显示整个管道的代码。
-
我已经相应地编辑了我的问题
标签: python scikit-learn feature-extraction