Sparse matrices with named rows and columns

In the last few years I have used a lot R’s base library Matrix that has implementation of sparse matrix objects and efficient computations. To the sparse matrices from R’s Matrix library one can assign and retrieve row names and column names with the functions colnames and rownames. Sometimes I miss this in Mathematica so I started a Mathematica package that implements similar functionalities. The package is named RSparseMatrix.m has purely Mathematica language implementations (i.e. it does not use RLink ). It can be loaded/downloaded from MathematicaForPrediction at GitHub:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/RSparseMatrix.m"]

The package provides functions to create and do operations over RSparseMatrix objects of that are basically SparseArray objects with row and column names. A major design decision is to restrict these functionalities to two dimensional sparse arrays and lists of strings as row and column names. (Note that the package is not finished and in some functions the row and column names are ignored.)

The package attempts to cover as many as possible of the functionalities for sparse matrix objects that are provided by R’s Matrix library. (Sub-matrix extraction by row and column names, row and column names propagation for dot products, row and column binding sparse matrices, row and column sums, etc.) This document has examples and tests for RSparseMatrix.m .

My participation in WTC 2015 with a talk about Mathematica and R comparison was one the main motivators to write about RSparseMatrix.m. I find RSparseMatrix.m helpful when communicating certain ideas with machine learning teams with heavy R adoption in them.

Here is an image with sub-matrix taking with using row names and column names:

And here is image with examples of Dot products:

This blog post and this document have detailed usage examples and the following (attempt for a) neat example.

Consider this incidence matrix that represents a bi-partite graph of relationships of actors starring in movies:

We can use an RSparseMatrix object of it with named rows and columns (rBiMat).

Here is the corresponding graph:

If we want to see which actors have participated in movies together with Orlando Bloom we can do the following:

Actors-starring-with-Orlando-Bloom

4 Likes

Very useful, Anton, thanks for sharing, I added this post to a few useful groups including Mathematica Add-Ons. Have you hears of PackageData.net? - A good place to mention it too.

Thanks for proclaiming this package in Mathematica Add-Ons, Vitaliy! I will remember your advice to use the forums / groups you mentioned.

1 Like

Anton,
This looks potentially useful. However, when I tried to load it, I got a

Needs::nocont: Context RSparseMatrix` was not created when Needs was evaluated. >>

error.
When I then tried importing it, it imported OK, but some of the functions did not work.

What commands did you use? I have no problem loading the file with Import. The package is under development so it is not a real Mathematica package.

Please try these commands:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/RSparseMatrix.m"]
rmat = MakeRSparseMatrix[SparseArray[RandomReal[{0, 1}, {4, 5}]]]
SetRowNames[rmat, Characters["ABCD"]]
SetColumnNames[rmat, Characters["abcde"]]
MatrixForm[rmat]

You should get something like this:

Further, commands like this:

MatrixForm[rmat[[{"A", "D", "C", "B"}]].Transpose[rmat]]

seem to work:

1 Like

Anton,
I’m very embarrassed. Before Importing it, I had tried loading it by Needs and by Get. Then after that the Import didn’t work. But now when I try Import initially, it works fine.

Great then. Thanks for using this package!