Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A minimal PythonObject configuration for the SciPy package
ResourceFunction["SciPyObject"][] returns a configured PythonObject for the Python package SciPy in a new Python session.  | |
ResourceFunction["SciPyObject"][session] uses the specified running ExternalSessionObject session.  | |
ResourceFunction["SciPyObject"][…,"func"[args,opts]] executes the function func with the specified arguments and options.  | 
| BSR | block sparse row | 
| COO | coordinate format | 
| CSC | compressed sparse column | 
| CSR | compressed sparse row | 
Create a Python object for the SciPy package:
| In[1]:= | 
| Out[1]= | ![]()  | 
Create a Python compressed sparse row (CSR) matrix from a dense matrix:
| In[2]:= | 
| Out[2]= | ![]()  | 
Import the Python sparse matrix: as SparseArray:
| In[3]:= | 
| Out[3]= | 
Display in the dense form:
| In[4]:= | 
| Out[4]= | ![]()  | 
Clean up by closing the Python session:
| In[5]:= | 
Export a SparseArray object to Python as a CSR matrix:
| In[6]:= | 
| Out[6]= | ![]()  | 
| In[7]:= | 
| Out[7]= | 
| In[8]:= | 
| Out[8]= | ![]()  | 
Import the Python sparse matrix as a SparseArray:
| In[9]:= | 
| Out[9]= | 
Display in the dense form:
| In[10]:= | 
| Out[10]= | 
| In[11]:= | 
SciPyObject supports the creation of both sparse matrices and sparse arrays:
| In[12]:= | 
| Out[12]= | ![]()  | 
| In[13]:= | 
| Out[13]= | 
| In[14]:= | 
| In[15]:= | 
| Out[15]= | ![]()  | 
Instances of all these arrays can be imported:
| In[16]:= | 
| Out[16]= | ![]()  | 
They are all equivalent to each other and the original matrix:
| In[17]:= | 
| Out[17]= | 
| In[18]:= | 
Access the functionality of the SciPy package, for instance, compute the Minkowski distance between two arrays:
| In[19]:= | 
| Out[19]= | ![]()  | 
| In[20]:= | 
| Out[20]= | ![]()  | 
| In[21]:= | 
| Out[21]= | 
| In[22]:= | 
Many Python packages, for instance, NetworkX, return sparse arrays in one of the the SciPy formats:
| In[23]:= | 
| Out[23]= | 
Create a graph in Python:
| In[24]:= | 
| Out[24]= | 
Compute the adjacency matrix on the Python side:
| In[25]:= | 
| Out[25]= | ![]()  | 
Import the matrix as SparseArray:
| In[26]:= | 
| Out[26]= | 
Display in the denseForm:
| In[27]:= | 
| Out[27]= | 
For comparison, compute the adjacency matrix of the imported graph:
| In[28]:= | 
| Out[28]= | ![]()  | 
| In[29]:= | 
| Out[29]= | 
| In[30]:= | 
| Out[30]= | 
| In[31]:= | 
Create a Python object for the SciPy's compressed sparse row matrix using the "Configuration" option:
| In[32]:= | ![]()  | 
| Out[32]= | ![]()  | 
Define a sparse matrix in Python:
| In[33]:= | 
| Out[33]= | ![]()  | 
Convert the Python object to SparseArray:
| In[34]:= | 
| Out[34]= | 
| In[35]:= | 
Wolfram Language 12.3 (May 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License