200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > mxnet基础到提高(46)-ndarray.zeros CSRNDArray稀疏矩阵

mxnet基础到提高(46)-ndarray.zeros CSRNDArray稀疏矩阵

时间:2024-02-27 09:52:04

相关推荐

mxnet基础到提高(46)-ndarray.zeros CSRNDArray稀疏矩阵

mxnet.ndarray.zeros(shape, ctx=None, dtype=None, stype=None, **kwargs)[source]返回一个新的array数组,指定shape和type(形状和类型),用0填充参数shape(int 或 int tuple元组):空数组的形状ctx (Context, 可选项) – 一个可选设备上下文dtype (字符串表示的类型名称 or numpy.dtype, 可选) – 可选类型类型,默认为float32stype (string, optional) – 空数组的存储类型,比如T ‘row_sparse’, ‘csr’, 等返回一个新创建的数组返回类型NDArray, CSRNDArray或RowSparseNDArray

lass mxnet.ndarray.sparse.CSRNDArray(handle, writable=True)[source]Bases: mxnet.ndarray.sparse.BaseSparseNDArray二维NDArray压缩稀疏行格式的稀疏表示。CSRNDArray将NDArray表示为三个独立的数组:data、indptr和indices。它使用CSR表示,行i的列indices索引存储在indices[indptr[i]:indptr[i+1]]中,它们对应的值存储在data[indptr[i]:indptr[i+1]]中。给定行的列索引应按升序排序。同一行不允许重复列项。A sparse representation of 2D NDArray in the Compressed Sparse Row format.A CSRNDArray represents an NDArray as three separate arrays: data, indptr and indices. It uses the CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]].The column indices for a given row are expected to be sorted in ascending order. Duplicate column entries for the same row are not allowed.

dataA deep copy NDArray of the data array of the CSRNDArray.indicesA deep copy NDArray of the indices array of the CSRNDArray.indptrA deep copy NDArray of the indptr array of the CSRNDArray.

a = mx.nd.array([[0, 1, 0], [2, 0, 0], [0, 0, 0], [0, 0, 3]])a = a.tostype('csr')a.data.asnumpy()array([ 1., 2., 3.], dtype=float32)a.indices.asnumpy()#得到列array([1, 0, 2])a.indptr.asnumpy()array([0, 1, 2, 2, 3])

>>> b.asnumpy()array([[0., 1., 0.],[2., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 3.]], dtype=float32)>>> b.data[1. 2. 3.]<NDArray 3 @cpu(0)>>>> b.indices[1 0 2]<NDArray 3 @cpu(0)>>>> b.indptr[0 1 2 2 2 3]<NDArray 6 @cpu(0)>>>> #第2行的>>> b.indptr[2][2]<NDArray 1 @cpu(0)>>>> b.indptr[3][2]<NDArray 1 @cpu(0)>>>>#第2行无数据>>> b.data[2:2][]>>>#第3行无数据>>> b.indptr[3][2]<NDArray 1 @cpu(0)>>>> b.indptr[3][2]<NDArray 1 @cpu(0)>>>> b.indptr[4][2]<NDArray 1 @cpu(0)>>>>#第4行有数据>>> b.indptr[4][2]<NDArray 1 @cpu(0)>>>>> b.indptr[5][3]<NDArray 1 @cpu(0)>>>> b.data[2:3][3.]<NDArray 1 @cpu(0)>>>> b<CSRNDArray 5x3 @cpu(0)>>>> b.asnumpy()array([[0., 1., 0.],[2., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 3.]], dtype=float32)

>>> a = mx.nd.array([[0, 1, 0], [2, 0, 0], [0,0,0],[0, 0, 0], [0, 1, 3]])>>> b = a.tostype('csr')>>> b.asnumpy()array([[0., 1., 0.],[2., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 1., 3.]], dtype=float32)>>> b.data[1. 2. 1. 3.]<NDArray 4 @cpu(0)>>>> b.indices[1 0 1 2]<NDArray 4 @cpu(0)>>>> b.indptr[0 1 2 2 2 4]<NDArray 6 @cpu(0)>#下面取第4行的数值>>> b.indptr[4][2]<NDArray 1 @cpu(0)>>>> b.indptr[5][4]<NDArray 1 @cpu(0)>>>> b.data[2:4][1. 3.]<NDArray 2 @cpu(0)>>>>

from mxnet import ndimport mxnet as mxx=nd.zeros(8)y=nd.zeros((2,3),mx.cpu(),'int32','csr')print(x)print(y)print(y.asnumpy())

[0. 0. 0. 0. 0. 0. 0. 0.]<NDArray 8 @cpu(0)><CSRNDArray 2x3 @cpu(0)>[[0 0 0][0 0 0]]

from mxnet import ndimport mxnet as mxa = mx.nd.array([[0, 1, 0,1,1], [2,0,1, 0, 0], [0,0,0,0,0],[0, 0, 0,0,0], [0, 1,0,3,2]])b = a.tostype('csr')print(b.asnumpy())print(b.indices)print(b.indptr)print(b.indptr[4],b.indptr[5])print(b.data)print(b.data[5:8])print(b.indices[5:8])

[[0. 1. 0. 1. 1.][2. 0. 1. 0. 0.][0. 0. 0. 0. 0.][0. 0. 0. 0. 0.][0. 1. 0. 3. 2.]][1 3 4 0 2 1 3 4]<NDArray 8 @cpu(0)>[0 3 5 5 5 8]<NDArray 6 @cpu(0)>[5]<NDArray 1 @cpu(0)> [8]<NDArray 1 @cpu(0)>[1. 1. 1. 2. 1. 1. 3. 2.]<NDArray 8 @cpu(0)>[1. 3. 2.]<NDArray 3 @cpu(0)>[1 3 4]<NDArray 3 @cpu(0)>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。