DOK
Bases: SparseArray
, NDArrayOperatorsMixin
A class for building sparse multidimensional arrays.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape
|
tuple[int](ndim)
|
The shape of the array. |
required |
data
|
dict
|
The key-value pairs for the data in this array. |
None
|
dtype
|
dtype
|
The data type of this array. If left empty, it is inferred from the first element. |
None
|
fill_value
|
scalar
|
The fill value of this array. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
dtype |
dtype
|
The datatype of this array. Can be |
shape |
tuple[int]
|
The shape of this array. |
data |
dict
|
The keys of this dictionary contain all the indices and the values contain the nonzero entries. |
See Also
sparse.COO
: A read-only sparse array.
Examples:
You can create sparse.DOK
objects from Numpy arrays.
>>> x = np.eye(5, dtype=np.uint8)
>>> x[2, 3] = 5
>>> s = DOK.from_numpy(x)
>>> s
<DOK: shape=(5, 5), dtype=uint8, nnz=6, fill_value=0>
You can also create them from just shapes, and use slicing assignment.
>>> s2 = DOK((5, 5), dtype=np.int64)
>>> s2[1:3, 1:3] = [[4, 5], [6, 7]]
>>> s2
<DOK: shape=(5, 5), dtype=int64, nnz=4, fill_value=0>
You can convert sparse.DOK
arrays to sparse.COO
arrays, or numpy.ndarray
objects.
>>> from sparse import COO
>>> s3 = COO(s2)
>>> s3
<COO: shape=(5, 5), dtype=int64, nnz=4, fill_value=0>
>>> s2.todense()
array([[0, 0, 0, 0, 0],
[0, 4, 5, 0, 0],
[0, 6, 7, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
>>> s4 = COO.from_numpy(np.eye(4, dtype=np.uint8))
>>> s4
<COO: shape=(4, 4), dtype=uint8, nnz=4, fill_value=0>
>>> s5 = DOK.from_coo(s4)
>>> s5
<DOK: shape=(4, 4), dtype=uint8, nnz=4, fill_value=0>
You can also create sparse.DOK
arrays from a shape and a dict of
values. Zeros are automatically ignored.
>>> values = {
... (1, 2, 3): 4,
... (3, 2, 1): 0,
... }
>>> s6 = DOK((5, 5, 5), values)
>>> s6
<DOK: shape=(5, 5, 5), dtype=int64, nnz=1, fill_value=0.0>
Source code in sparse/numba_backend/_dok.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|
Attributes
shape = tuple(int(sh) for sh in shape)
instance-attribute
fill_value = self.dtype.type(fill_value)
instance-attribute
device
property
ndim
property
The number of dimensions of this array.
Returns:
Type | Description |
---|---|
int
|
The number of dimensions of this array. |
See Also
sparse.DOK.ndim
: Equivalent property forsparse.DOK
arrays.numpy.ndarray.ndim
: Numpy equivalent property.
Examples:
>>> from sparse import COO
>>> import numpy as np
>>> x = np.random.rand(1, 2, 3, 1, 2)
>>> s = COO.from_numpy(x)
>>> s.ndim
5
>>> s.ndim == x.ndim
True
size
property
The number of all elements (including zeros) in this array.
Returns:
Type | Description |
---|---|
int
|
The number of elements. |
See Also
numpy.ndarray.size
: Numpy equivalent property.
Examples:
>>> from sparse import COO
>>> import numpy as np
>>> x = np.zeros((10, 10))
>>> s = COO.from_numpy(x)
>>> s.size
100
density
property
The ratio of nonzero to all elements in this array.
Returns:
Type | Description |
---|---|
float
|
The ratio of nonzero to all elements. |
See Also
sparse.COO.size
: Number of elements.sparse.COO.nnz
: Number of nonzero elements.
Examples:
>>> import numpy as np
>>> from sparse import COO
>>> x = np.zeros((8, 8))
>>> x[0, :] = 1
>>> s = COO.from_numpy(x)
>>> s.density
0.125
amax = max
class-attribute
instance-attribute
amin = min
class-attribute
instance-attribute
round_ = round
class-attribute
instance-attribute
real
property
The real part of the array.
Examples:
>>> from sparse import COO
>>> x = COO.from_numpy([1 + 0j, 0 + 1j])
>>> x.real.todense()
array([1., 0.])
>>> x.real.dtype
dtype('float64')
Returns:
Name | Type | Description |
---|---|---|
out |
SparseArray
|
The real component of the array elements. If the array dtype is real, the dtype of the array is used for the output. If the array is complex, the output dtype is float. |
See Also
numpy.ndarray.real
: NumPy equivalent attribute.numpy.real
: NumPy equivalent function.
imag
property
The imaginary part of the array.
Examples:
>>> from sparse import COO
>>> x = COO.from_numpy([1 + 0j, 0 + 1j])
>>> x.imag.todense()
array([0., 1.])
>>> x.imag.dtype
dtype('float64')
Returns:
Name | Type | Description |
---|---|---|
out |
SparseArray
|
The imaginary component of the array elements. If the array dtype is real, the dtype of the array is used for the output. If the array is complex, the output dtype is float. |
See Also
numpy.ndarray.imag
: NumPy equivalent attribute.numpy.imag
: NumPy equivalent function.
data = {}
instance-attribute
dtype = np.dtype(dtype)
instance-attribute
nnz
property
The number of nonzero elements in this array.
Returns:
Type | Description |
---|---|
int
|
The number of nonzero elements. |
See Also
sparse.COO.nnz
: Equivalentsparse.COO
array property.numpy.count_nonzero
: A similar Numpy function.scipy.sparse.coo_matrix.nnz
: The Scipy equivalent property.
Examples:
>>> values = {
... (1, 2, 3): 4,
... (3, 2, 1): 0,
... }
>>> s = DOK((5, 5, 5), values)
>>> s.nnz
1
format
property
The storage format of this array.
Returns:
Type | Description |
---|---|
str
|
The storage format of this array. |
See Also
scipy.sparse.dok_matrix.format
: The Scipy equivalent property.
Examples:
>>> import sparse
>>> s = sparse.random((5, 5), density=0.2, format="dok")
>>> s.format
'dok'
>>> t = sparse.random((5, 5), density=0.2, format="coo")
>>> t.format
'coo'
nbytes
property
The number of bytes taken up by this object. Note that for small arrays, this may undercount the number of bytes due to the large constant overhead.
Returns:
Type | Description |
---|---|
int
|
The approximate bytes of memory taken by this object. |
See Also
numpy.ndarray.nbytes
: The equivalent Numpy property.
Examples:
>>> import sparse
>>> x = sparse.random((100, 100), density=0.1, format="dok")
>>> x.nbytes
8000
Functions
to_device(device, /, *, stream=None)
Source code in sparse/numba_backend/_sparse_array.py
55 56 57 58 59 |
|
reduce(method, axis=(0,), keepdims=False, **kwargs)
Performs a reduction operation on this array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
ufunc
|
The method to use for performing the reduction. |
required |
axis
|
Union[int, Iterable[int]]
|
The axes along which to perform the reduction. Uses all axes by default. |
(0,)
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
**kwargs
|
dict
|
Any extra arguments to pass to the reduction operation. |
{}
|
See Also
numpy.ufunc.reduce
: A similar Numpy method.sparse.COO.reduce
: This method implemented on COO arrays.sparse.GCXS.reduce
: This method implemented on GCXS arrays.
Source code in sparse/numba_backend/_sparse_array.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
sum(axis=None, keepdims=False, dtype=None, out=None)
Performs a sum operation along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to sum. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
dtype
|
dtype
|
The data type of the output array. |
None
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.sum
: Equivalent numpy function.scipy.sparse.coo_matrix.sum
: Equivalent Scipy function.
Source code in sparse/numba_backend/_sparse_array.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
|
max(axis=None, keepdims=False, out=None)
Maximize along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to maximize. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
out
|
dtype
|
The data type of the output array. |
None
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.max
: Equivalent numpy function.scipy.sparse.coo_matrix.max
: Equivalent Scipy function.
Source code in sparse/numba_backend/_sparse_array.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
any(axis=None, keepdims=False, out=None)
See if any values along array are True
. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to minimize. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.any
: Equivalent numpy function.
Source code in sparse/numba_backend/_sparse_array.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
|
all(axis=None, keepdims=False, out=None)
See if all values in an array are True
. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to minimize. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.all
: Equivalent numpy function.
Source code in sparse/numba_backend/_sparse_array.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
min(axis=None, keepdims=False, out=None)
Minimize along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to minimize. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
out
|
dtype
|
The data type of the output array. |
None
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.min
: Equivalent numpy function.scipy.sparse.coo_matrix.min
: Equivalent Scipy function.
Source code in sparse/numba_backend/_sparse_array.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
|
prod(axis=None, keepdims=False, dtype=None, out=None)
Performs a product operation along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to multiply. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
dtype
|
dtype
|
The data type of the output array. |
None
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.prod
: Equivalent numpy function.
Source code in sparse/numba_backend/_sparse_array.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
round(decimals=0, out=None)
Evenly round to the given number of decimals.
See Also
numpy.round
: NumPy equivalent ufunc.sparse.elemwise
: Apply an arbitrary element-wise function to one or two arguments.
Source code in sparse/numba_backend/_sparse_array.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
|
clip(min=None, max=None, out=None)
Clip (limit) the values in the array.
Return an array whose values are limited to [min, max]
. One of min
or max must be given.
See Also
- sparse.clip : For full documentation and more details.
numpy.clip
: Equivalent NumPy function.
Source code in sparse/numba_backend/_sparse_array.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
|
astype(dtype, casting='unsafe', copy=True)
Copy of the array, cast to a specified type.
See Also
scipy.sparse.coo_matrix.astype
: SciPy sparse equivalent functionnumpy.ndarray.astype
: NumPy equivalent ufunc.sparse.elemwise
: Apply an arbitrary element-wise function to one or two arguments.
Source code in sparse/numba_backend/_sparse_array.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
|
mean(axis=None, keepdims=False, dtype=None, out=None)
Compute the mean along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to compute the mean. Uses all axes by default. |
None
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
dtype
|
dtype
|
The data type of the output array. |
None
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.ndarray.mean
: Equivalent numpy method.scipy.sparse.coo_matrix.mean
: Equivalent Scipy method.
Notes
- The
out
parameter is provided just for compatibility with Numpy and isn't actually supported.
Examples:
You can use sparse.COO.mean
to compute the mean of an array across any
dimension.
>>> from sparse import COO
>>> x = np.array([[1, 2, 0, 0], [0, 1, 0, 0]], dtype="i8")
>>> s = COO.from_numpy(x)
>>> s2 = s.mean(axis=1)
>>> s2.todense()
array([0.5, 1.5, 0., 0.])
You can also use the keepdims
argument to keep the dimensions
after the mean.
>>> s3 = s.mean(axis=0, keepdims=True)
>>> s3.shape
(1, 4)
You can pass in an output datatype, if needed.
>>> s4 = s.mean(axis=0, dtype=np.float16)
>>> s4.dtype
dtype('float16')
By default, this reduces the array down to one number, computing the mean along all axes.
>>> s.mean()
np.float64(0.5)
Source code in sparse/numba_backend/_sparse_array.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
|
var(axis=None, dtype=None, out=None, ddof=0, keepdims=False)
Compute the variance along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to compute the variance. Uses all axes by default. |
None
|
dtype
|
dtype
|
The output datatype. |
None
|
out
|
SparseArray
|
The array to write the output to. |
None
|
ddof
|
int
|
The degrees of freedom. |
0
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.ndarray.var
: Equivalent numpy method.
Examples:
You can use sparse.COO.var
to compute the variance of an array across any
dimension.
>>> from sparse import COO
>>> x = np.array([[1, 2, 0, 0], [0, 1, 0, 0]], dtype="i8")
>>> s = COO.from_numpy(x)
>>> s2 = s.var(axis=1)
>>> s2.todense()
array([0.6875, 0.1875])
You can also use the keepdims
argument to keep the dimensions
after the variance.
>>> s3 = s.var(axis=0, keepdims=True)
>>> s3.shape
(1, 4)
You can pass in an output datatype, if needed.
>>> s4 = s.var(axis=0, dtype=np.float16)
>>> s4.dtype
dtype('float16')
By default, this reduces the array down to one number, computing the variance along all axes.
>>> s.var()
np.float64(0.5)
Source code in sparse/numba_backend/_sparse_array.py
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 |
|
std(axis=None, dtype=None, out=None, ddof=0, keepdims=False)
Compute the standard deviation along the given axes. Uses all axes by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
Union[int, Iterable[int]]
|
The axes along which to compute the standard deviation. Uses all axes by default. |
None
|
dtype
|
dtype
|
The output datatype. |
None
|
out
|
SparseArray
|
The array to write the output to. |
None
|
ddof
|
int
|
The degrees of freedom. |
0
|
keepdims
|
bool_
|
Whether or not to keep the dimensions of the original array. |
False
|
Returns:
Type | Description |
---|---|
SparseArray
|
The reduced output sparse array. |
See Also
numpy.ndarray.std
: Equivalent numpy method.
Examples:
You can use sparse.COO.std
to compute the standard deviation of an array
across any dimension.
>>> from sparse import COO
>>> x = np.array([[1, 2, 0, 0], [0, 1, 0, 0]], dtype="i8")
>>> s = COO.from_numpy(x)
>>> s2 = s.std(axis=1)
>>> s2.todense()
array([0.8291562, 0.4330127])
You can also use the keepdims
argument to keep the dimensions
after the standard deviation.
>>> s3 = s.std(axis=0, keepdims=True)
>>> s3.shape
(1, 4)
You can pass in an output datatype, if needed.
>>> s4 = s.std(axis=0, dtype=np.float16)
>>> s4.dtype
dtype('float16')
By default, this reduces the array down to one number, computing the standard deviation along all axes.
>>> s.std()
0.7071067811865476
Source code in sparse/numba_backend/_sparse_array.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
|
conj()
Return the complex conjugate, element-wise.
The complex conjugate of a complex number is obtained by changing the sign of its imaginary part.
Examples:
>>> from sparse import COO
>>> x = COO.from_numpy([1 + 2j, 2 - 1j])
>>> res = x.conj()
>>> res.todense()
array([1.-2.j, 2.+1.j])
>>> res.dtype
dtype('complex128')
Returns:
Name | Type | Description |
---|---|---|
out |
SparseArray
|
The complex conjugate, with same dtype as the input. |
See Also
numpy.ndarray.conj
: NumPy equivalent method.numpy.conj
: NumPy equivalent function.
Source code in sparse/numba_backend/_sparse_array.py
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 |
|
isinf()
abstractmethod
Source code in sparse/numba_backend/_sparse_array.py
989 990 991 |
|
isnan()
abstractmethod
Source code in sparse/numba_backend/_sparse_array.py
993 994 995 |
|
from_scipy_sparse(x, /, *, fill_value=None)
classmethod
Create a sparse.DOK
array from a scipy.sparse.spmatrix
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
spmatrix
|
The matrix to convert. |
required |
fill_value
|
scalar
|
The fill-value to use when converting. |
None
|
Returns:
Type | Description |
---|---|
DOK
|
The equivalent |
Examples:
>>> import scipy.sparse
>>> x = scipy.sparse.rand(6, 3, density=0.2)
>>> s = DOK.from_scipy_sparse(x)
>>> np.array_equal(x.todense(), s.todense())
True
Source code in sparse/numba_backend/_dok.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
from_coo(x)
classmethod
Get a sparse.DOK
array from a sparse.COO
array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
COO
|
The array to convert. |
required |
Returns:
Type | Description |
---|---|
DOK
|
The equivalent |
Examples:
>>> from sparse import COO
>>> s = COO.from_numpy(np.eye(4))
>>> s2 = DOK.from_coo(s)
>>> s2
<DOK: shape=(4, 4), dtype=float64, nnz=4, fill_value=0.0>
Source code in sparse/numba_backend/_dok.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
to_coo()
Convert this sparse.DOK
array to a sparse.COO
array.
Returns:
Type | Description |
---|---|
COO
|
The equivalent |
Examples:
>>> s = DOK((5, 5))
>>> s[1:3, 1:3] = [[4, 5], [6, 7]]
>>> s
<DOK: shape=(5, 5), dtype=float64, nnz=4, fill_value=0.0>
>>> s2 = s.to_coo()
>>> s2
<COO: shape=(5, 5), dtype=float64, nnz=4, fill_value=0.0>
Source code in sparse/numba_backend/_dok.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
from_numpy(x)
classmethod
Get a sparse.DOK
array from a Numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
The array to convert. |
required |
Returns:
Type | Description |
---|---|
DOK
|
The equivalent |
Examples:
>>> s = DOK.from_numpy(np.eye(4))
>>> s
<DOK: shape=(4, 4), dtype=float64, nnz=4, fill_value=0.0>
Source code in sparse/numba_backend/_dok.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
todense()
Convert this sparse.DOK
array into a Numpy array.
Returns:
Type | Description |
---|---|
ndarray
|
The equivalent dense array. |
See Also
sparse.COO.todense
: EquivalentCOO
array method.scipy.sparse.coo_matrix.todense
: Equivalent Scipy method.
Examples:
>>> s = DOK((5, 5))
>>> s[1:3, 1:3] = [[4, 5], [6, 7]]
>>> s.todense()
array([[0., 0., 0., 0., 0.],
[0., 4., 5., 0., 0.],
[0., 6., 7., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
Source code in sparse/numba_backend/_dok.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
asformat(format, **kwargs)
Convert this sparse array to a given format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format
|
str
|
A format string. |
required |
Returns:
Name | Type | Description |
---|---|---|
out |
SparseArray
|
The converted array. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the format isn't supported. |
Source code in sparse/numba_backend/_dok.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
|
reshape(shape, order='C')
Returns a new sparse.DOK
array that is a reshaped version of this array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape
|
tuple[int]
|
The desired shape of the output array. |
required |
Returns:
Type | Description |
---|---|
DOK
|
The reshaped output array. |
See Also
numpy.ndarray.reshape
: The equivalent Numpy function.
Notes
The order
parameter is provided just for compatibility with
Numpy and isn't actually supported.
Examples:
>>> s = DOK.from_numpy(np.arange(25))
>>> s2 = s.reshape((5, 5))
>>> s2.todense()
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Source code in sparse/numba_backend/_dok.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|