Skip to content

Commit 4236f9d

Browse files
committed
docs: add from_numpy examples for sparse tensor constructors
Signed-off-by: ChiLin Chiu <chilin.chiou@gmail.com>
1 parent 134638d commit 4236f9d

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

python/pyarrow/tensor.pxi

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,18 @@ shape: {self.shape}"""
411411
Shape of the tensor.
412412
dim_names : list, optional
413413
Names of the dimensions.
414+
415+
Examples
416+
--------
417+
>>> import pyarrow as pa
418+
>>> import numpy as np
419+
>>> data = np.array([1.0, 2.0, 3.0], dtype=np.float32)
420+
>>> coords = np.array([[0, 1], [1, 0], [1, 2]], dtype=np.int64)
421+
>>> sparse_coo = pa.SparseCOOTensor.from_numpy(data, coords, shape=(2, 3))
422+
>>> sparse_coo
423+
<pyarrow.SparseCOOTensor>
424+
type: float
425+
shape: (2, 3)
414426
"""
415427
cdef shared_ptr[CSparseCOOTensor] csparse_tensor
416428
cdef vector[int64_t] c_shape
@@ -735,6 +747,20 @@ shape: {self.shape}"""
735747
Shape of the matrix.
736748
dim_names : list, optional
737749
Names of the dimensions.
750+
751+
Examples
752+
--------
753+
>>> import pyarrow as pa
754+
>>> import numpy as np
755+
>>> data = np.array([1.0, 2.0, 3.0], dtype=np.float64)
756+
>>> indptr = np.array([0, 2, 3], dtype=np.int64)
757+
>>> indices = np.array([0, 2, 1], dtype=np.int64)
758+
>>> sparse_csr = pa.SparseCSRMatrix.from_numpy(
759+
... data, indptr, indices, shape=(2, 3))
760+
>>> sparse_csr
761+
<pyarrow.SparseCSRMatrix>
762+
type: double
763+
shape: (2, 3)
738764
"""
739765
cdef shared_ptr[CSparseCSRMatrix] csparse_tensor
740766
cdef vector[int64_t] c_shape
@@ -992,6 +1018,20 @@ shape: {self.shape}"""
9921018
Shape of the matrix.
9931019
dim_names : list, optional
9941020
Names of the dimensions.
1021+
1022+
Examples
1023+
--------
1024+
>>> import pyarrow as pa
1025+
>>> import numpy as np
1026+
>>> data = np.array([1.0, 3.0, 2.0], dtype=np.float64)
1027+
>>> indptr = np.array([0, 1, 2, 3], dtype=np.int64)
1028+
>>> indices = np.array([0, 1, 0], dtype=np.int64)
1029+
>>> sparse_csc = pa.SparseCSCMatrix.from_numpy(
1030+
... data, indptr, indices, shape=(2, 3))
1031+
>>> sparse_csc
1032+
<pyarrow.SparseCSCMatrix>
1033+
type: double
1034+
shape: (2, 3)
9951035
"""
9961036
cdef shared_ptr[CSparseCSCMatrix] csparse_tensor
9971037
cdef vector[int64_t] c_shape
@@ -1265,6 +1305,27 @@ shape: {self.shape}"""
12651305
produce the prefix tree.
12661306
dim_names : list, optional
12671307
Names of the dimensions.
1308+
1309+
Examples
1310+
--------
1311+
>>> import pyarrow as pa
1312+
>>> import numpy as np
1313+
>>> data = np.array([1.0, 2.0], dtype=np.float32)
1314+
>>> indptr = [
1315+
... np.array([0, 1, 2], dtype=np.int64),
1316+
... np.array([0, 1, 2], dtype=np.int64),
1317+
... ]
1318+
>>> indices = [
1319+
... np.array([0, 1], dtype=np.int64),
1320+
... np.array([1, 2], dtype=np.int64),
1321+
... np.array([0, 1], dtype=np.int64),
1322+
... ]
1323+
>>> sparse_csf = pa.SparseCSFTensor.from_numpy(
1324+
... data, indptr, indices, shape=(2, 3, 2))
1325+
>>> sparse_csf
1326+
<pyarrow.SparseCSFTensor>
1327+
type: float
1328+
shape: (2, 3, 2)
12681329
"""
12691330
cdef shared_ptr[CSparseCSFTensor] csparse_tensor
12701331
cdef vector[int64_t] c_axis_order

0 commit comments

Comments
 (0)