[Numpy-discussion] Short-hand array creation in `numpy.mat` style

Nathaniel Smith njs at pobox.com
Sun Jul 6 17:43:10 EDT 2014


On Sun, Jul 6, 2014 at 9:35 PM, Daniel da Silva
<var.mail.daniel at gmail.com> wrote:
> The idea is that there be a short-hand for creating arrays as there is for
> matrices:
>
>   np.mat('.2 .7 .1; .3 .5 .2; .1 .1 .9')
>
> It was suggested in GitHub issue #4817 in light that it would be beneficial
> to beginners and to presenters during demonstrations.  In GitHub pull
> request #484, I implemented this as the np.arr function.
>
> Does anyone have any feedback on the API details? Some examples from my
> implementation follow.
>
>>>> np.arr('3; 4; 5')
>     array([[3],
>            [4],
>            [5]])
>
>>>> np.arr('3; 4; 5', dtype=float)
>     array([[ 3.],
>            [ 4.],
>            [ 5.]])
>
>>>> np.arr('1 0 0; 0 1 0; 0 0 1')
>     array([[1, 0, 0],
>            [0, 1, 0],
>            [0, 0, 1]])
>
>>>> np.arr('4, 5; 6, 7')
>     array([[4, 5],
>            [6, 7]])

It occurs to me that np.mat always returns a 2d matrix, but for arrays
there are more options.

What should np.arr('1 2 3') return? a 1d array or a 2d row vector?
(Maybe np.arr('1 2 3;') should give the row-vector?)

Should there be some way to write 3d or higher-d arrays?

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org



More information about the NumPy-Discussion mailing list