我们正在开展一项数据挖掘项目,并使用R语言中的tm软件包中的removeSparseTerms函数来减少文档-词项矩阵的特征。
然而,我们希望将代码移植到Python上。在sklearn、nltk或其他软件包中是否有类似的功能呢?
谢谢!
我们正在开展一项数据挖掘项目,并使用R语言中的tm软件包中的removeSparseTerms函数来减少文档-词项矩阵的特征。
然而,我们希望将代码移植到Python上。在sklearn、nltk或其他软件包中是否有类似的功能呢?
谢谢!
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=2)
corpus = [
'This is the first document.',
'This is the second second document.',
'And the third one.',
'Is this the first document?',
]
vectorizer = vectorizer.fit(corpus)
print vectorizer.vocabulary_
#prints {u'this': 4, u'is': 2, u'the': 3, u'document': 0, u'first': 1}
X = vectorizer.transform(corpus)
X
是文档-术语矩阵。
(如果您涉足信息检索,还要考虑 Tf-idf术语加权。)MIN_VAL_ALLOWED
,然后执行以下操作:import numpy as np
from scipy.sparse import csr_matrix
MIN_VAL_ALLOWED = 2
X = csr_matrix([[7,8,0],
[2,1,1],
[5,5,0]])
z = np.squeeze(np.asarray(X.sum(axis=0) > MIN_VAL_ALLOWED)) #z is the non-sparse terms
print X[:,z].toarray()
#prints X without the third term (as it is sparse)
[[7 8]
[2 1]
[5 5]]
如果您想对文档频率进行最小阈值设置,请先将矩阵二值化,然后以相同的方式使用它:
(使用X = X [:, z]
,使X
保持为csr_matrix
。)
import numpy as np
from scipy.sparse import csr_matrix
MIN_DF_ALLOWED = 2
X = csr_matrix([[7, 1.3, 0.9, 0],
[2, 1.2, 0.8 , 1],
[5, 1.5, 0 , 0]])
#Creating a copy of the data
B = csr_matrix(X, copy=True)
B[B>0] = 1
z = np.squeeze(np.asarray(X.sum(axis=0) > MIN_DF_ALLOWED))
print X[:,z].toarray()
#prints
[[ 7. 1.3]
[ 2. 1.2]
[ 5. 1.5]]
MIN_DF_ALLOWED
来设置阈值。