Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrices in Python

Tags:

python

matrix

Yesterday I had the need for a matrix type in Python.

Apparently, a trivial answer to this need would be to use numpy.matrix(), but the additional issue I have is that I would like a matrix to store arbitrary values with mixed types, similarly to a list. numpy.matrix does not perform this. An example is

>>> numpy.matrix([[1,2,3],[4,"5",6]])
matrix([['1', '2', '3'],
        ['4', '5', '6']], 
       dtype='|S4')
>>> numpy.matrix([[1,2,3],[4,5,6]])
matrix([[1, 2, 3],
        [4, 5, 6]])

As you can see, the numpy.matrix must be homogeneous in content. If a string value is present in my initialization, every value gets implicitly stored as a string. This is also confirmed by accessing the single values

>>> numpy.matrix([[1,2,3],[4,"5",6]])[1,1]
'5'
>>> numpy.matrix([[1,2,3],[4,"5",6]])[1,2]
'6'

Now, the Python list type can instead accept mixed types. You can have a list containing an integer and a string, both conserving their type. What I would need is something similar to a list, but operating in a matrix-like behavior.

Therefore, I had to implement my own type. I had two choices for the internal implementation: list containing lists, and dictionaries. Both solutions have shortcomings:

  • list of lists require careful synchronization of the various lists' sizes. Swapping two rows is easy. Swapping two columns is less easy. Removing a row is easy as well.
  • dictionaries (with a tuple as a key) are slightly better, but you have to define the limits of your key (for example, you cannot insert element 5,5 if your matrix is 3x3), and they are more complex to use to insert, remove, or swap columns or rows.

Edit: clarification. The concrete reason on why I need this functionality is because I am reading CSV files. Once I collect the values from a CSV file (values that can be string, integers, floats) I would like to perform swapping, removal, insertion and other operations alike. For this reason I need a "matrix list".

My curiosities are:

  • do you know if a Python data type providing this service already exists (maybe in a "non-battery included" library out there)?
  • why is this data type not provided in the standard library? Too restricted interest maybe?
  • How would you have solved this need? Dictionary, list, or another smarter solution?
like image 464
Stefano Borini Avatar asked Apr 04 '09 01:04

Stefano Borini


2 Answers

You can have inhomogeneous types if your dtype is object:

In [1]: m = numpy.matrix([[1, 2, 3], [4, '5', 6]], dtype=numpy.object)
In [2]: m
Out[2]: 
matrix([[1, 2, 3],
        [4, 5, 6]], dtype=object)
In [3]: m[1, 1]
Out[3]: '5'
In [4]: m[1, 2]
Out[4]: 6

I have no idea what good this does you other than fancy indexing, because, as Don pointed out, you can't do math with this matrix.

like image 116
Autoplectic Avatar answered Oct 19 '22 23:10

Autoplectic


I'm curious why you want this functionality; as I understand it, the reason for having matrices (in numpy), is primarily for doing linear math (matrix transformations and so on).

I'm not sure what the mathematical definition would be for the product of a decimal and a String.

Internally, you'll probably want to look at sparse matrix implementations (http://www.inf.ethz.ch/personal/arbenz/pycon03_contrib.pdf). There are lots of ways to do this (hash, list, linked list), and each has its own advantages and drawbacks. If your matrix isn't going to have a lot of nulls or zeroes, then you can ditch the sparse implementations.

like image 26
Don Werve Avatar answered Oct 19 '22 22:10

Don Werve