- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究 TFIDF。我用过 tfidf_vectorizer.fit_transform。它返回一个 csr_matrix,但我无法理解结果的结构。
documents = ( "The sky is blue", "The sun is bright", "The sun in thesky is bright", "We can see the shining sun, the bright sun" )
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
print(tfidf_matrix)
(0, 9) 0.34399327143
(0, 7) 0.519713848879
(0, 4) 0.420753151645
(0, 0) 0.659191117868
(1, 9) 0.426858009784
(1, 4) 0.522108621994
(1, 8) 0.522108621994
(1, 1) 0.522108621994
(2, 9) 0.526261040111
(2, 7) 0.397544332095
(2, 4) 0.32184639876
(2, 8) 0.32184639876
(2, 1) 0.32184639876
(2, 3) 0.504234576856
(3, 9) 0.390963088213
(3, 8) 0.47820398015
(3, 1) 0.239101990075
(3, 10) 0.374599471224
(3, 2) 0.374599471224
(3, 5) 0.374599471224
(3, 6) 0.374599471224
tfidf_matrix 是一个 csr_matrix。所以我找到了这个,但是没有与结果相同的结构:scipy.sparse.csr_matrix
值的结构是 (0, 9) 0.34399327143 ?
最佳答案
您看到的只是调用print(my_csr_mat)
时使用的string-representation。它列出了(在您的情况下)矩阵中的所有 nonzeros 。 (可能会有大量非零值的截断输出)。
因为这是一个稀疏矩阵,所以它有 2 个维度。
(0, 9) 0.34399327143
表示:矩阵元素@位置[0,9]为0.34399327143。
小演示:
import numpy as np
from scipy.sparse import csr_matrix
matrix_dense = np.arange(20).reshape(4,5)
zero_out = np.random.choice((0,1), size=(4,5), p=(0.7, 0.3))
matrix_dense_mod = matrix_dense * zero_out
print(matrix_dense_mod)
sparse_mat = csr_matrix(matrix_dense_mod)
print(sparse_mat)
输出:
[[ 0 0 2 0 4]
[ 0 6 0 8 0]
[ 0 11 0 13 14]
[15 0 0 18 19]]
(0, 2) 2
(0, 4) 4
(1, 1) 6
(1, 3) 8
(2, 1) 11
(2, 3) 13
(2, 4) 14
(3, 0) 15
(3, 3) 18
(3, 4) 19
我不确定你所说的是什么意思所以我找到了这个,但是没有与结果相同的结构
,但要注意:scipy.sparse 文档中的大多数示例都有打印调用中的 my_mat.toarray(),这意味着它正在从具有不同字符串表示样式的稀疏矩阵构建密集数组。
关于Python - csr_matrix 的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678491/
我有一个 csr_matrix 'a' 类型的稀疏矩阵。我想执行一个操作来创建一个新的 csr_matrix 'b',其中 'b' 的每一行与 'a' 的第 i 行相同。 我认为对于普通的 numpy
我将每个 XML 文档表示为 csr_matrix 格式的特征矩阵。现在我有大约 3000 个 XML 文档,我得到了一个 csr_matrices 列表。我想将这些矩阵中的每一个展平成为特征向量,然
我正在研究 TFIDF。我用过 tfidf_vectorizer.fit_transform。它返回一个 csr_matrix,但我无法理解结果的结构。 数据输入: documents = ( "Th
这个问题在这里已经有了答案: What is the built-in function A of numpy array.A? (4 个答案) 关闭 3 年前。 我最近看到这样的事情: impor
我有两个(scipy)CSR 稀疏矩阵: A (12414693, 235470) B (235470, 48063) 执行: A.dot(B) 导致段错误。 我做错了什么? 编辑 我已经向 scip
我有一个 MxN 稀疏 csr_matrix,我想在矩阵的右侧添加一些只有零的列。原则上,数组indptr、indices和data保持不变,所以我只想改变矩阵的维度。但是,这似乎没有实现。 >>>
我有一个 csr_matrix,假设我调用了: import scipy.sparse as ss mat = ss.csr.csr_matrix((50, 100)) 现在我想修改这个矩阵上的一些值
我有一个稀疏矩阵: from scipy.sparse import csr_matrix M=csr_matrix((5,5)) M[2,3]=4 我想迭代所有非零条目,例如: for x,y,v
我从 pandas Dataframe 中的 SQL 导入二进制数据,该数据框由 UserId 和 ItemId 列组成。我正在使用隐式/二进制数据,正如您在下面的pivot_table 中看到的那样
我在使用 scipy.sparse.csr_matrix 时遇到问题: 例如: a = csr_matrix([[1,2,3],[4,5,6]]) b = csr_matrix([[7,8,9],[1
我按以下方式制作 csr_matrix: >>> A = sparse.csr_matrix([[0, 1, 0], [1, 0, 1],
我想知道 indptr 相对于标准 row, col 定义的用途。由于列表存储的数据位较少,是否最好使用 indptr?如果是这样的话,为什么不总是使用这个方法(假设 row, col 列表是有序的)
我有大约 10,000 个稀疏矩阵,每个矩阵大小为 50,000x5,平均密度为 0.0004。对于每个循环(10000 次),我计算 numpy 数组并将其转换为 csr_matrix 并将其附加到
我有一个稀疏的 csr_matrix,我想将单行的值更改为不同的值。但是,我找不到简单有效的实现方式。这是它必须做的: A = csr_matrix([[0, 1, 0],
我有一个通过 scipy.sparse 创建的稀疏 988x1 向量(csr_matrix 中的一列)。有没有办法在不必将稀疏矩阵转换为密集矩阵的情况下获得其均值和标准差? numpy.mean 似乎
我有一个非常大的稀疏 scipy 矩阵。尝试使用 save_npz 导致出现以下错误: >>> sp.save_npz('/projects/BIGmatrix.npz',W) Traceback (
这里的数据有些奇怪。 如果我创建一个 scipy.sparse.csr_matrix ,其 data 属性仅包含 0 和 1,然后要求它打印 data 属性,有时会出现 2在输出中(其他时候不是)。
我有一个 1 列 x 行的 SciPy csr_matrix(在本例中为向量)。其中有浮点值,我需要将其转换为离散类标签 -1、0 和 1。这应该使用阈值函数来完成,该函数将浮点值映射到这 3 个类标
我想逐步构建一个非常大的稀疏矩阵。问题是 lil_matrix 占用了太多 RAM,以至于效率低下。例如,如果我想创建一个 2000 万 x 2000 万的 lil_matrix,它会完全耗尽我的 R
我正在尝试标准化 csr_matrix: with 91833 stored elements in Compressed Sparse Row format> 我试过的是这样的: import n
我是一名优秀的程序员,十分优秀!