# create numerical arrays from strings Classic List Threaded 6 messages Open this post in threaded view
|

## create numerical arrays from strings

 NumPy matrix construction includes as a convenience feature the construction of matrices with a Matlab-like syntax. E.g., np.mat('1 2;3 4'). Is it correct that this syntax is not supported for direct (i.e., not using `mat`) ndarray creation? You may ask, where would this possibly matter? The answer: in the undergraduate classroom. Compare np.mat('1 2; 3 4') to np.array([[1, 2], [3, 4]]) for readability and intimidation factor. Little things matter when getting started with students who lack programming background. Thanks, Alan Isaac _______________________________________________ NumPy-Discussion mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: create numerical arrays from strings

 Hi Alan On Thu, 06 Feb 2014 08:46:49 -0500, Alan G Isaac wrote: > You may ask, where would this possibly matter? > The answer: in the undergraduate classroom. As a lecturer, I understand where you are coming from, but I don't think we can ultimately make API decisions based on teachability. The ndarray constructor already has behavior defined for strings: np.array('1 2 3; 4 5 6') array('1 2 3; 4 5 6',       dtype='|S12') So we can't easily change that now.  The best is probably to write a small utility library for your students that help them to easily construct arrays. Also, if you teach them inside an IPython Notebook, they can easily type np.ndarray([[1, 2],             [3, 4]]) which is quite readable and makes use of standard Python objects. Regards Stéfan _______________________________________________ NumPy-Discussion mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: create numerical arrays from strings

 In reply to this post by Alan Isaac On Thu, Feb 6, 2014 at 5:46 AM, Alan G Isaac wrote: NumPy matrix construction includes as a convenience feature the construction of matrices with a Matlab-like syntax. E.g., np.mat('1 2;3 4'). Is it correct that this syntax is not supported for direct (i.e., not using `mat`) ndarray creation? You may ask, where would this possibly matter? The answer: in the undergraduate classroom. Compare np.mat('1 2; 3 4') to np.array([[1, 2], [3, 4]]) for readability and intimidation factor. Little things matter when getting started with students who lack programming background.1) so use np.mat ! 2) The "right" way involves a few more keystrokes -- is this really a big deal?    "Commas separate elements, each row is enclosed in square brackets"   vs:   "whitespace separates elements, semi-colons separate rows."     I'm not sure it's that much harder to understand for a newbie. I'm sure it is for someone used to MATLAB, but do we really want to encourage folks to keep their MATLAB habits? 3) Even if it is substantially easier for a newbie, I think we need to be very careful in teaching to select for "easy to learn first" over "the right way to do it" -- in general, I think it's more important to establish good habits and understanding of what's under the covers than maximizing the ability to type in their first array literal. 4) we really don't want to go down the perl-esque route of "strings are interpreted as numbers if they happen to be numbers"IMHO, and all that.... -CHB-- Christopher Barker, Ph.D.OceanographerEmergency Response DivisionNOAA/NOS/OR&R            (206) 526-6959   voice 7600 Sand Point Way NE   (206) 526-6329   faxSeattle, WA  98115       (206) 526-6317   main reception[hidden email] _______________________________________________ NumPy-Discussion mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: create numerical arrays from strings

 In reply to this post by Alan Isaac Date: Thu, 6 Feb 2014 08:42:38 -0800 From: Chris Barker <[hidden email]> Subject: Re: [Numpy-discussion] create numerical arrays from strings To: Discussion of Numerical Python <[hidden email]> Message-ID:         <[hidden email]> 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 nphelp(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')amatrix([[1, 2],        [3, 4]]) b = np.array(a)barray([[1, 2],       [3, 4]])and so, as we should expect:c=np.array(np.mat('1 2;3 4'))carray([[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 [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion