[SciPy-User] getting started with ndarray

David Goldsmith d.l.goldsmith at gmail.com
Wed May 5 22:31:17 EDT 2010


It's rare that one wants to "manually" create arrays using np.array,
however.  Typically, the arrays used in numerics are "standard" in some way
(e.g., the array corresponding to the n x n identity matrix, obtained from
np.eye(n)) and thus numpy has many, _many_ functions which provide these
various standard arrays for you.  Even in the case of an array created from
data in a file, one doesn't read in the data and then pass it as an argument
to np.array - there's a numpy function which reads the file-stored data
directly into an array for you.

DG

On Wed, May 5, 2010 at 7:24 PM, Keith Goodman <kwgoodman at gmail.com> wrote:

> On Wed, May 5, 2010 at 7:18 PM, Victor Eijkhout
> <eijkhout at tacc.utexas.edu> wrote:
> > I found the "guide to numpy" book, but I can't figure out how to
> > create a multi-dimensional array. Is there a short tutorial? Or can
> > someone give me a short example program with the most relevant features?
>
> Here's one way to create arrays:
>
> One dimensional:
>
> >> import numpy as np
> >> x1 = np.array([1, 2, 3])
> >> x1.ndim
>   1
> >> x1
>   array([1, 2, 3])
>
> Two dimensional:
>
> >> x2 = np.array([[1, 2], [3, 4]])
> >> x2.ndim
>   2
> >> x2
> array([[1, 2],
>       [3, 4]])
>
> Three dimensional:
>
> >> x3 = np.random.rand(2, 3, 4)
> >> x3.ndim
>   3
> >> x3.shape
>   (2, 3, 4)
> >> x3
> array([[[ 0.85887601,  0.2988635 ,  0.93155938,  0.48419988],
>        [ 0.677853  ,  0.67478433,  0.7065251 ,  0.49045808],
>        [ 0.87160361,  0.55503905,  0.36378423,  0.39314846]],
>
>       [[ 0.80761194,  0.54838378,  0.80576339,  0.08248982],
>        [ 0.16729305,  0.16320019,  0.5628961 ,  0.77325458],
>        [ 0.7073337 ,  0.08927084,  0.89050264,  0.54985488]]])
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



-- 
Mathematician: noun, someone who disavows certainty when their uncertainty
set is non-empty, even if that set has measure zero.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100505/306510f8/attachment.html>


More information about the SciPy-User mailing list