Date: Thu, 6 Feb 2014 08:42:38 -0800

> From: Chris Barker <[email protected]>
> Subject: Re: [Numpy-discussion] create numerical arrays from strings
> To: Discussion of Numerical Python <[email protected]>
> Message-ID:
>         <
> calgmxekvnqok6wty-jbjzgaeu5ewhh1_flmsqxjsujfclex...@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
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to