输入 [1]
已复制!
import os
os.environ["SPARSE_BACKEND"] = "Finch"
import sparse
import numpy as np
import os os.environ["SPARSE_BACKEND"] = "Finch" import sparse import numpy as np
/home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/checkouts/stable/sparse/__init__.py:24: SparseFutureWarning: Changing back-ends is a development feature, please do not rely on it in production. warnings.warn(
[juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/lib/python3.12/site-packages/juliacall/juliapkg.json [juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/lib/python3.12/site-packages/finch/juliapkg.json [juliapkg] Found dependencies: /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/lib/python3.12/site-packages/juliapkg/juliapkg.json [juliapkg] Locating Julia =1.10.0, ^1.10.3 [juliapkg] Querying Julia versions from https://julialang-s3.julialang.org/bin/versions.json [juliapkg] WARNING: About to install Julia 1.11.5 to /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/pyjuliapkg/install. [juliapkg] If you use juliapkg in more than one environment, you are likely to [juliapkg] have Julia installed in multiple locations. It is recommended to [juliapkg] install JuliaUp (https://github.com/JuliaLang/juliaup) or Julia [juliapkg] (https://julia-lang.cn/downloads) yourself. [juliapkg] Downloading Julia from https://julialang-s3.julialang.org/bin/linux/x64/1.11/julia-1.11.5-linux-x86_64.tar.gz
download complete [juliapkg] Verifying download [juliapkg] Installing Julia 1.11.5 to /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/pyjuliapkg/install
[juliapkg] Using Julia 1.11.5 at /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/pyjuliapkg/install/bin/julia [juliapkg] Using Julia project at /home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env [juliapkg] Writing Project.toml: [deps] PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" OpenSSL_jll = "458c3c95-2e84-50aa-8efc-19380b2a3a95" Finch = "9177782c-1635-4eb9-9bfb-d9dfa25e6bce" HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605" TensorMarket = "8b7d4fe7-0b45-4d0d-9dd8-5cc9b23b4b77" [compat] PythonCall = "=0.9.25" OpenSSL_jll = "~3.0" Finch = "^1.2.9" HDF5 = "^0.17.2" NPZ = "^0.4.3" TensorMarket = "^0.2" [juliapkg] Installing packages: import Pkg Pkg.Registry.update() Pkg.add([ Pkg.PackageSpec(name="PythonCall", uuid="6099a3de-0909-46bc-b1f4-468b9a2dfc0d"), Pkg.PackageSpec(name="OpenSSL_jll", uuid="458c3c95-2e84-50aa-8efc-19380b2a3a95"), Pkg.PackageSpec(name="Finch", uuid="9177782c-1635-4eb9-9bfb-d9dfa25e6bce"), Pkg.PackageSpec(name="HDF5", uuid="f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"), Pkg.PackageSpec(name="NPZ", uuid="15e1cf62-19b3-5cfa-8e77-841668bca605"), Pkg.PackageSpec(name="TensorMarket", uuid="8b7d4fe7-0b45-4d0d-9dd8-5cc9b23b4b77"), ]) Pkg.resolve() Pkg.precompile()
Installing known registries into `~/.julia`
Added `General` registry to ~/.julia/registries
Updating registry at `~/.julia/registries/General.toml` Resolving package versions...
Installed Scratch ───────────────────── v1.2.1 Installed libaec_jll ────────────────── v1.1.3+0 Installed MPICH_jll ─────────────────── v4.3.0+1 Installed Rmath ─────────────────────── v0.8.0 Installed MicroMamba ────────────────── v0.1.14 Installed IrrationalConstants ───────── v0.2.4 Installed AutoHashEquals ────────────── v2.2.0 Installed HypergeometricFunctions ───── v0.3.28 Installed PtrArrays ─────────────────── v1.3.0 Installed StatsFuns ─────────────────── v1.5.0 Installed IterTools ─────────────────── v1.10.0 Installed JSON3 ─────────────────────── v1.14.2 Installed JSON ──────────────────────── v0.21.4 Installed Preferences ───────────────── v1.4.3 Installed PythonCall ────────────────── v0.9.25
Installed OpenSSL_jll ───────────────── v3.0.16+0 Installed Hwloc_jll ─────────────────── v2.12.1+0 Installed PDMats ────────────────────── v0.11.35 Installed TableTraits ───────────────── v1.0.1 Installed Parsers ───────────────────── v2.8.3 Installed Tables ────────────────────── v1.12.0 Installed SpecialFunctions ──────────── v2.5.1 Installed DataAPI ───────────────────── v1.16.0 Installed ZipFile ───────────────────── v0.10.1 Installed MicrosoftMPI_jll ──────────── v10.1.4+3 Installed JLLWrappers ───────────────── v1.7.0 Installed OpenMPI_jll ───────────────── v5.0.7+2 Installed Pidfile ───────────────────── v1.3.0 Installed micromamba_jll ────────────── v1.5.8+0
Installed IteratorInterfaceExtensions ─ v1.0.0 Installed DataValueInterfaces ───────── v1.0.0 Installed AbstractTrees ─────────────── v0.4.5 Installed PrecompileTools ───────────── v1.2.1 Installed SyntaxInterface ───────────── v0.2.1 Installed NPZ ───────────────────────── v0.4.3 Installed OrderedCollections ────────── v1.8.0 Installed QuadGK ────────────────────── v2.11.2 Installed FillArrays ────────────────── v1.13.0 Installed FileIO ────────────────────── v1.17.0 Installed MPIPreferences ────────────── v0.1.11 Installed Reexport ──────────────────── v1.2.2 Installed MPItrampoline_jll ─────────── v5.5.3+0 Installed TensorMarket ──────────────── v0.2.1
Installed HDF5 ──────────────────────── v0.17.2 Installed Rmath_jll ─────────────────── v0.5.1+0 Installed Statistics ────────────────── v1.11.1
Installed Requires ──────────────────── v1.3.1 Installed LogExpFunctions ───────────── v0.3.29 Installed AliasTables ───────────────── v1.1.3 Installed DataStructures ────────────── v0.18.22 Installed MacroTools ────────────────── v0.5.16 Installed RewriteTools ──────────────── v0.2.4 Installed Distributions ─────────────── v0.25.120 Installed Finch ─────────────────────── v1.2.9 Installed UnsafePointers ────────────── v1.0.0 Installed StatsAPI ──────────────────── v1.7.0 Installed OpenSpecFun_jll ───────────── v0.5.6+0 Installed UnsafeAtomics ─────────────── v0.3.0 Installed HDF5_jll ──────────────────── v1.14.6+0 Installed Compat ────────────────────── v4.16.0 Installed CondaPkg ──────────────────── v0.2.29 Installed SortingAlgorithms ─────────── v1.2.1 Installed StatsBase ─────────────────── v0.34.5
Installed Missings ──────────────────── v1.2.0 Installed DocStringExtensions ───────── v0.9.4 Installed pixi_jll ──────────────────── v0.41.3+0 Installed StructTypes ───────────────── v1.11.0
Updating `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Project.toml` [9177782c] + Finch v1.2.9 [f67ccb44] + HDF5 v0.17.2 [15e1cf62] + NPZ v0.4.3 [6099a3de] + PythonCall v0.9.25 [8b7d4fe7] + TensorMarket v0.2.1 ⌅ [458c3c95] + OpenSSL_jll v3.0.16+0 Updating `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Manifest.toml`
[1520ce14] + AbstractTrees v0.4.5 [66dad0bd] + AliasTables v1.1.3 [15f4f7f2] + AutoHashEquals v2.2.0 [34da2185] + Compat v4.16.0 [992eb4ea] + CondaPkg v0.2.29 [9a962f9c] + DataAPI v1.16.0 [864edb3b] + DataStructures v0.18.22 [e2d170a0] + DataValueInterfaces v1.0.0 [31c24e10] + Distributions v0.25.120 [ffbed154] + DocStringExtensions v0.9.4 [5789e2e9] + FileIO v1.17.0 [1a297f60] + FillArrays v1.13.0 [9177782c] + Finch v1.2.9 [f67ccb44] + HDF5 v0.17.2 [34004b35] + HypergeometricFunctions v0.3.28 [92d709cd] + IrrationalConstants v0.2.4 [c8e1da08] + IterTools v1.10.0 [82899510] + IteratorInterfaceExtensions v1.0.0 [692b3bcd] + JLLWrappers v1.7.0 [682c06a0] + JSON v0.21.4 [0f8b85d8] + JSON3 v1.14.2 [2ab3a3ac] + LogExpFunctions v0.3.29 [3da0fdf6] + MPIPreferences v0.1.11 [1914dd2f] + MacroTools v0.5.16 [0b3b1443] + MicroMamba v0.1.14 [e1d29d7a] + Missings v1.2.0 [15e1cf62] + NPZ v0.4.3 [bac558e1] + OrderedCollections v1.8.0 [90014a1f] + PDMats v0.11.35 [69de0a69] + Parsers v2.8.3 [fa939f87] + Pidfile v1.3.0 ⌅ [aea7be01] + PrecompileTools v1.2.1 [21216c6a] + Preferences v1.4.3 [43287f4e] + PtrArrays v1.3.0 [6099a3de] + PythonCall v0.9.25 [1fd47b50] + QuadGK v2.11.2 [189a3867] + Reexport v1.2.2 [ae029012] + Requires v1.3.1 [5969e224] + RewriteTools v0.2.4 [79098fc4] + Rmath v0.8.0 [6c6a2e73] + Scratch v1.2.1 [a2af1166] + SortingAlgorithms v1.2.1 [276daf66] + SpecialFunctions v2.5.1 [10745b16] + Statistics v1.11.1 [82ae8749] + StatsAPI v1.7.0 [2913bbd2] + StatsBase v0.34.5 [4c63d2b9] + StatsFuns v1.5.0 [856f2bd8] + StructTypes v1.11.0 [b33eeca9] + SyntaxInterface v0.2.1 [3783bdb8] + TableTraits v1.0.1 [bd369af6] + Tables v1.12.0 [8b7d4fe7] + TensorMarket v0.2.1 [013be700] + UnsafeAtomics v0.3.0 [e17b2a0c] + UnsafePointers v1.0.0 [a5390f91] + ZipFile v0.10.1 [0234f1f7] + HDF5_jll v1.14.6+0 [e33a78d0] + Hwloc_jll v2.12.1+0 [7cb0a576] + MPICH_jll v4.3.0+1 [f1f71cc9] + MPItrampoline_jll v5.5.3+0 [9237b28f] + MicrosoftMPI_jll v10.1.4+3 [fe0851c0] + OpenMPI_jll v5.0.7+2 ⌅ [458c3c95] + OpenSSL_jll v3.0.16+0 [efe28fd5] + OpenSpecFun_jll v0.5.6+0 [f50d1b31] + Rmath_jll v0.5.1+0 [477f73a3] + libaec_jll v1.1.3+0 [f8abcde7] + micromamba_jll v1.5.8+0 [4d7b5844] + pixi_jll v0.41.3+0 [0dad84c5] + ArgTools v1.1.2 [56f22d72] + Artifacts v1.11.0 [2a0f44e3] + Base64 v1.11.0 [ade2ca70] + Dates v1.11.0 [f43a241f] + Downloads v1.6.0 [7b1f6079] + FileWatching v1.11.0 [b77e0a4c] + InteractiveUtils v1.11.0 [4af54fe1] + LazyArtifacts v1.11.0 [b27032c2] + LibCURL v0.6.4 [76f85450] + LibGit2 v1.11.0 [8f399da3] + Libdl v1.11.0 [37e2e46d] + LinearAlgebra v1.11.0 [56ddb016] + Logging v1.11.0 [d6f4376e] + Markdown v1.11.0 [a63ad114] + Mmap v1.11.0 [ca575930] + NetworkOptions v1.2.0 [44cfe95a] + Pkg v1.11.0 [de0858da] + Printf v1.11.0 [9a3f8284] + Random v1.11.0 [ea8e919c] + SHA v0.7.0 [9e88b42a] + Serialization v1.11.0 [2f01184e] + SparseArrays v1.11.0 [4607b0f0] + SuiteSparse [fa267f1f] + TOML v1.0.3 [a4e569a6] + Tar v1.10.0 [8dfed614] + Test v1.11.0 [cf7118a7] + UUIDs v1.11.0 [4ec0a83e] + Unicode v1.11.0 [e66e0078] + CompilerSupportLibraries_jll v1.1.1+0 [deac9b47] + LibCURL_jll v8.6.0+0 [e37daf67] + LibGit2_jll v1.7.2+0 [29816b5a] + LibSSH2_jll v1.11.0+1 [c8ffd9c3] + MbedTLS_jll v2.28.6+0 [14a3606d] + MozillaCACerts_jll v2023.12.12 [4536629a] + OpenBLAS_jll v0.3.27+1 [05823500] + OpenLibm_jll v0.8.5+0 [bea87d4a] + SuiteSparse_jll v7.7.0+0 [83775a58] + Zlib_jll v1.2.13+1 [8e850b90] + libblastrampoline_jll v5.11.0+0 [8e850ede] + nghttp2_jll v1.59.0+0 [3f19e933] + p7zip_jll v17.4.0+2 Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
Precompiling project...
406.4 ms ✓ TensorMarket
633.3 ms ✓ OrderedCollections 642.6 ms ✓ AutoHashEquals
304.3 ms ✓ Reexport 466.2 ms ✓ DataValueInterfaces
476.4 ms ✓ DocStringExtensions 1171.1 ms ✓ FillArrays
553.0 ms ✓ AbstractTrees
360.0 ms ✓ UnsafePointers 710.6 ms ✓ Statistics
258.6 ms ✓ IteratorInterfaceExtensions
438.1 ms ✓ DataAPI 1345.3 ms ✓ IrrationalConstants
529.6 ms ✓ StatsAPI 481.3 ms ✓ IterTools
536.0 ms ✓ SyntaxInterface
2092.1 ms ✓ UnsafeAtomics 365.0 ms ✓ PtrArrays
627.9 ms ✓ Requires 648.1 ms ✓ ZipFile
655.2 ms ✓ Scratch 786.9 ms ✓ StructTypes
530.8 ms ✓ SuiteSparse 619.0 ms ✓ Pidfile
3318.1 ms ✓ MacroTools
711.6 ms ✓ Compat
760.4 ms ✓ Preferences
733.3 ms ✓ FillArrays → FillArraysSparseArraysExt 447.2 ms ✓ FillArrays → FillArraysStatisticsExt
812.4 ms ✓ Statistics → SparseArraysExt 430.4 ms ✓ TableTraits
476.5 ms ✓ Missings
522.6 ms ✓ RewriteTools 783.9 ms ✓ LogExpFunctions
635.1 ms ✓ AliasTables
455.8 ms ✓ Compat → CompatLinearAlgebraExt
910.7 ms ✓ PDMats
652.4 ms ✓ MPIPreferences 548.4 ms ✓ PrecompileTools
508.2 ms ✓ JLLWrappers
786.6 ms ✓ Tables
751.7 ms ✓ FillArrays → FillArraysPDMatsExt
1725.6 ms ✓ DataStructures
553.0 ms ✓ OpenSSL_jll
605.6 ms ✓ Rmath_jll
585.6 ms ✓ MicrosoftMPI_jll
639.2 ms ✓ Hwloc_jll
6454.5 ms ✓ FileIO
1332.9 ms ✓ MPItrampoline_jll 1121.2 ms ✓ micromamba_jll
729.0 ms ✓ OpenSpecFun_jll
1312.7 ms ✓ pixi_jll
642.4 ms ✓ libaec_jll
782.3 ms ✓ SortingAlgorithms
1194.1 ms ✓ QuadGK
858.6 ms ✓ Rmath
1142.1 ms ✓ OpenMPI_jll
1436.4 ms ✓ MPICH_jll
1680.2 ms ✓ NPZ
3321.8 ms ✓ MicroMamba
2631.5 ms ✓ SpecialFunctions
2498.2 ms ✓ StatsBase 2027.6 ms ✓ HDF5_jll
1214.5 ms ✓ HypergeometricFunctions
2352.8 ms ✓ StatsFuns
19652.5 ms ✓ Parsers
5736.6 ms ✓ Distributions
10074.7 ms ✓ HDF5
1763.1 ms ✓ JSON
1212.2 ms ✓ Distributions → DistributionsTestExt
9518.7 ms ✓ JSON3
3643.1 ms ✓ CondaPkg
14168.3 ms ✓ PythonCall
224260.9 ms ✓ Finch
2974.8 ms ✓ Finch → SparseArraysExt
3837.0 ms ✓ Finch → HDF5Ext 3981.6 ms ✓ Finch → NPZExt
2308.3 ms ✓ Finch → TensorMarketExt 78 dependencies successfully precompiled in 267 seconds. 32 already precompiled. 3 dependencies had output during precompilation: ┌ Finch │ [ Info: Running enhanced Finch.jl precompilation... (to disable, run `using Preferences; Preferences.set_preferences!("Finch", "precompile_workload" => false; force=true)`). See https://julialang.github.io/PrecompileTools.jl/stable/#Package-developers:-reducing-the-cost-of-precompilation-during-development for more. │ [ Info: Precompiling common tensor formats... │ [ Info: Precompiling common tensor formats... (estimated: 0 minutes and 4 seconds) │ [ Info: Precompiling common tensor operations... │ │ [ Info: Precompiling common tensor operations... (estimated: 2 minutes and 29 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 25 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 34 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 1 minutes and 8 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 53 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 38 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 26 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 16 seconds) │ │ [ Info: Precompiling common tensor operations... (estimated: 0 minutes and 8 seconds) │ │ [ Info: Done! └ ┌ CondaPkg │ Downloading artifact: pixi └ ┌ MicroMamba │ Downloading artifact: micromamba └
No Changes to `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Project.toml` No Changes to `~/checkouts/readthedocs.org/user_builds/sparse-nd/envs/stable/julia_env/Manifest.toml`
Detected IPython. Loading juliacall extension. See https://juliapy.github.io/PythonCall.jl/stable/compat/#IPython
执行操作¶
让我们创建两个数组。
输入 [2]
已复制!
rng = np.random.default_rng(42) # Seed for reproducibility
a = sparse.random((3, 3), density=1 / 6, random_state=rng)
b = sparse.random((3, 3), density=1 / 6, random_state=rng)
rng = np.random.default_rng(42) # 可重现性种子 a = sparse.random((3, 3), density=1 / 6, random_state=rng) b = sparse.random((3, 3), density=1 / 6, random_state=rng)
现在让我们对它们进行矩阵乘法。
输入 [3]
已复制!
c = a @ b
c = a @ b
并将结果视为一个(密集)NumPy 数组。
输入 [4]
已复制!
c_dense = c.todense()
c_dense = c.todense()
现在让我们对其他格式执行相同的操作,并比较结果。
In [5]
已复制!
for format in ["coo", "csr", "csc", "dense"]:
af = sparse.asarray(a, format=format)
bf = sparse.asarray(b, format=format)
cf = af @ bf
np.testing.assert_array_equal(c_dense, cf.todense())
for format in ["coo", "csr", "csc", "dense"]: af = sparse.asarray(a, format=format) bf = sparse.asarray(b, format=format) cf = af @ bf np.testing.assert_array_equal(c_dense, cf.todense())