[PYTHON MATRIX-SIG] New Tutorial / Some Questions

James Hugunin jjh@Goldilocks.LCS.MIT.EDU
Mon, 5 Feb 96 10:56:03 EST


   From: da@maigret.cog.brown.edu (David Ascher)

   Given that I'm starting a course on Python on Tuesday for a bunch of
   neural net folks, I had to take a good hard look at the Numeric package.  

   Jim H. has gotten more mail from me than he probably wanted.  The good

Actually, I love the mail.  This is the best way to really shake down
the package.

   I came up with a few questions in the process.  Some have gone to jim
   h. and hinsen, but I thought I'd spread the load:

Well, since nobody else has answered yet (and since I'd LOVE to see
your course succeed).

   1. I can't seem to find the counterpart to the toFile() method.

There used to be such a function in Numeric.py.  The reason it's not
there any more is to strongly encourage people to use pickling of
matrices and better yet of objects containing matrices instead of the
raw toFile and fromFile methods.  This is the method I use to store
the fairly complicated GaussianClassifier objects that I use for
speech recognition.  This approach will gain you a certain degree of
architecture independence (though 64-bit longs on alpha's give it
trouble) and will cost almost nothing in performance terms.

Note, you can still read in an array from a file if you need to for
compatibility reasons as follows:

a = fromString(fp.read())

   2. The choose() and take() methods are a little fuzzy for me.  Can
      someone give me a description of what they do, and more useful even
      examples of why they're useful?

Actually, Konrad or Paul could probably do a better job of this than
me, but here are my two favorite examples.


static char doc_choose[] = "m.choose(m_0, ..., m_(n-1)).  m is a array
of integers from 0 to n-1.  Each entry in m will be taken from the
corresponding entry in m_i, where i is the value of m.  Frequently
used with exactly two arguments for a boolean valued m,
ie. m.choose(m_false, m_true).";

Assume a is an array that you want to "clip" so that no values are
greater than 100.0.

(a.greater(100.0)).choose(a, 100.0)

Everywhere that a.greater(100.0) is false (ie. 0) this will "choose"
the corresponding value in a.  Everywhere else it will "choose" 100.0.


static char doc_take[] = "m.take([i_0, ..., i_n], dimension=0).
Selects the items from m along dimension";

Take is the equivalent of "full product form indexing" that for a
number of reasons is not available in normal indexing.  The most
common use I have for take is to implement decoding from mu-law to
16-bit linear sound files.  mu-law files map the bytes from 0-255 to a
given 16-bit number using a non-linear scale as a form of compression.

Assuming you have an array mu_law_array (of length 256) where
 mu_law_array[value_mu_law] = value_linear

linear_encoded_array = mu_law_array.take(mu_law_encoded_array)


   3. Shouldn't typecodes look more like real types than strings, sort of
      like exceptions?  

	   >>> b.typecode()
	   'l'
      vs.
	   >>> b.typecode()
	   LongInteger  or whatever.

The only hassle is that doing this would lead to yet another new
object type added to python (after already adding array's, ufunc's,
and slices (hopefully soon).  I'm willing to be convinced that this is
the right choice, but for now I opted for the minimal approach.

   That's all for now.  Let me know what you think of the tutorial.

Will look at it ASAP.

-Jim



=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================