<div dir="ltr">Date: Thu, 6 Feb 2014 08:42:38 -0800<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
From: Chris Barker <<a href="mailto:chris.barker@noaa.gov">chris.barker@noaa.gov</a>><br>
Subject: Re: [Numpy-discussion] create numerical arrays from strings<br>
To: Discussion of Numerical Python <<a href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a>><br>
Message-ID:<br>
<<a href="mailto:CALGmxEKVNQok6wtY-jbjzgaeU5ewHh1_FLmSQXJsUJfcLExh2w@mail.gmail.com">CALGmxEKVNQok6wtY-jbjzgaeU5ewHh1_FLmSQXJsUJfcLExh2w@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
1) so use np.mat !<br></blockquote><div><br></div><div>To elaborate on this (because I, for one, was not aware that mat supported this API, and, significantly, the fact that it does, does not appear in its docstring:<br>
<br>import numpy as np<br>help(np.mat)<br>Help on function asmatrix in module numpy.matrixlib.defmatrix:<br><br>asmatrix(data, dtype=None)<br> Interpret the input as a matrix.<br> <br> Unlike `matrix`, `asmatrix` does not make a copy if the input is already<br>
a matrix or an ndarray. Equivalent to ``matrix(data, copy=False)``.<br> <br> Parameters<br> ----------<br> data : array_like<br> Input data.<br> <br> Returns<br> -------<br> mat : matrix<br>
`data` interpreted as a matrix.<br> <br> Examples<br> --------<br> >>> x = np.array([[1, 2], [3, 4]])<br> <br> >>> m = np.asmatrix(x)<br> <br> >>> x[0,0] = 5<br> <br>
>>> m<br> matrix([[5, 2],<br> [3, 4]])<br>)<br></div></div><br></div><div class="gmail_extra">However, we do have:<br><br>a=np.mat('1 2;3 4')<br>a<br>matrix([[1, 2],<br> [3, 4]])<br>
b = np.array(a)<br>b<br>array([[1, 2],<br> [3, 4]])<br><br></div><div class="gmail_extra">and so, as we should expect:<br></div><div class="gmail_extra"><br>c=np.array(np.mat('1 2;3 4'))<br>c<br>array([[1, 2],<br>
[3, 4]])<br><br></div><div class="gmail_extra">So the substance of the utility function Stefan suggests is one line:<br><br></div><div class="gmail_extra">def numstr2numarr(in):<br> """ 'in' is a matlab-style array containing strings for the numerical array entries """<br>
</div><div class="gmail_extra"> return np.array(np.mat(in))<br><br></div><div class="gmail_extra">In essence, numpy "almost" provides the API you're asking for.<br><br></div><div class="gmail_extra">DG<br>
</div></div>