Array creation routines

See also

Array creation

Ones and zeros

empty (shape[, dtype, order]) Return a new array of given shape and type, without initialising entries.
empty_like (a) Create a new array with the same shape and type as another.
eye (N[, M, k, dtype]) Return a 2-D array with ones on the diagonal and zeros elsewhere.
identity (n[, dtype]) Return the identity array.
ones (shape[, dtype, order]) Return a new array of given shape and type, filled with ones.
ones_like (x[, out]) Returns an array of ones with the same shape and type as a given array.
zeros (shape[, dtype, order]) Return a new array of given shape and type, filled with zeros.
zeros_like (a) Returns an array of zeros with the same shape and type as a given array.

From existing data

array (object[, dtype, copy, order, ...]) Create an array.
asarray (a[, dtype, order]) Convert the input to an array.
asanyarray (a[, dtype, order]) Convert the input to a ndarray, but pass ndarray subclasses through.
ascontiguousarray (a[, dtype]) Return a contiguous array in memory (C order).
asmatrix (data[, dtype]) Interpret the input as a matrix.
copy (a) Return an array copy of the given object.
frombuffer (buffer[, dtype, count, offset]) Interpret a buffer as a 1-dimensional array.
fromfile (file[, dtype, count, sep]) Construct an array from data in a text or binary file.
fromfunction (function, shape, **kwargs) Construct an array by executing a function over each coordinate.
fromiter (iterable, dtype[, count]) Create a new 1-dimensional array from an iterable object.
loadtxt (fname[, dtype, comments, ...]) Load data from a text file.

Creating record arrays (numpy.rec)


numpy.rec is the preferred alias for numpy.core.records.

core.records.array (obj[, dtype, shape, offset, ...]) Construct a record array from a wide-variety of objects.
core.records.fromarrays (arrayList[, dtype, shape, formats, ...]) create a record array from a (flat) list of arrays
core.records.fromrecords (recList[, dtype, shape, formats, ...]) create a recarray from a list of records in text form
core.records.fromstring (datastring[, dtype, shape, offset, ...]) create a (read-only) record array from binary data contained in a string
core.records.fromfile (fd[, dtype, shape, offset, ...]) Create an array from binary file data

Creating character arrays (numpy.char)


numpy.char is the preferred alias for numpy.core.defchararray.

core.defchararray.array (obj[, itemsize, copy, ...])

Numerical ranges

arange ([start,] stop[, step, ...]) Return evenly spaced values within a given interval.
linspace (start, stop[, num, endpoint, retstep]) Return evenly spaced numbers over a specified interval.
logspace (start, stop[, num, endpoint, base]) Return numbers spaced evenly on a log scale.
meshgrid (x, y) Return coordinate matrices from two coordinate vectors.
mgrid Construct a multi-dimensional filled “meshgrid”.

Building matrices

diag (v[, k]) Extract a diagonal or construct a diagonal array.
diagflat (v[, k]) Create a 2-dimensional array with the flattened input as a diagonal.
tri (N[, M, k, dtype]) Construct an array filled with ones at and below the given diagonal.
tril (m[, k]) Lower triangle of an array.
triu (m[, k]) Upper triangle of an array.
vander (x[, N]) Generate a Van der Monde matrix.

The Matrix class

mat (data[, dtype]) Interpret the input as a matrix.
bmat (obj[, ldict, gdict]) Build a matrix object from a string, nested sequence, or array.