简体   繁体   English

Numpy:将稀疏矩阵转换为ndarray

[英]Numpy: Transform sparse matrix to ndarray

I really couldn't google it. 我真的不能谷歌吧。 How to transform sparse matrix to ndarray? 如何将稀疏矩阵转换为ndarray?

Assume, I have sparse matrix t of zeros. 假设我有零稀疏矩阵t。 Then 然后

g = t.todense()
g[:10] 

matrix([[0],
    [0],
    [0],
    [0],
    [0],
    [0],
    [0],
    [0],
    [0],
    [0]])

instead of [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 代替[0,0,0,0,0,0,0,0,0,0]

Solution: 解:

t.toarray().flatten() t.toarray()。弄平()

Use np.asarray : 使用np.asarray

>>> a = np.asarray(g)
>>> a
array([[0],
       [0],
       [0],
       [0],
       [0],
       [0],
       [0],
       [0],
       [0],
       [0]])

Where g is your dense matrix in the example (after calling t.todense() ). 其中g是示例中的密集矩阵(在调用t.todense() )。

You specifically asked for the output of 你特意要求输出

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

which has only one dimension. 它只有一个维度。 To get that, you'll want to flatten the array: 为此,您需要flatten数组:

>>> flat_array = np.asarray(g).flatten()
>>> flat_array
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Edit: 编辑:

You can skip straight to the array from the sparse matrix with: 您可以从稀疏矩阵直接跳到数组:

a = t.toarray()

转置矩阵以将第一列转换为第一列

g = g.T

ValueError:未能找到可以处理输入的数据适配器:<class 'numpy.ndarray'> , <class 'scipy.sparse.csr.csr_matrix'< div><div id="text_translate"><p> 请帮我解决这个问题</p><pre>X_train = np.asarray(X_train) y_train = np.asarray(y_train) X_test = np.asarray(X_test) y_test = np.asarray(y_test) history = model.fit(X_train, y_train, epochs=75, batch_size=batch_size, verbose=2, validation_data=(X_test, y_test), callbacks= [lrate])</pre><p> ValueError:无法找到可以处理输入的数据适配器:&lt;class 'numpy.ndarray'&gt;, &lt;class 'scipy.sparse.csr.csr_matrix' 即使我转换为 numpy 数组,但出现错误。 请帮忙。 谢谢你。</p></div></class></class> - ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, <class 'scipy.sparse.csr.csr_matrix'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个numpy ndarray添加到稀疏矩阵 - adding a numpy ndarray to a sparse matrix numpy.ndarray 稀疏矩阵到密集 - numpy.ndarray sparse matrix to dense 如何将“SciPy稀疏矩阵”转换为“NumPy矩阵”? - How do I transform a “SciPy sparse matrix” to a “NumPy matrix”? 如何将 numpy.matrix 或数组转换为 scipy 稀疏矩阵 - How to transform numpy.matrix or array to scipy sparse matrix 稀疏矩阵中位数 - Median for sparse matrix in numpy 将字典稀疏矩阵转换为numpy稀疏矩阵 - Convert dictionary sparse matrix to numpy sparse matrix 将字符串矩阵转换为 numpy ndarray - converting an string matrix to numpy ndarray 将numpy数组排列成ndarray或矩阵 - Permutations of a numpy array into an ndarray or matrix 使numpy ndarray矩阵对称 - Making a numpy ndarray matrix symmetric ValueError:未能找到可以处理输入的数据适配器:<class 'numpy.ndarray'> , <class 'scipy.sparse.csr.csr_matrix'< div><div id="text_translate"><p> 请帮我解决这个问题</p><pre>X_train = np.asarray(X_train) y_train = np.asarray(y_train) X_test = np.asarray(X_test) y_test = np.asarray(y_test) history = model.fit(X_train, y_train, epochs=75, batch_size=batch_size, verbose=2, validation_data=(X_test, y_test), callbacks= [lrate])</pre><p> ValueError:无法找到可以处理输入的数据适配器:&lt;class 'numpy.ndarray'&gt;, &lt;class 'scipy.sparse.csr.csr_matrix' 即使我转换为 numpy 数组,但出现错误。 请帮忙。 谢谢你。</p></div></class></class> - ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, <class 'scipy.sparse.csr.csr_matrix'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM