Message Boards Message Boards

Sparse matrices with named rows and columns

Posted 9 years ago

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: enter image description here

And here is image with examples of Dot products:

enter image description here

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: enter image description here

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

Here is the corresponding graph: enter image description here

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

POSTED BY: Anton Antonov
6 Replies
Posted 9 years ago

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.

POSTED BY: Vilis Nams

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:

enter image description here

Further, commands like this:

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

seem to work:

enter image description here

POSTED BY: Anton Antonov
Posted 9 years ago

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.

POSTED BY: Vilis Nams

Great then. Thanks for using this package!

POSTED BY: Anton Antonov

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.

POSTED BY: Vitaliy Kaurov

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

POSTED BY: Anton Antonov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract