1

我有一个如下所示的数据框:

                                    description      priority  CDT  JDT  
0  Create Help Index Fails with seemingly incorre...       P3    0    0       
1  Internal compiler error when compiling switch ...       P3    0    1       
2  Default text sizes in org.eclipse.jface.resour...       P3    0    0       
3  [Presentations] [ViewMgmt] Holding mouse down ...       P3    0    0       
4  Parsing of function declarations in stdio.h is...       P2    1    0       

PDE  Platform  Web Tools  priorityLevel  
0         0          0              2  
1         0          0              2  
2         1          0              2  
3         1          0              2  
4         0          0              1  

我目前正在尝试训练一种机器学习算法,它将文本与除(丢弃)和(真实标签)'description'之外的其余数字特征一起纳入。'priority''priorityLevel'

这基本上是一个 NLP 应用程序。我遇到的问题是'description'必须首先通过一个'CountVectorizer()'函数:

X = df['description'] cv = CountVectorizer() X = cv.fit_transform(X)

当我将其传递给训练算法时,返回的输出与数据帧的其余部分不兼容。

我需要能够X在它被矢量化后组合df[['CDT', 'JDT', 'PDE', 'Platform', 'Web Tools']]成一个变量,以便拆分和训练:

X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2,random_state=101)

nb = MultinomialNB() nb.fit(X_train, y_train)

本质上,X应该包含矢量化文本以及数值变量。迄今为止所有的努力都失败了。

我也尝试过通过管道进行操作:

pipeline = Pipeline([ ('bow', CountVectorizer()), # strings to token integer counts. ('classifier', MultinomialNB()), ])

pipeline.fit(X_train,y_train)

但我收到错误,表明尺寸不兼容。

有谁知道一种更简单的方法来完成将矢量化器返回的稀疏矩阵与数值矩阵一起,以便我可以训练算法?

感谢所有帮助。

编辑:

我只使用矢量化文本训练了这个算法,没有任何问题。尝试将其他功能合并到训练集中时出现了我的问题。

4

1 回答 1

1

根据您的代码,您可以通过以下方式计算文本信息的词频,CountVectorizer()
但是当您这样调用代码时:

X = cv.fit_transform(X)

您将获得类型的数据<'scipy.sparse.csr.csr_matrix'>,而不是<'numpy.ndarray'>. 所以当你做数据融合的时候,可能会出现问题。
您可以使用此代码获取类型的数据<'numpy.ndarray'>

X = cv.fit_transform(X).toarray()

数据如下所示:

print X
[[1 1 0 0 1]
 [1 0 0 1 1]
 [1 0 1 0 1]]
print type(x)
<type 'numpy.ndarray'>
于 2017-09-01T02:02:32.027 回答