create numerical arrays from strings

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

create numerical arrays from strings

Alan Isaac
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
Reply | Threaded
Open this post in threaded view
|

Re: create numerical arrays from strings

Stéfan van der Walt
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
Reply | Threaded
Open this post in threaded view
|

Re: create numerical arrays from strings

Chris Barker - NOAA Federal
In reply to this post by Alan Isaac
On Thu, Feb 6, 2014 at 5:46 AM, Alan G Isaac <[hidden email]> 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.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: create numerical arrays from strings

OlyDLG
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 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
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: create numerical arrays from strings

Alan Isaac
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
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: create numerical arrays from strings

R Schumacher
In reply to this post by Alan Isaac
At 05:46 AM 2/6/2014, Alan G Isaac wrote:
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.

my $.02:
'1 2; 3 4'
is a non-obvious and non-intuitive way to describe a 2D array or matrix - and try explaining later that the values are actually stored in memory as 1,3,2,4 and why and watch the freshman chins drop...

>>> np.array([[1,2],
...           [3,4]])
...           
array([[1, 2],
       [3, 4]])
Why use both significant whitespace and punctuation to separate elements?

I've billed many months rewriting old Matlab code into Python - please don't saddle future engineers with a closed, non-objective, expensive product based on FORTRAN and written in C that breaks old code with every release. </rant>
There are so many fine, easy tutorials like http://wiki.scipy.org/Tentative_NumPy_Tutorial
http://www.loria.fr/~rougier/teaching/matplotlib/

- Ray




_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion