我对二进制文本分类的任务的工作,我已经如下应用在我的数据向量化:错误合并两个二维数组时零维数组不能被串联
count_vect = CountVectorizer(tokenizer=tokens)
X_train_counts = count_vect.fit_transform(docs_train.data)
print X_train_counts.shape
(150, 370)
因为我想只需要从类中随机抽取“0”(一个在我的例子),并以一流的归类“1”,我做了以下内容:
x = X_train_counts
y = docs_train.target
a_x,a_y=x[y==0,:],y[y==0]
b_x,b_y=x[y==1,:],y[y==1]
inds=np.random.choice(range(a_x.shape[0]),50)
random_x=a_x[inds,:]
random_y=a_y[inds]
x_merged=np.concatenate((random_x,b_x))
y_merged=np.concatenate((random_y,b_y))
X_train,y_train=shuffle(x_merged, y_merged, random_state=0)
但我总是得到以下错误:
x_merged=np.concatenate((random_x,b_x))
ValueError: zero-dimensional arrays cannot be concatenated
虽然当我打印的形状也gaves我:
print random_x.shape
print b_x.shape
(50, 370)
(50, 370)
任何想法如何解决呢?当然它会保留与标签链接的索引。
更新: 这是内容/类型每个阵列的打印时,下面的命令执行:
print random_x[:5,:].toarray()
print b_x[:5,:].toarray()
print (type(random_x))
print (type(b_x))
[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[4 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
<class 'scipy.sparse.csr.csr_matrix'>
<class 'scipy.sparse.csr.csr_matrix'>
打印了一下两个数组的,所以我们可以看到什么在里面。请张贴确切的印刷品。我认为它不是关于形状,而是关于类型或尺寸。 – armatita
我已经更新了每个数组的内容/类型的问题......谢谢 – Ophilia
这是它与concatenate函数不兼容的类型。我已经给出了一个正式的答案来解决这个问题(嗯,我想)。 – armatita