w3resource
NumPy Tutorial

NumPy: Array creation routines

Array creation routines

NumPy: numpy-logo
Ones and zeros
Name Description Syntax
empty() Return a new array of given shape and type, without initializing entries. empty(shape[, dtype, order])
empty_like Return a new array with the same shape and type as a given array. empty_like(a[, dtype, order, subok])
eye() Return a 2-D array with ones on the diagonal and zeros elsewhere. eye(N[, M, k, dtype])
identity() Return the identity array. identity(n[, dtype])
ones() Return a new array of given shape and type, filled with ones. ones(shape[, dtype, order])
ones_like Return an array of ones with the same shape and type as a given array. ones_like(a[, dtype, order, subok])
zeros Return a new array of given shape and type, filled with zeros. zeros(shape[, dtype, order])
zeros_like Return an array of zeros with the same shape and type as a given array. zeros_like(a[, dtype, order, subok])
full() Return a new array of given shape and type, filled with fill_value. full(shape, fill_value[, dtype, order])
full_like() Return a full array with the same shape and type as a given array. full_like(a, fill_value[, dtype, order, subok])
From existing data
Name Description Syntax
array() Create an array. array(object[, dtype, copy, order, subok, ndmin])
asarray() Convert the input to an array. asarray(a[, dtype, order])
asanyarray() Convert the input to an ndarray, but pass ndarray subclasses through. asanyarray(a[, dtype, order])
ascontiguousarray() Return a contiguous array in memory (C order). ascontiguousarray(a[, dtype])
asmatrix() Interpret the input as a matrix. asmatrix(data[, dtype])
copy() Return an array copy of the given object. copy(a[, order]
frombuffer() Interpret a buffer as a 1-dimensional array. frombuffer(buffer[, dtype, count, offset])
fromfile() Construct an array from data in a text or binary file. fromfile(file[, dtype, count, sep])
fromfunction() Construct an array by executing a function over each coordinate. fromfunction(function, shape, **kwargs)
fromiter() Create a new 1-dimensional array from an iterable object. fromiter(iterable, dtype[, count])
fromstring() A new 1-D array initialized from raw binary or text data in a string. fromstring(string[, dtype, count, sep])
loadtxt() Load data from a text file. loadtxt(fname[, dtype, comments, delimiter, ...])

Creating record arrays (numpy.rec)

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

Creating character arrays (numpy.char)

Name Description Syntax
core.defchararray.array() Create a chararray. core.defchararray.array(obj[, itemsize, ...])
core.defchararray.array() Convert the input to a chararray, copying the data only if necessary. core.defchararray.asarray(obj[, itemsize, ...])
Numerical ranges
Name Description Syntax
arange() Return evenly spaced values within a given interval. arange([start,] stop[, step,][, dtype])
linspace() Return evenly spaced numbers over a specified interval. linspace(start, stop[, num, endpoint, ...])
logspace() Return numbers spaced evenly on a log scale. logspace(start, stop[, num, endpoint, base, ...])
geomspace() Return numbers spaced evenly on a log scale (a geometric progression). geomspace(start, stop[, num, endpoint, dtype])
meshgrid() Return coordinate matrices from coordinate vectors. meshgrid(*xi, **kwargs)
mgrid() nd_grid instance which returns a dense multi-dimensional "meshgrid". mgrid
ogrid() nd_grid instance which returns an open multi-dimensional "meshgrid". ogrid
Building matrices
Name Description Syntax
diag() Extract a diagonal or construct a diagonal array. diag(v[, k])
diagflat() Create a two-dimensional array with the flattened input as a diagonal. diagflat(v[, k])
tri() An array with ones at and below the given diagonal and zeros elsewhere. tri(N[, M, k, dtype])
tril() Lower triangle of an array. tril(m[, k])
triu() Upper triangle of an array. triu(m[, k])
vander() Generate a Vandermonde matrix. vander(x[, N, increasing])
The Matrix class
Name Description Syntax
mat() Interpret the input as a matrix. mat(data[, dtype])
bmat() Build a matrix object from a string, nested sequence, or array. bmat(obj[, ldict, gdict])

Previous: NumPy ndarray
Next: Ones and Zeros empty()