[Numpy-discussion] create numerical arrays from strings
David Goldsmith
d.l.goldsmith at gmail.com
Thu Feb 6 18:03:57 EST 2014
Date: Thu, 6 Feb 2014 08:42:38 -0800
> From: Chris Barker <chris.barker at noaa.gov>
> Subject: Re: [Numpy-discussion] create numerical arrays from strings
> To: Discussion of Numerical Python <numpy-discussion at scipy.org>
> Message-ID:
> <
> CALGmxEKVNQok6wtY-jbjzgaeU5ewHh1_FLmSQXJsUJfcLExh2w at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> 1) so use np.mat !
>
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:
import numpy as np
help(np.mat)
Help on function asmatrix in module numpy.matrixlib.defmatrix:
asmatrix(data, dtype=None)
Interpret the input as a matrix.
Unlike `matrix`, `asmatrix` does not make a copy if the input is already
a matrix or an ndarray. Equivalent to ``matrix(data, copy=False)``.
Parameters
----------
data : array_like
Input data.
Returns
-------
mat : matrix
`data` interpreted as a matrix.
Examples
--------
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],
[3, 4]])
)
However, we do have:
a=np.mat('1 2;3 4')
a
matrix([[1, 2],
[3, 4]])
b = np.array(a)
b
array([[1, 2],
[3, 4]])
and so, as we should expect:
c=np.array(np.mat('1 2;3 4'))
c
array([[1, 2],
[3, 4]])
So the substance of the utility function Stefan suggests is one line:
def numstr2numarr(in):
""" 'in' is a matlab-style array containing strings for the numerical
array entries """
return np.array(np.mat(in))
In essence, numpy "almost" provides the API you're asking for.
DG
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140206/f5aaaa57/attachment.html>
More information about the NumPy-Discussion
mailing list