Re: [Numpydiscussion] create numerical arrays from strings
Date: Thu, 6 Feb 2014 08:42:38 0800
From: Chris Barker
Subject: Re: [Numpydiscussion] create numerical arrays from strings To: Discussion of Numerical Python MessageID: < CALGmxEKVNQok6wtYjbjzgaeU5ewHh1_FLmSQXJsUJfcLExh2w@mail.gmail.com> ContentType: text/plain; charset="utf8" 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 matlabstyle 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
On 2/6/2014 6:03 PM, David Goldsmith wrote:
So the substance of the utility function Stefan suggests is one line:
It's even easier than that: np.mat('1 2;3 4').A However the context is the introduction of the language to students who have no programming experience, not my personal convenience (which this affects not at all). Cheers, Alan
participants (2)

Alan G Isaac

David Goldsmith