mxnet基础到提高(46)-ndarray.zeros,CSRNDArray稀疏矩阵
生活随笔
收集整理的這篇文章主要介紹了
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 nd
import mxnet as mx
x=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 nd
import mxnet as mx
a = 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)>
總結
以上是生活随笔為你收集整理的mxnet基础到提高(46)-ndarray.zeros,CSRNDArray稀疏矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用自定义注解做点什么——自定义注解有什么
- 下一篇: SpringBoot使用Easypoi导