Best practices for consistent handling of both vectors and matrices

73 views
Skip to first unread message

RJ Nowling

unread,
Jul 23, 2015, 11:51:06 AM7/23/15
to Numerical Clojure
Hi all,

I've started contributing to Rinu Boney's clatern machine learning library which uses core.matrix.  One of the challenges has been how to handle the wide range of sequence data structures and handling vectors and matrices in a uniform way.

For example, for the linear regression module, we want to be able to take a single vector or a matrix of vectors on which to calculate regressions.  I've found that (join-along) doesn't work on 1D vectors so I had to handle 1D vectors separately using (join).

How can I check if an input is a supported core.matrix implementation?  And how can I build a 2D matrix of one row from a 1D vector?

Thanks!
RJ

Mike Anderson

unread,
Jul 23, 2015, 10:10:35 PM7/23/15
to Numerical Clojure, rnow...@gmail.com, rnow...@gmail.com
On Thursday, 23 July 2015 23:51:06 UTC+8, RJ Nowling wrote:
Hi all,

I've started contributing to Rinu Boney's clatern machine learning library which uses core.matrix.  One of the challenges has been how to handle the wide range of sequence data structures and handling vectors and matrices in a uniform way.

For example, for the linear regression module, we want to be able to take a single vector or a matrix of vectors on which to calculate regressions.  I've found that (join-along) doesn't work on 1D vectors so I had to handle 1D vectors separately using (join).

join-along should work for vectors, indeed it seems to do so just fine for me:

(join-along 0 [1 2 3] [4 5 6] [7])
=> [1 2 3 4 5 6 7]

If you have a case that fails, can you file an issue on GitHub? Thanks!

For linear regression on matrices.... we don't yet support matrix arguments but there is an issue in vectorz-clj to solve it:



How can I check if an input is a supported core.matrix implementation?  And how can I build a 2D matrix of one row from a 1D vector?

Depends what you mean by "check if an input is a supported core.matrix implementation" but I would use "array?":

(array? [1 2 3])
=> true

(array? "bob")
=> false

The "reshape function allows arbitrary reshaping of arrays (in row elementwise order)

(reshape [1 2 3 4] [1 4])
=> [[1 2 3 4]]

(reshape [[1 2] [3 4]] [10])
=> [1 2 3 4 0 0 0 0 0 0]
 

Thanks!
RJ

RJ Nowling

unread,
Jul 23, 2015, 10:56:09 PM7/23/15
to Mike Anderson, Numerical Clojure
Thanks, Mike!

I was trying to do this:

(join-along 1 [1 2 3] 4)

But doing it this way instead may work for me:

(join-along 1 [1 2 3] [4])

For the conversion question, I may want to convert '(1 2 3) to [1 2 3].  I think the solution may be to call (matrix x) on every input:

user=> (matrix '(1 2 3))
[1 2 3]

RJ Nowling

unread,
Jul 23, 2015, 10:59:23 PM7/23/15
to Mike Anderson, Numerical Clojure
To follow up, I was actually trying to do this:

user=> (join-along 1 [4] [1 2 3])
RuntimeException Can't join an array to a scalar number!  clojure.core.matrix.impl.default/eval8241/fn--8242 (default.clj:1228)


Is that an expected error or should I file an issue?

RJ Nowling

unread,
Jul 23, 2015, 11:00:21 PM7/23/15
to Mike Anderson, Numerical Clojure
Or more specifically, I want to join along the last dimension, whatever that dimension is.  I can find the last dimension by using (dimensionality) but is there a shortcut like using -1 for the dimension with (join-along) ?

Mike Anderson

unread,
Jul 27, 2015, 1:28:54 AM7/27/15
to Numerical Clojure, rnow...@gmail.com, rnow...@gmail.com
Shortcuts like that are a bit of an ugly hack :-)

Seriously, you only save little bit of typing, at the expense of understandability and maintainability. Definitely not worth it IMHO. 
Reply all
Reply to author
Forward
0 new messages