Function Repository Resource:

SciPyObject

Source Notebook

A minimal PythonObject configuration for the SciPy package

Contributed by: Igor Bakshee

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.

Details

The Python package SciPy provides algorithms for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics and other classes of problems.
ResourceFunction["SciPyObject"] sets up a configuration of the resource function PythonObject that makes working with SciPy more convenient. Alternatively, this can be done using the "Configuration" option. ResourceFunction["PythonObject"][,"Configuration"ResourceFunction["SciPyObject"]].
ResourceFunction["SciPyObject"] exports SparseArray objects to Python and imports SciPy sparse arrays and matrices as SparseArray objects.
Multiple sparse array formats are supported and can be specified as shown in the examples below. The supported sparse formats include:
BSRblock sparse row
COOcoordinate format
CSCcompressed sparse column
CSRcompressed sparse row

Examples

Basic Examples (2) 

Create a Python object for the SciPy package:

In[1]:=
scipy = ResourceFunction["SciPyObject"][]
Out[1]=

Create a Python compressed sparse row (CSR) matrix from a dense matrix:

In[2]:=
scipy["csr_matrix"[IdentityMatrix[{5, 10}]]]
Out[2]=

Import the Python sparse matrix: as SparseArray:

In[3]:=
% // Normal
Out[3]=

Display in the dense form:

In[4]:=
MatrixForm[%]
Out[4]=

Clean up by closing the Python session:

In[5]:=
DeleteObject[scipy["Session"]]

Export a SparseArray object to Python as a CSR matrix:

In[6]:=
scipy = ResourceFunction["SciPyObject"][]
Out[6]=
In[7]:=
s = SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]
Out[7]=
In[8]:=
scipy["csr_matrix"[s]]
Out[8]=

Import the Python sparse matrix as a SparseArray:

In[9]:=
% // Normal
Out[9]=

Display in the dense form:

In[10]:=
MatrixForm[%]
Out[10]=
In[11]:=
DeleteObject[scipy["Session"]]

Scope (2) 

SciPyObject supports the creation of both sparse matrices and sparse arrays:

In[12]:=
scipy = ResourceFunction["SciPyObject"][]
Out[12]=
In[13]:=
s = SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]
Out[13]=
In[14]:=
formats = {"bsr_array", "bsr_matrix", "coo_array", "coo_matrix", "csc_array", "csc_matrix", "csr_array", "csr_matrix"};
In[15]:=
scipy[#[s]] & /@ formats
Out[15]=

Instances of all these arrays can be imported:

In[16]:=
Normal /@ %
Out[16]=

They are all equivalent to each other and the original matrix:

In[17]:=
SameQ[Flatten[{%, s}]]
Out[17]=
In[18]:=
DeleteObject[scipy["Session"]]

Access the functionality of the SciPy package, for instance, compute the Minkowski distance between two arrays:

In[19]:=
scipy = ResourceFunction["SciPyObject"][]
Out[19]=
In[20]:=
scipy["minkowski_distance"[{{0, 0}, {0, 0}}, {{1, 1}, {0, 1}}]]
Out[20]=
In[21]:=
Normal[%] // Normal
Out[21]=
In[22]:=
DeleteObject[scipy["Session"]]

Applications (6) 

Many Python packages, for instance, NetworkX, return sparse arrays in one of the the SciPy formats:

In[23]:=
nx = ResourceFunction["NetworkXObject"][]
Out[23]=

Create a graph in Python:

In[24]:=
g = nx["Graph"[{{1, 2}, {2, 3}, {3, 1}}]]
Out[24]=

Compute the adjacency matrix on the Python side:

In[25]:=
nx["AdjacencyMatrix"[g]]
Out[25]=

Import the matrix as SparseArray:

In[26]:=
m = Normal[%]
Out[26]=

Display in the denseForm:

In[27]:=
MatrixForm[%]
Out[27]=

For comparison, compute the adjacency matrix of the imported graph:

In[28]:=
Normal[g]
Out[28]=
In[29]:=
AdjacencyMatrix[%]
Out[29]=
In[30]:=
MatrixForm[%]
Out[30]=
In[31]:=
DeleteObject[nx["Session"]]

Properties and Relations (3) 

Create a Python object for the SciPy's compressed sparse row matrix using the "Configuration" option:

In[32]:=
csr = ResourceFunction["PythonObject"]["csr_matrix", "Configuration" -> ResourceFunction["SciPyObject"], Initialization -> "from scipy.sparse import csr_matrix"]
Out[32]=

Define a sparse matrix in Python:

In[33]:=
csr[All][IdentityMatrix[{5, 10}]]
Out[33]=

Convert the Python object to SparseArray:

In[34]:=
% // Normal
Out[34]=
In[35]:=
DeleteObject[csr["Session"]]

Requirements

Wolfram Language 12.3 (May 2021) or above

Version History

  • 1.1.0 – 14 February 2024
  • 1.0.0 – 15 March 2022

Related Resources

License Information