- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 pandas 系列,里面全是文字。使用 sklearn
包中的 CountVectorizer
函数,我计算了稀疏矩阵。我也确定了最重要的词。现在我想过滤我的稀疏矩阵,只包含那些排名靠前的词。
原始数据包含超过 7000
行,包含超过 75000
个单词。因此我在这里创建了一个样本数据
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
words = pd.Series(['This is first row of the text column',
'This is second row of the text column',
'This is third row of the text column',
'This is fourth row of the text column',
'This is fifth row of the text column'])
count_vec = CountVectorizer(stop_words='english')
sparse_matrix = count_vec.fit_transform(words)
我已经为该列中的所有单词创建了稀疏矩阵。这里只是为了打印我的稀疏矩阵,我使用 .toarray()
函数将其转换为数组。
print count_vec.get_feature_names()
print sparse_matrix.toarray()
[u'column', u'fifth', u'fourth', u'row', u'second', u'text']
[[1 0 0 1 0 1]
[1 0 0 1 1 1]
[1 0 0 1 0 1]
[1 0 1 1 0 1]
[1 1 0 1 0 1]]
现在我正在使用以下方法寻找经常出现的单词
# Get frequency count of all features
features_count = sparse_matrix.sum(axis=0).tolist()[0]
features_names = count_vec.get_feature_names()
features = pd.DataFrame(zip(features_names, features_count),
columns=['features', 'count']
).sort_values(by=['count'], ascending=False)
features count
0 column 5
3 row 5
5 text 5
1 fifth 1
2 fourth 1
4 second 1
从上面的结果我们知道出现频率最高的词是column
, row
& text
。现在我只想为这些词过滤我的稀疏矩阵。我不将我的稀疏矩阵转换为数组然后进行过滤。因为我的原始数据出现内存错误,因为字数非常多。
我能够获得稀疏矩阵的唯一方法是使用 vocabulary
属性再次重复这些特定单词的步骤,就像这样
countvec_subset = CountVectorizer(vocabulary= ['column', 'text', 'row'])
相反,我正在寻找一个更好的解决方案,我可以直接为这些词过滤稀疏矩阵,而不是从头开始重新创建它。
最佳答案
您可以对稀疏矩阵进行切片。您需要导出用于切片的列。 sparse_matrix[:, columns]
In [56]: feature_count = sparse_matrix.sum(axis=0)
In [57]: columns = tuple(np.where(feature_count == feature_count.max())[1])
In [58]: columns
Out[58]: (0, 3, 5)
In [59]: sparse_matrix[:, columns].toarray()
Out[59]:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], dtype=int64)
In [60]: type(sparse_matrix[:, columns])
Out[60]: scipy.sparse.csr.csr_matrix
In [71]: np.array(features_names)[list(columns)]
Out[71]:
array([u'column', u'row', u'text'],
dtype='<U6')
切片子集仍然是一个scipy.sparse.csr.csr_matrix
关于python - 仅从 sklearn CountVectorizer 稀疏矩阵中过滤某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454667/
我在服务器上 checkout 了一个 git 存储库。该存储库过去在根目录下包含所有相关文件,但我必须进行一些更改,现在我有两个文件夹,src 和 dist,我想跟踪这两个文件夹. 我遇到的问题是,
我很难弄清楚 VkDescriptorSetLayoutBinding::binding 的任何用例,这是结构: struct VkDescriptorSetLayoutBinding { u
Python中能否有效获取稀疏向量的范数? 我尝试了以下方法: from scipy import sparse from numpy.linalg import norm vector1 = spa
我正在尝试找出为什么这段代码不对数组进行排序... 任意向量。 x = array([[3, 2, 4, 5, 7, 4, 3, 4, 3, 3, 1, 4, 6, 3, 2, 4, 3, 2]])
有谁知道如何压缩(编码)稀疏 vector ?稀疏 vector 表示有许多“0”的 1xN 矩阵。 例如 10000000000001110000000000000000100000000 上面是稀
我使用稀疏高斯过程进行 Rasmussen 回归。[http://www.tsc.uc3m.es/~miguel/downloads.php][1] 预测平均值的语法是: [~, mu_1, ~, ~
我在朴素贝叶斯分类器中使用 Mahout API。其中一个功能是 SparseVectorsFromSequenceFiles虽然我已经尝试过旧的谷歌搜索,但我仍然不明白什么是稀疏 vector 。最
我正在尝试将JavaScript稀疏数组映射到C#表示形式。 建议这样做的方法是什么? 它正在考虑使用一个字典,该字典包含在原始数组中包含值的原始词列表。 还有其他想法吗? 谢谢! 最佳答案 注意 针
如果我想求解一个完整上三角系统,我可以调用linsolve(A,b,'UT')。然而,这目前不支持稀疏矩阵。我该如何克服这个问题? 最佳答案 UT 和 LT 系统是最容易解决的系统之一。读一读on t
我有一个带有 MultiIndex 的 Pandas DataFrame。 MultiIndex 的值在 (0,0) 到 (1000,1000) 范围内,该列有两个字段 p 和 q. 但是,DataF
我目前正在实现一个小型有限元模拟。使用 Python/Numpy,我正在寻找一种有效的方法来创建全局刚度矩阵: 1)我认为应该使用coo_matrix()从较小的单元刚度矩阵创建稀疏矩阵。但是,我可以
a , b是 1D numpy ndarray与整数数据类型具有相同的大小。 C是一个 2D scipy.sparse.lil_matrix . 如果索引[a, b]包含重复索引,C[a, b] +=
我有一个大的、连通的、稀疏的邻接表形式的图。我想找到两个尽可能远的顶点,即 diameter of the graph以及实现它的两个顶点。 对于不同的应用程序,我对无向和有向情况下的这个问题都很感兴
上下文:我将 Eigen 用于人工神经网络,其中典型维度为每层约 1000 个节点。所以大部分操作是将大小为 ~(1000,1000) 的矩阵 M 与大小为 1000 的 vector 或一批 B v
我有一些大小合适的矩阵 (2000*2000),我希望在矩阵的元素中有符号表达式 - 即 .9**b + .8**b + .7**b ... 是一个元素的例子。矩阵非常稀疏。 我通过添加中间计算来创建
在 R 或 C++ 中是否有一种快速填充(稀疏)矩阵的方法: A, B, 0, 0, 0 C, A, B, 0, 0 0, C, A, B, 0 0, 0, C, A, B 0, 0, 0, C, A
我有一个大的稀疏 numpy/scipy 矩阵,其中每一行对应于高维空间中的一个点。我想进行以下类型的查询: 给定一个点P(矩阵中的一行)和一个距离epsilon,找到与epsilon距离最大的所有点
假设我有一个 scipy.sparse.csr_matrix 代表下面的值 [[0 0 1 2 0 3 0 4] [1 0 0 2 0 3 4 0]] 我想就地计算非零值的累积和,这会将数组更改为:
我了解如何在 Git 中配置稀疏 checkout ,但我想知道是否可以消除前导目录。例如,假设我有一个 Git 存储库,其文件夹结构如下: 文件夹1/foo 文件夹2/foo/bar/stuff 文
根据 this thread , Git 中的排除 sparse-checkout feature应该实现。是吗? 假设我有以下结构: papers/ papers/... presentations
我是一名优秀的程序员,十分优秀!