[Numpy-discussion] Short-hand array creation in `numpy.mat` style
Eric Firing
efiring at hawaii.edu
Sun Jul 6 18:06:25 EDT 2014
On 2014/07/06, 11:43 AM, Nathaniel Smith wrote:
> 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?
I would say 1d array. This is numpy, not numpy.matrix.
> (Maybe np.arr('1 2 3;') should give the row-vector?)
Yes, it is reasonable that a semicolon should trigger 2d.
>
> Should there be some way to write 3d or higher-d arrays?
No, there should not. This is for quick demos and that sort of thing.
It is not a substitute for np.array(). (I'm not entirely convinced
np.arr() is a good idea at all; but if it is, it must be kept simple.)
A possible downside for beginners is that this might delay their
understanding that the commas are needed for np.array([1, 2, 3]).
Eric
>
> -n
>
More information about the NumPy-Discussion
mailing list